26
Working effectively with Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer

Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

  • Upload
    others

  • View
    47

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Working effectively with

Arm Cortex-M TrustZone

Kyota Yokoo, Field Application Engineer

Page 2: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Agenda

• Introduction of Arm v8-M MCU

• Overview of Arm v8-M TrustZone

• Getting started with IAR Embedded Workbench for Arm

and TrustZone

• Demo

Page 3: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Introduction of Arm v8-M MCU

Page 4: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Arm Cortex-M Lineup

Cortex-M3

Cortex-M4

Cortex-M0/M0+

Cortex-M7

Cortex-M23

Cortex-M33

v6-M v7-M v8-M

Page 5: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

What is new about v8-M

• The keyword is “Security”

• Still 32-bit architecture

• Stack Limitation

– Now, Stack overflow can enter Fault handler

• Improved MPU(Memory Protection Unit)

– Now, MPU Range selection is flexible

• TrustZone for Cortex-M!

Page 6: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Overview of Arm v8-M TrustZone

Page 7: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

TrustZone adds Secure state

• In addition to privilege level, security state is also added.

Thread mode

Privileged/Unprivileged

Handler mode

Privilege

Secure State

Thread mode

Privileged/Unprivileged

Handler mode

Privilege

Thread mode

Privileged/Unprivileged

Handler mode

Privilege

Non-Secure State

Armv7-M(Cortex-M3/M4) Armv8-M TrustZone

Page 8: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Memory-map with TrustZone

• Security attributes are mapped to regions

Secure

Non-Secure

Secure

Non-Secure

Secure

Non-Secure

Flash

SRAM

Peripherals

Privileged

Unprivileged

Privileged

Unprivileged

Privileged

Unprivileged

Similar concept to v7-M MPU

Page 9: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

How to configure Secure regions?

• Use SAU and/or IDAU

Address

SAU *Option

(Security Attribute Unit)

IDAU *Option

(Implementation Defined Attribute Unit)

Com

pare

Address

Security

4 Types of v8-M Devices - / - SAU / - - / IDAU SAU /

IDAU

Page 10: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Switching Secure states

• Using function call to switch secure states

Secure State Non-Secure State

Thread mode

Privileged/Unprivileged

Handler mode

Privilege

Thread mode

Privileged/Unprivileged

Handler mode

Privilege

NSCNon Secure

Callable

SG

SG

Page 11: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Some more information

• Function call from Secure State to Non-Secure state

should also be handled with special keywords

• Programs will always start from Secure state

• SAU can be configured only with Secure State

Page 12: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Getting started with IAR Embedded

Workbench for Arm and TrustZone

Page 13: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Guideline for security design

• Make the “Secure code” smaller

• Reduce the number of call for “Secure code”

• Try not to let “Secure code” talk to the external world

More code, More risks

Non-Secure StateSecure State NSC

Page 14: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Workflow for TrustZone

Secure state Non-Secure state

Create a project for Secure state Create a project for Non-Secure state

Set project options for Secure state

Configure SAU / IDAU

Implement NSC functions

Generates code and NSC library

Set project options for Non-Secure state

Import NSC library

Implement calls for Secure codeImplement calls for Non-Secure code

Page 15: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Project Options for Secure State

Secure state

This enables to generate secure codes

Page 16: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Configure SAU / IDAU

SAU *example IDAU *example (SAML11)

#define SAU_CTRL (*((volatile unsigned int *) 0xE000EDD0))

#define SAU_RNR (*((volatile unsigned int *) 0xE000EDD8))

#define SAU_RBAR (*((volatile unsigned int *) 0xE000EDDC))

#define SAU_RLAR (*((volatile unsigned int *) 0xE000EDE0))

static void SAU_setup(void)

{

/* region #0: non-secure callable, 0x000000C0 - 0x000000DF */

SAU_RNR = 0;

SAU_RBAR = 0x000000C0;

SAU_RLAR = 0x000000C3;

/* region #1: non-secure, 0x00200000 - 0x003fffff */

SAU_RNR = 1;

SAU_RBAR = 0x00200000;

SAU_RLAR = 0x003fffe1;

/* region #2: non-secure, 0x20200000 - 0x203fffff */

SAU_RNR = 2;

SAU_RBAR = 0x20200000;

SAU_RLAR = 0x203fffe1;

/* Enable SAU */

SAU_CTRL = 1;

}

IDAU and its configurations are vary

between device families.

Page 17: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Implement calls for Non-Secure code

#define NON_SECURE_START (0x00000C00ul) // Start address of Non-Secure State

typedef __cmse_nonsecure_call void (*NonSecure_fpVoid) (void);

int main(void)

{

NonSecure_fpVoid NonSecure_ResetHandler;

NonSecure_ResetHandler = (NonSecure_fpVoid)(*((uint32_t *)(NON_SECURE_START + 4u)));

NonSecure_ResetHandler(); // jump to Reset Handler of Non Secure state

}

main_s.c

• Declare function pointers with cmse_nonsecure_call”

BLXNS instruction will be generated to switch states

Page 18: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Implement NSC Functions

Secure_Functions.h#define CMSE_NS_CALL __cmse_nonsecure_call

#define CMSE_NS_ENTRY __cmse_nonsecure_entry

CMSE_NS_ENTRY uint32_t secure_toupper(uint32_t i);

#include “Secure_Functions.h”

CMSE_NS_ENTRY uint32_t secure_toupper(uint32_t i)

{

return( toupper( i ) );

}

Secure_Functions.c

define symbol NSC_start = 0x00000800;

define symbol NSC_end = 0x000008FF;

define region NSC_region =

mem:[from NSC_start to NSC_end];

place in NSC_region { section Veneer$$CMSE };

Linker Configuration file

SG instructions for the functions will be generated in NSC region

• Declare and Define the function with “__cmse_nonsecure_entry”

Page 19: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Generates code and NSC library

Non-Secure project will link the library

• “Make” will generate execution file and NSC library

Page 20: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Project Options for Non-Secure State

Non-Secure state

Page 21: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Imports NSC library

• Link the import library generated in Secure project

Page 22: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Implement calls for Secure code

#include “Secure_Functions.h”

void main(void)

{

char t;

while(1){

t = secure_toupper(s);

}

}

main_ns.c

• Include the header file for the secure function declaration*The file should be shared with Secure and Non-Secure projects

main_ns.c (Non-Secure State)

import_lib.o (NSC)

Secure_Functions.c (Secure State)

Page 23: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Demonstration

Page 24: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Demo settings and program

I-jet

UART

Reset Handler

Reset Handler

Initialize

Jump to “Non-Secure”

Initialize

Receive from UART

Call secure code

ToUpper()

Send to UARTMicrochip SAML11(Cortex-M23: - / IDAU)

Page 25: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Summary

• The keyword of v8-M is security, especially TrustZone

• To use TrustZone, you should create and configure two

projects for Secure and Non-Secure

• IAR Embedded Workbench provides a support for

Secure and Non-Secure code communicates easily

Page 26: Working effectively with Arm Cortex-M TrustZone · 2019-03-06 · Arm Cortex-M TrustZone Kyota Yokoo, Field Application Engineer. Agenda • Introduction of Arm v8-M MCU ... • Programs

Thank you for your attention!

www.iar.com