at10747: sam l advanced encryption standard (aes)...

48
SMART ARM-based Microcontrollers AT10747: SAM L Advanced Encryption Standard (AES) Driver APPLICATION NOTE Introduction This driver for Atmel ® | SMART ARM ® -based microcontrollers provides an interface for the configuration and management of the device's Advanced Encryption Standard functionality. The following driver API modes are covered by this manual: Polled APIs Callback APIs The Advanced Encryption Standard module supports all five confidentiality modes of operation for symmetrical key block cipher algorithms (as specified in the NIST Special Publication 800-38A Recommendation): Electronic Code Book (ECB) Cipher Block Chaining (CBC) Output Feedback (OFB) Cipher Feedback (CFB) Counter (CTR) The following peripheral is used by this module: AES (Advanced Encryption Standard) The following devices can use this module: Atmel | SMART SAM L21 Atmel | SMART SAM L22 The outline of this documentation is as follows: Prerequisites Module Overview Special Considerations Extra Information Examples API Overview Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-12/2015

Upload: others

Post on 13-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

SMART ARM-based Microcontrollers

AT10747 SAM L Advanced Encryption Standard(AES) Driver

APPLICATION NOTE

Introduction

This driver for Atmelreg | SMART ARMreg-based microcontrollers provides aninterface for the configuration and management of the devices AdvancedEncryption Standard functionality The following driver API modes arecovered by this manual

bull Polled APIsbull Callback APIs

The Advanced Encryption Standard module supports all five confidentialitymodes of operation for symmetrical key block cipher algorithms (as specifiedin the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

The following peripheral is used by this modulebull AES (Advanced Encryption Standard)

The following devices can use this modulebull Atmel | SMART SAM L21bull Atmel | SMART SAM L22

The outline of this documentation is as followsbull Prerequisitesbull Module Overviewbull Special Considerationsbull Extra Informationbull Examplesbull API Overview

Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Table of Contents

Introduction1

1 Software License 4

2 Prerequisites5

3 Module Overview631 Encryption and Decryption 632 Hardware Countermeasures 633 Galois Counter Mode (GCM)7

4 Special Considerations8

5 Extra Information 9

6 Examples10

7 API Overview 1171 Variable and Type Definitions 11

711 Type aes_callback_t1172 Structure Definitions 11

721 Struct aes_config 1173 Macro Definitions11

731 Module Status Flags 1174 Function Definitions12

741 Configuration and Initialization12742 Start Enable and Write13743 Status Management16744 Galois Counter Mode17745 Callback Configuration and Initialization19746 Callback Enabling and Disabling 19

75 Enumeration Definitions 20751 Enum aes_callback_type20752 Enum aes_cfb_size 21753 Enum aes_countermeature_type21754 Enum aes_encrypt_mode21755 Enum aes_key_size22756 Enum aes_operation_mode22757 Enum aes_start_mode22

8 Extra Information for Advanced Encryption Standard2381 Acronyms2382 Dependencies2383 Errata2384 Module History23

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

2

9 Examples for Advanced Encryption Standard 2491 Quick Start Guide for AES - Basic24

911 Quick Start 24912 Use Case 28

92 Quick Start Guide for AES - Callback29921 Quick Start Callback 29922 Use Case 40

93 Quick Start Guide for AES - DMA41931 Quick Start 41932 Use Case 45

10 Document Revision History 47

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

3

1 Software LicenseRedistribution and use in source and binary forms with or without modification are permitted providedthat the following conditions are met

1 Redistributions of source code must retain the above copyright notice this list of conditions and thefollowing disclaimer

2 Redistributions in binary form must reproduce the above copyright notice this list of conditions and thefollowing disclaimer in the documentation andor other materials provided with the distribution

3 The name of Atmel may not be used to endorse or promote products derived from this software withoutspecific prior written permission

4 This software may only be redistributed and used in connection with an Atmel microcontroller product

THIS SOFTWARE IS PROVIDED BY ATMEL AS IS AND ANY EXPRESS OR IMPLIED WARRANTIESINCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESSFOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLYDISCLAIMED IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTALSPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TOPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS ORBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETHERIN CONTRACT STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OFSUCH DAMAGE

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

4

2 PrerequisitesThere are no prerequisites for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

5

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 2: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table of Contents

Introduction1

1 Software License 4

2 Prerequisites5

3 Module Overview631 Encryption and Decryption 632 Hardware Countermeasures 633 Galois Counter Mode (GCM)7

4 Special Considerations8

5 Extra Information 9

6 Examples10

7 API Overview 1171 Variable and Type Definitions 11

711 Type aes_callback_t1172 Structure Definitions 11

721 Struct aes_config 1173 Macro Definitions11

731 Module Status Flags 1174 Function Definitions12

741 Configuration and Initialization12742 Start Enable and Write13743 Status Management16744 Galois Counter Mode17745 Callback Configuration and Initialization19746 Callback Enabling and Disabling 19

75 Enumeration Definitions 20751 Enum aes_callback_type20752 Enum aes_cfb_size 21753 Enum aes_countermeature_type21754 Enum aes_encrypt_mode21755 Enum aes_key_size22756 Enum aes_operation_mode22757 Enum aes_start_mode22

8 Extra Information for Advanced Encryption Standard2381 Acronyms2382 Dependencies2383 Errata2384 Module History23

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

2

9 Examples for Advanced Encryption Standard 2491 Quick Start Guide for AES - Basic24

911 Quick Start 24912 Use Case 28

92 Quick Start Guide for AES - Callback29921 Quick Start Callback 29922 Use Case 40

93 Quick Start Guide for AES - DMA41931 Quick Start 41932 Use Case 45

10 Document Revision History 47

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

3

1 Software LicenseRedistribution and use in source and binary forms with or without modification are permitted providedthat the following conditions are met

1 Redistributions of source code must retain the above copyright notice this list of conditions and thefollowing disclaimer

2 Redistributions in binary form must reproduce the above copyright notice this list of conditions and thefollowing disclaimer in the documentation andor other materials provided with the distribution

3 The name of Atmel may not be used to endorse or promote products derived from this software withoutspecific prior written permission

4 This software may only be redistributed and used in connection with an Atmel microcontroller product

THIS SOFTWARE IS PROVIDED BY ATMEL AS IS AND ANY EXPRESS OR IMPLIED WARRANTIESINCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESSFOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLYDISCLAIMED IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTALSPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TOPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS ORBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETHERIN CONTRACT STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OFSUCH DAMAGE

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

4

2 PrerequisitesThere are no prerequisites for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

5

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 3: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

9 Examples for Advanced Encryption Standard 2491 Quick Start Guide for AES - Basic24

911 Quick Start 24912 Use Case 28

92 Quick Start Guide for AES - Callback29921 Quick Start Callback 29922 Use Case 40

93 Quick Start Guide for AES - DMA41931 Quick Start 41932 Use Case 45

10 Document Revision History 47

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

3

1 Software LicenseRedistribution and use in source and binary forms with or without modification are permitted providedthat the following conditions are met

1 Redistributions of source code must retain the above copyright notice this list of conditions and thefollowing disclaimer

2 Redistributions in binary form must reproduce the above copyright notice this list of conditions and thefollowing disclaimer in the documentation andor other materials provided with the distribution

3 The name of Atmel may not be used to endorse or promote products derived from this software withoutspecific prior written permission

4 This software may only be redistributed and used in connection with an Atmel microcontroller product

THIS SOFTWARE IS PROVIDED BY ATMEL AS IS AND ANY EXPRESS OR IMPLIED WARRANTIESINCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESSFOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLYDISCLAIMED IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTALSPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TOPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS ORBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETHERIN CONTRACT STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OFSUCH DAMAGE

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

4

2 PrerequisitesThere are no prerequisites for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

5

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 4: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

1 Software LicenseRedistribution and use in source and binary forms with or without modification are permitted providedthat the following conditions are met

1 Redistributions of source code must retain the above copyright notice this list of conditions and thefollowing disclaimer

2 Redistributions in binary form must reproduce the above copyright notice this list of conditions and thefollowing disclaimer in the documentation andor other materials provided with the distribution

3 The name of Atmel may not be used to endorse or promote products derived from this software withoutspecific prior written permission

4 This software may only be redistributed and used in connection with an Atmel microcontroller product

THIS SOFTWARE IS PROVIDED BY ATMEL AS IS AND ANY EXPRESS OR IMPLIED WARRANTIESINCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESSFOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLYDISCLAIMED IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTALSPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TOPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS ORBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETHERIN CONTRACT STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OFSUCH DAMAGE

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

4

2 PrerequisitesThere are no prerequisites for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

5

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 5: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

2 PrerequisitesThere are no prerequisites for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

5

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 6: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

3 Module OverviewThe Advanced Encryption Standard (AES) is a specification for the encryption of electronic dataestablished by the US National Institute of Standards and Technology (NIST) in 2001 It is compliant withthe American FIPS (Federal Information Processing Standard) Publication 197 specification

The AES supports all five confidentiality modes of operation for symmetrical key block cipher algorithms(as specified in the NIST Special Publication 800-38A Recommendation)

bull Electronic Code Book (ECB)bull Cipher Block Chaining (CBC)bull Output Feedback (OFB)bull Cipher Feedback (CFB)bull Counter (CTR)

Data transfers both to and from the AES module can occur using the peripheral DMA controller channelsthus minimizing processor intervention for large data buffer transfers

As soon as the initialization vector the input data and the key are configured the encryptiondecryptionprocess may be started Once the process has completed the encrypteddecrypted data can be read outvia registers or through DMA channels

31 Encryption and DecryptionThe AES is capable of using cryptographic keys of 128192256 bits to encrypt and decrypt data in blocksof 128 bits In Cipher Feedback Mode (CFB) five data sizes are possible (8 16 32 64 or 128 bits)

The input to the encryption processes of the CBC CFB and OFB modes includes in addition to theplaintext a 128-bit data block called the Initialization Vector (IV) The Initialization Vector is used in theinitial step in the encryption of a message and in the corresponding decryption of the message

There are three encryptiondecryption start modesbull Manual Mode Start encryptiondecryption manuallybull Auto Start Mode Once the correct number of input data registers is written processing is

automatically started DMA operation uses this modebull Last Output Data Mode (LOD) This mode is used to generate message authentication code (MAC)

on data in CCM mode of operation

32 Hardware CountermeasuresThe AES module features four types of hardware countermeasures that are useful for protecting dataagainst differential power analysis attacks

bull Type 1 Randomly add one cycle to data processingbull Type 2 Randomly add one cycle to data processing (other version)bull Type 3 Add a random number of clock cycles to data processing subject to a maximum of

111315 clock cycles for key sizes of 128192256 bitsbull Type 4 Add random spurious power consumption during data processing

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

6

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 7: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

33 Galois Counter Mode (GCM)GCM is comprised of the AES engine in CTR mode along with a universal hash function (GHASH engine)that is defined over a binary Galois field to produce a message authentication tag The GHASH engineprocesses data packets after the AES operation GCM provides assurance of the confidentiality of datathrough the AES Counter mode of operation for DRAFT 920 encryption Authenticity of the confidentialdata is assured through the GHASH engine Refer to the NIST Special Publication 800-38DRecommendation for more complete information

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

7

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 8: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

4 Special ConsiderationsThere are no special considerations for this module

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

8

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 9: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

5 Extra InformationFor extra information see Extra Information for Advanced Encryption Standard This includes

bull Acronymsbull Dependenciesbull Erratabull Module History

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

9

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 10: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

6 ExamplesFor a list of examples related to this driver see Examples for Advanced Encryption Standard

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

10

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 11: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

7 API Overview

71 Variable and Type Definitions

711 Type aes_callback_t

typedef void( aes_callback_t )(void)

AES interrupt callback function type

72 Structure Definitions

721 Struct aes_config

AES Configuration structure

Table 7-1 Members

Type Name Description

enum aes_cfb_size cfb_size Cipher feedback data size

enum aes_countermeature_type ctype Countermeasure type

bool enable_key_gen Enable key generation

bool enable_xor_key Enable XOR key

enum aes_encrypt_mode encrypt_mode AES data mode (decryption or encryption)

enum aes_key_size key_size AES key size

bool lod Last output data mode enabledisable

enum aes_operation_mode opmode AES cipher operation mode

enum aes_start_mode start_mode Start mode

73 Macro Definitions

731 Module Status Flags

AES status flags returned by aes_get_status() and cleared by aes_clear_status()

7311 Macro AES_ENCRYPTION_COMPLETE

define AES_ENCRYPTION_COMPLETE

AES encryption complete

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

11

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 12: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

7312 Macro AES_GF_MULTI_COMPLETE

define AES_GF_MULTI_COMPLETE

AES GF multiplication complete

74 Function Definitions

741 Configuration and Initialization

7411 Function aes_get_config_defaults()

Initializes an AES configuration structure to defaults

void aes_get_config_defaults( struct aes_config const config)

Initializes the specified AES configuration structure to a set of known default values

Note  This function should be called to initialize all new instances of AES configuration structures beforethey are further modified by the user application

The default configuration is as followsbull Data encryptionbull 128-bit AES key sizebull 128-bit cipher feedback sizebull Manual start modebull Electronic Codebook (ECB) modebull All countermeasures are enabledbull XRO key is disabledbull Key generation is disabledbull Last output data mode is disabled

Table 7-2 Parameters

Data direction Parameter name Description

[out] config Pointer to an AES configuration structure

7412 Function aes_set_config()

Configure the AES module

void aes_set_config( struct aes_module const module Aes const hw struct aes_config const config)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

12

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 13: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table 7-3 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

7413 Function aes_init()

Initialize the AES module

void aes_init( struct aes_module const module Aes const hw struct aes_config const config)

Table 7-4 Parameters

Data direction Parameter name Description

[out] module Pointer to the software instance struct

[in] hw Module hardware register base address pointer

[in] config Pointer to an AES configuration structure

742 Start Enable and Write

7421 Function aes_start()

Start a manual encryptiondecryption process

void aes_start( struct aes_module const module)

Table 7-5 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7422 Function aes_set_new_message()

Notifies the module that the next input data block is the beginning of a new message

void aes_set_new_message( struct aes_module const module)

Table 7-6 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

13

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 14: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

7423 Function aes_clear_new_message()

Clear the indication of the beginning for a new message

void aes_clear_new_message( struct aes_module const module)

Table 7-7 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7424 Function aes_enable()

Enable the AES module

void aes_enable( struct aes_module const module)

Table 7-8 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

7425 Function aes_disable()

Disable the AES module

void aes_disable( struct aes_module const module)

Table 7-9 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

7426 Function aes_write_key()

Write the 128192256-bit cryptographic key

void aes_write_key( struct aes_module const module const uint32_t key)

Table 7-10 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] key Pointer to 468 contiguous 32-bit words

Note  The key size depends on the current AES configuration

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

14

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 15: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

7427 Function aes_write_init_vector()

Write the initialization vector (for the CBC CFB OFB CTR and GCM cipher modes)

void aes_write_init_vector( struct aes_module const module const uint32_t vector)

Table 7-11 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] vector Pointer to four contiguous 32-bit words

7428 Function aes_write_input_data()

Write the input data (four consecutive 32-bit words)

void aes_write_input_data( struct aes_module const module const uint32_t p_input_data_buffer)

Table 7-12 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] input_data_buffer Pointer to an input data buffer

7429 Function aes_read_output_data()

Read the output data

void aes_read_output_data( struct aes_module const module uint32_t p_output_data_buffer)

Note  The data buffer that holds the processed data must be large enough to hold four consecutive 32-bit words

Table 7-13 Parameters

Data direction Parameter name Description

[in] module Pointer to the software instance struct

[in] output_data_buffer Pointer to an output buffer

74210 Function aes_write_random_seed()

Write AES random seed

void aes_write_random_seed( struct aes_module const module uint32_t seed)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

15

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 16: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table 7-14 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] seed Seed for the random number generator

743 Status Management

7431 Function aes_get_status()

Retrieves the current module status

uint32_t aes_get_status( struct aes_module const module)

Retrieves the status of the module giving overall state information

Table 7-15 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

Table 7-16 Return Values

Return value Description

AES_ENCRYPTION_COMPLETE AES encryption complete

AES_GF_MULTI_COMPLETE AES GF multiplication complete

7432 Function aes_clear_status()

Clears a module status flag

void aes_clear_status( struct aes_module const module const uint32_t status_flags)

Clears the given status flag of the module

Table 7-17 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] status_flags Bitmask flags to clear

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

16

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 17: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

744 Galois Counter Mode

7441 Function aes_gcm_read_ghash()

Get the AES GCM Hash Value

uint32_t aes_gcm_read_ghash( struct aes_module const module uint32_t id)

Table 7-18 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASH array (range 0 to 3)

ReturnsThe content of the GHASHRx[x = 03] value

7442 Function aes_gcm_write_ghash()

Set the AES GCM Hash Value

void aes_gcm_write_ghash( struct aes_module const module uint32_t id uint32_t ghash)

Table 7-19 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the GHASHx array (range 0 to 3)

[in] ghash GCM hash value

7443 Function aes_gcm_read_hash_key()

Get AES GCM Hash key

uint32_t aes_gcm_read_hash_key( struct aes_module const module uint32_t id)

Table 7-20 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

ReturnsThe contents of the HASHKEYx[x = 03] specified

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

17

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 18: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

7444 Function aes_gcm_write_hash_key()

Set the AES GCM Hash key

void aes_gcm_write_hash_key( struct aes_module const module uint32_t id uint32_t key)

Table 7-21 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] id Index into the Hash key array (range 0 to 3)

[in] key GCM Hash key

7445 Function aes_gcm_read_cipher_len()

Get the AES GCM cipher length

uint32_t aes_gcm_read_cipher_len( struct aes_module const module)

Table 7-22 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

ReturnsThe contents of the HASHKEYx[x = 03] specified

7446 Function aes_gcm_write_cipher_len()

Set the AES GCM cipher length

void aes_gcm_write_cipher_len( struct aes_module const module uint32_t len)

Table 7-23 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

[in] len Cipher length

7447 Function aes_gcm_set_end_message_status()

Set GCM end of input message status

void aes_gcm_set_end_message_status( struct aes_module const module)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

18

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 19: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table 7-24 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7448 Function aes_gcm_clear_end_message_status()

Clear GCM end of input message status

void aes_gcm_clear_end_message_status( struct aes_module const module)

Table 7-25 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

7449 Function aes_gcm_set_gf_multiplication()

Set GF multiplication of GCM mode

void aes_gcm_set_gf_multiplication( struct aes_module const module)

Table 7-26 Parameters

Data direction Parameter name Description

[in] module Pointer to the AES software instance struct

745 Callback Configuration and Initialization

7451 Function aes_register_callback()

enum status_code aes_register_callback( const aes_callback_t callback const enum aes_callback_type type)

7452 Function aes_unregister_callback()

enum status_code aes_unregister_callback( const aes_callback_t callback const enum aes_callback_type type)

746 Callback Enabling and Disabling

7461 Function aes_enable_callback()

Enable an AES callback

enum status_code aes_enable_callback( struct aes_module const module const enum aes_callback_type type)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

19

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 20: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table 7-27 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-28 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

7462 Function aes_disable_callback()

Disable an AES callback

enum status_code aes_disable_callback( struct aes_module const module const enum aes_callback_type type)

Table 7-29 Parameters

Data direction Parameter name Description

[in out] module Pointer to the software instance struct

[in] type Callback source type

ReturnsStatus of the callback enable operation

Table 7-30 Return Values

Return value Description

STATUS_OK The callback was enabled successfully

STATUS_ERR_INVALID_ARG If an invalid callback type was supplied

75 Enumeration Definitions

751 Enum aes_callback_type

AES callback type

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

20

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 21: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Table 7-31 Members

Enum value Description

AES_CALLBACK_ENCRYPTION_COMPLETE Encryption complete callback

AES_CALLBACK_GF_MULTI_COMPLETE GF Multiplication Complete callback

752 Enum aes_cfb_size

AES Cipher FeedBack (CFB) size

Table 7-32 Members

Enum value Description

AES_CFB_SIZE_128 Cipher feedback data size is 128-bit

AES_CFB_SIZE_64 Cipher feedback data size is 64-bit

AES_CFB_SIZE_32 Cipher feedback data size is 32-bit

AES_CFB_SIZE_16 Cipher feedback data size is 16-bit

AES_CFB_SIZE_8 Cipher feedback data size is 8-bit

753 Enum aes_countermeature_type

AES countermeasure type

Table 7-33 Members

Enum value Description

AES_COUNTERMEASURE_TYPE_disabled Countermeasure type all disabled

AES_COUNTERMEASURE_TYPE_1 Countermeasure1 enabled

AES_COUNTERMEASURE_TYPE_2 Countermeasure2 enabled

AES_COUNTERMEASURE_TYPE_3 Countermeasure3 enabled

AES_COUNTERMEASURE_TYPE_4 Countermeasure4 enabled

AES_COUNTERMEASURE_TYPE_ALL Countermeasure type all enabled

754 Enum aes_encrypt_mode

AES processing mode

Table 7-34 Members

Enum value Description

AES_DECRYPTION Decryption of data will be performed

AES_ENCRYPTION Encryption of data will be performed

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

21

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 22: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

755 Enum aes_key_size

AES cryptographic key size

Table 7-35 Members

Enum value Description

AES_KEY_SIZE_128 AES key size is 128-bit

AES_KEY_SIZE_192 AES key size is 192-bit

AES_KEY_SIZE_256 AES key size is 256-bit

756 Enum aes_operation_mode

AES operation mode

Table 7-36 Members

Enum value Description

AES_ECB_MODE Electronic Codebook (ECB)

AES_CBC_MODE Cipher Block Chaining (CBC)

AES_OFB_MODE Output Feedback (OFB)

AES_CFB_MODE Cipher Feedback (CFB)

AES_CTR_MODE Counter (CTR)

AES_CCM_MODE Counter (CCM)

AES_GCM_MODE Galois Counter Mode (GCM)

757 Enum aes_start_mode

AES start mode

Table 7-37 Members

Enum value Description

AES_MANUAL_START Manual start mode

AES_AUTO_START Auto start mode

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

22

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 23: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

8 Extra Information for Advanced Encryption Standard

81 Acronyms

Below is a table listing the acronyms used in this module along with their intended meanings

Acronym Definition

AAD Additional Authenticated Data

CBC Cipher Block Chaining

CFB Cipher Feedback

CTR Counter

DMA Direct Memory Access

DMAC DMA Controller

ECB Electronic Codebook

GCM Galois Counter Mode

OFB Output Feedback

QSG Quick Start Guide

82 Dependencies

This driver has the following dependencies

bull None

83 Errata

There are no errata related to this driver

84 Module History

An overview of the module history is presented in the table below with details on the enhancements andfixes made to the module since its first release The current version of this corresponds to the newestversion in the table

Changelog

Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

23

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 24: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

9 Examples for Advanced Encryption StandardThis is a list of the available Quick Start Guides (QSGs) and example applications for SAM AdvancedEncryption Standard (AES) Driver QSGs are simple examples with step-by-step instructions to configureand use this driver in a selection of use cases Note that a QSG can be compiled as a standaloneapplication or be added to the user application

bull Quick Start Guide for AES - Basicbull Quick Start Guide for AES - Callbackbull Quick Start Guide for AES - DMA

91 Quick Start Guide for AES - BasicThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryption

Upon startup the program uses the USART driver to display application output message from which ECBencryptiondecryption modes can be tested

911 Quick Start

9111 Prerequisites

There are no prerequisites for this use case

9112 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

24

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 25: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

25

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 26: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and consolesystem_init()configure_usart()

aes_get_config_defaults(ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

26

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 27: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

aes_init(ampaes_instanceAES ampg_aes_cfg)aes_enable(ampaes_instance)

9113 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE]

State indicate volatile bool state = false AES configuration struct aes_config g_aes_cfg AES instancestruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

27

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 28: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

912 Use Case

9121 Code

Copy-paste the following code to your user application Run ECB mode testecb_mode_test()

9122 Workflow

1 Configure ECB mode encryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

2 Configure ECB mode decryption and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

28

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 29: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb)

Wait for the end of the decryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE)) aes_read_output_data(ampaes_instanceoutput_data)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

92 Quick Start Guide for AES - CallbackThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption and decryptionbull CBC encryption and decryptionbull CFB128 encryption and decryptionbull OFB encryption and decryptionbull CTR encryption and decryption

Upon startup the program uses the USART driver to display application output message from whichseveral encryptiondecryption modes can be tested

921 Quick Start Callback

9211 Prerequisites

There are no prerequisites for this use case

9212 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

29

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 30: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

30

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 31: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Add to the main application source file outside of any functions Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user application

static void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void aes_callback(void) Read the output aes_read_output_data(ampaes_instance output_data) state = true

static void ecb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg) Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

aes_set_new_message(ampaes_instance)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

31

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 32: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ecb) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cbc_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

32

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 33: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

printf(- CBC cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

if ((ref_cipher_text_cbc[0] = output_data[0]) || (ref_cipher_text_cbc[1] = output_data[1]) || (ref_cipher_text_cbc[2] = output_data[2]) || (ref_cipher_text_cbc[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CBC decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CBC_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

33

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 34: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

aes_write_init_vector(ampaes_instance init_vector)

aes_set_new_message(ampaes_instance) Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cbc) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void cfb128_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_cfb128[0] = output_data[0]) || (ref_cipher_text_cfb128[1] = output_data[1]) || (ref_cipher_text_cfb128[2] = output_data[2]) || (ref_cipher_text_cfb128[3] = output_data[3])) printf(rnKOrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

34

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 35: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CFB128 decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_cfb128) aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ofb_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

35

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 36: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance) Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ofb[0] = output_data[0]) || (ref_cipher_text_ofb[1] = output_data[1]) || (ref_cipher_text_ofb[2] = output_data[2]) || (ref_cipher_text_ofb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- OFB decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_OFB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ofb)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait for the end of the decryption process while (false == state)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

36

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 37: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void ctr_mode_test(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR cipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr) aes_set_new_message(ampaes_instance) Write the data to be ciphered to the input data registers aes_write_input_data(ampaes_instance ref_plain_text) aes_clear_new_message(ampaes_instance)

Wait for the end of the encryption process while (false == state)

check the result if ((ref_cipher_text_ctr[0] = output_data[0]) || (ref_cipher_text_ctr[1] = output_data[1]) || (ref_cipher_text_ctr[2] = output_data[2]) || (ref_cipher_text_ctr[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- CTR decipher modern) printf(- Auto start modern) printf(- 4 32bit wordsrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

37

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 38: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_DECRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_MANUAL_START g_aes_cfgopmode = AES_CTR_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

Set the initialization vector aes_write_init_vector(ampaes_instance init_vector_ctr)

Write the data to be deciphered to the input data registers aes_write_input_data(ampaes_instance ref_cipher_text_ctr)

aes_set_new_message(ampaes_instance) aes_start(ampaes_instance) aes_clear_new_message(ampaes_instance) Wait for the end of the decryption process while (false == state)

check the result if ((ref_plain_text[0] = output_data[0]) || (ref_plain_text[1] = output_data[1]) || (ref_plain_text[2] = output_data[2]) || (ref_plain_text[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE) aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

38

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 39: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

9213 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = 0xacab4976 0x46b21981 0x9b8ee9ce 0x7d19e912

uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = 0x2ed93f3b 0x20ad2db7 0xf8493433 0x4afb3ce8

uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = 0x91614d87 0x26e320b6 0x6468ef1b 0xceb60d99

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

const uint32_t init_vector[4] = 0x03020100 0x07060504 0x0b0a0908 0x0f0e0d0c

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

39

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 40: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

const uint32_t init_vector_ctr[4] = 0xf3f2f1f0 0xf7f6f5f4 0xfbfaf9f8 0xfffefdfc

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = false

struct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Configure initialize and enable AES module1 Configuration AES struct which can be filled out to adjust the configuration of a physical AES

peripheralaes_get_config_defaults(ampg_aes_cfg)

2 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

3 Enable the AES moduleaes_enable(ampaes_instance)

4 Register and enable the AES module callback Enable AES interrupt aes_register_callback(aes_callbackAES_CALLBACK_ENCRYPTION_COMPLETE)aes_enable_callback(ampaes_instanceAES_CALLBACK_ENCRYPTION_COMPLETE)

922 Use Case

9221 Code

Copy-paste the following code to your user applicationecb_mode_test()cbc_mode_test()cfb128_mode_test()ofb_mode_test()ctr_mode_test()

9222 Workflow

1 Configure ECB mode encryption and decryption and run testecb_mode_test()

2 Configure CBC mode encryption and decryption and run testcbc_mode_test()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

40

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 41: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

3 Configure CFB mode encryption and decryption and run testcfb128_mode_test()

4 Configure OFB mode encryption and decryption and run testofb_mode_test()

5 Configure CTR mode encryption and decryption and run testctr_mode_test()

93 Quick Start Guide for AES - DMAThe supported board list

bull SAM L21 Xplained Probull SAM L22 Xplained Pro

This example demonstrates how to use the AES driver to performbull ECB encryption with DMA

Upon startup the program uses the USART driver to display application output message from which ECBDMA encryption modes can be tested

931 Quick Start

9311 Prerequisites

There are no prerequisites for this use case

9312 Code

Add to the main application source file outside of any functionsdefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28 0x8815f7ab 0x3c4fcf09

Add to the main application source file outside of any functions

Output data array

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

41

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 42: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

Copy-paste the following setup code to your user applicationstatic void configure_usart(void)

struct usart_config config_usart usart_get_config_defaults(ampconfig_usart) config_usartbaudrate = 38400 config_usartmux_setting = EDBG_CDC_SERCOM_MUX_SETTING config_usartpinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0 config_usartpinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1 config_usartpinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2 config_usartpinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3 stdio_serial_init(ampusart_instance EDBG_CDC_MODULE ampconfig_usart) usart_enable(ampusart_instance)

static void ecb_mode_test_dma(void) printf(rn-----------------------------------rn) printf(- 128bit cryptographic keyrn) printf(- ECB cipher modern) printf(- DMA modern) printf(- 4 32bit words with DMArn) printf(-----------------------------------rn)

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

42

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 43: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

static void transfer_tx_rx_done(struct dma_resource const resource ) state = true

static void configure_dma_aes_wr(void) struct dma_resource_config tx_config dma_get_config_defaults(amptx_config)

tx_configperipheral_trigger = AES_DMAC_ID_WR tx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_tx amptx_config)

struct dma_descriptor_config tx_descriptor_config

dma_descriptor_get_config_defaults(amptx_descriptor_config)

tx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD tx_descriptor_configdst_increment_enable = false tx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE tx_descriptor_configsource_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text) tx_descriptor_configdestination_address =(uint32_t) amp(AES-gtINDATA) dma_descriptor_create(ampexample_descriptor_tx amptx_descriptor_config)

dma_add_descriptor(ampexample_resource_tx ampexample_descriptor_tx)

static void configure_dma_aes_rd(void)

struct dma_resource_config rx_config

dma_get_config_defaults(amprx_config)

rx_configperipheral_trigger = AES_DMAC_ID_RD rx_configtrigger_action = DMA_TRIGGER_ACTON_BLOCK

dma_allocate(ampexample_resource_rx amprx_config)

struct dma_descriptor_config rx_descriptor_config

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

43

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 44: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

dma_descriptor_get_config_defaults(amprx_descriptor_config)

rx_descriptor_configbeat_size = DMA_BEAT_SIZE_WORD rx_descriptor_configsrc_increment_enable = false rx_descriptor_configblock_transfer_count = AES_EXAMPLE_REFBUF_SIZE rx_descriptor_configsource_address = (uint32_t)amp(AES-gtINDATA) rx_descriptor_configdestination_address = (uint32_t)output_data + sizeof(output_data)

dma_descriptor_create(ampexample_descriptor_rx amprx_descriptor_config)

dma_add_descriptor(ampexample_resource_rx ampexample_descriptor_rx)

Add to user application initialization (typically the start of main())

Initialize the system and console system_init() configure_usart()

Configure AES DMA and enable callback configure_dma_aes_wr() configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE) dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

aes_get_config_defaults(ampg_aes_cfg) aes_init(ampaes_instanceAES ampg_aes_cfg)

aes_enable(ampaes_instance)

9313 Workflow

1 Define sample data from NIST-800-38A appendix F for ECB modedefine AES_EXAMPLE_REFBUF_SIZE 4

uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = 0xe2bec16b 0x969f402e 0x117e3de9 0x2a179373

uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = 0xb47bd73a 0x60367a0d 0xf3ca9ea8 0x97ef6624

const uint32_t key128[4] = 0x16157e2b 0xa6d2ae28

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

44

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 45: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

0x8815f7ab 0x3c4fcf09

2 Create related module variable and software instance structure

Output data array static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE] State indicate volatile bool state = falsestruct aes_config g_aes_cfgstruct aes_module aes_instancestruct usart_module usart_instance

3 Create DMA resource struct and descriptorstruct dma_resource example_resource_txstruct dma_resource example_resource_rxCOMPILER_ALIGNED(16)DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTORDmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR

4 Configure initialize and enable AES module1 Configuration AES DMA module which can be used for AES

Configure AES DMA and enable callback configure_dma_aes_wr()configure_dma_aes_rd()

dma_register_callback(ampexample_resource_tx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_tx DMA_CALLBACK_TRANSFER_DONE)

dma_register_callback(ampexample_resource_rx transfer_tx_rx_done DMA_CALLBACK_TRANSFER_DONE)dma_enable_callback(ampexample_resource_rx DMA_CALLBACK_TRANSFER_DONE)

2 Configuration AES struct which can be filled out to adjust the configuration of a physical AESperipheralaes_get_config_defaults(ampg_aes_cfg)

3 Initialize the AES configuration struct with the modules default valuesaes_init(ampaes_instanceAES ampg_aes_cfg)

4 Enable the AES moduleaes_enable(ampaes_instance)

932 Use Case

9321 Code

Copy-paste the following code to your user application ECB mode encryption test with DMA ecb_mode_test_dma()

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

45

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 46: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

9322 Workflow

1 Configure ECB mode encryption with DMA and run test

state = false

Configure the AES g_aes_cfgencrypt_mode = AES_ENCRYPTION g_aes_cfgkey_size = AES_KEY_SIZE_128 g_aes_cfgstart_mode = AES_AUTO_START g_aes_cfgopmode = AES_ECB_MODE g_aes_cfgcfb_size = AES_CFB_SIZE_128 g_aes_cfglod = false aes_set_config(ampaes_instanceAES ampg_aes_cfg)

Set the cryptographic key aes_write_key(ampaes_instance key128)

The initialization vector is not used by the ECB cipher mode

dma_start_transfer_job(ampexample_resource_tx) aes_set_new_message(ampaes_instance) aes_clear_new_message(ampaes_instance)

Wait DMA transfer while (false == state)

Wait for the end of the encryption process while ((aes_get_status(ampaes_instance) amp AES_ENCRYPTION_COMPLETE))

state = false dma_start_transfer_job(ampexample_resource_rx)

Wait DMA transfer while (false == state)

if ((ref_cipher_text_ecb[0] = output_data[0]) || (ref_cipher_text_ecb[1] = output_data[1]) || (ref_cipher_text_ecb[2] = output_data[2]) || (ref_cipher_text_ecb[3] = output_data[3])) printf(rnKOrn) else printf(rnOKrn)

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

46

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 47: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

10 Document Revision HistoryDoc Rev Date Comments

42445B 122015 Added support for SAM L22

42445A 062015 Initial release

Atmel AT10747 SAM L Advanced Encryption Standard (AES) Driver [APPLICATION NOTE]Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

47

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History
Page 48: AT10747: SAM L Advanced Encryption Standard (AES) Driverww1.microchip.com/.../Atmel-42445-SAM-Advanced-Encryption-Stan… · The Advanced Encryption Standard (AES) is a specification

Atmel Corporation 1600 Technology Drive San Jose CA 95110 USA T (+1)(408) 4410311 F (+1)(408) 4364200 | wwwatmelcom

copy 2015 Atmel Corporation Rev Atmel-42445B-SAM-Advanced-Encryption-Standard-AES-Driver_AT10747_Application Note-122015

Atmelreg Atmel logo and combinations thereof Enabling Unlimited Possibilitiesreg and others are registered trademarks or trademarks of Atmel Corporation in US andother countries ARMreg ARM Connectedreg and others are registered trademarks of ARM Ltd Other terms and product names may be trademarks of others

DISCLAIMER The information in this document is provided in connection with Atmel products No license express or implied by estoppel or otherwise to anyintellectual property right is granted by this document or in connection with the sale of Atmel products EXCEPT AS SET FORTH IN THE ATMEL TERMS ANDCONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS IMPLIEDOR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTY OF MERCHANTABILITYFITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT INDIRECTCONSEQUENTIAL PUNITIVE SPECIAL OR INCIDENTAL DAMAGES (INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS AND PROFITS BUSINESSINTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISEDOF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of thisdocument and reserves the right to make changes to specifications and products descriptions at any time without notice Atmel does not make any commitment toupdate the information contained herein Unless specifically provided otherwise Atmel products are not suitable for and shall not be used in automotiveapplications Atmel products are not intended authorized or warranted for use as components in applications intended to support or sustain life

SAFETY-CRITICAL MILITARY AND AUTOMOTIVE APPLICATIONS DISCLAIMER Atmel products are not designed for and will not be used in connection with anyapplications where the failure of such products would reasonably be expected to result in significant personal injury or death (ldquoSafety-Critical Applicationsrdquo) withoutan Atmel officers specific written consent Safety-Critical Applications include without limitation life support devices and systems equipment or systems for theoperation of nuclear facilities and weapons systems Atmel products are not designed nor intended for use in military or aerospace applications or environmentsunless specifically designated by Atmel as military-grade Atmel products are not designed nor intended for use in automotive applications unless specificallydesignated by Atmel as automotive-grade

  • Introduction
  • Table of Contents
  • 1 Software License
  • 2 Prerequisites
  • 3 Module Overview
    • 31 Encryption and Decryption
    • 32 Hardware Countermeasures
    • 33 Galois Counter Mode (GCM)
      • 4 Special Considerations
      • 5 Extra Information
      • 6 Examples
      • 7 API Overview
        • 71 Variable and Type Definitions
          • 711 Type aes_callback_t
            • 72 Structure Definitions
              • 721 Struct aes_config
                • 73 Macro Definitions
                  • 731 Module Status Flags
                    • 7311 Macro AES_ENCRYPTION_COMPLETE
                    • 7312 Macro AES_GF_MULTI_COMPLETE
                        • 74 Function Definitions
                          • 741 Configuration and Initialization
                            • 7411 Function aes_get_config_defaults()
                            • 7412 Function aes_set_config()
                            • 7413 Function aes_init()
                              • 742 Start Enable and Write
                                • 7421 Function aes_start()
                                • 7422 Function aes_set_new_message()
                                • 7423 Function aes_clear_new_message()
                                • 7424 Function aes_enable()
                                • 7425 Function aes_disable()
                                • 7426 Function aes_write_key()
                                • 7427 Function aes_write_init_vector()
                                • 7428 Function aes_write_input_data()
                                • 7429 Function aes_read_output_data()
                                • 74210 Function aes_write_random_seed()
                                  • 743 Status Management
                                    • 7431 Function aes_get_status()
                                    • 7432 Function aes_clear_status()
                                      • 744 Galois Counter Mode
                                        • 7441 Function aes_gcm_read_ghash()
                                        • 7442 Function aes_gcm_write_ghash()
                                        • 7443 Function aes_gcm_read_hash_key()
                                        • 7444 Function aes_gcm_write_hash_key()
                                        • 7445 Function aes_gcm_read_cipher_len()
                                        • 7446 Function aes_gcm_write_cipher_len()
                                        • 7447 Function aes_gcm_set_end_message_status()
                                        • 7448 Function aes_gcm_clear_end_message_status()
                                        • 7449 Function aes_gcm_set_gf_multiplication()
                                          • 745 Callback Configuration and Initialization
                                            • 7451 Function aes_register_callback()
                                            • 7452 Function aes_unregister_callback()
                                              • 746 Callback Enabling and Disabling
                                                • 7461 Function aes_enable_callback()
                                                • 7462 Function aes_disable_callback()
                                                    • 75 Enumeration Definitions
                                                      • 751 Enum aes_callback_type
                                                      • 752 Enum aes_cfb_size
                                                      • 753 Enum aes_countermeature_type
                                                      • 754 Enum aes_encrypt_mode
                                                      • 755 Enum aes_key_size
                                                      • 756 Enum aes_operation_mode
                                                      • 757 Enum aes_start_mode
                                                          • 8 Extra Information for Advanced Encryption Standard
                                                            • 81 Acronyms
                                                            • 82 Dependencies
                                                            • 83 Errata
                                                            • 84 Module History
                                                              • 9 Examples for Advanced Encryption Standard
                                                                • 91 Quick Start Guide for AES - Basic
                                                                  • 911 Quick Start
                                                                    • 9111 Prerequisites
                                                                    • 9112 Code
                                                                    • 9113 Workflow
                                                                      • 912 Use Case
                                                                        • 9121 Code
                                                                        • 9122 Workflow
                                                                            • 92 Quick Start Guide for AES - Callback
                                                                              • 921 Quick Start Callback
                                                                                • 9211 Prerequisites
                                                                                • 9212 Code
                                                                                • 9213 Workflow
                                                                                  • 922 Use Case
                                                                                    • 9221 Code
                                                                                    • 9222 Workflow
                                                                                        • 93 Quick Start Guide for AES - DMA
                                                                                          • 931 Quick Start
                                                                                            • 9311 Prerequisites
                                                                                            • 9312 Code
                                                                                            • 9313 Workflow
                                                                                              • 932 Use Case
                                                                                                • 9321 Code
                                                                                                • 9322 Workflow
                                                                                                  • 10 Document Revision History