cec1302 crypto api user's guide - microchip...

55
2016 Microchip Technology Inc. DS50002504A CEC1302 Crypto API User’s Guide

Upload: lamthien

Post on 19-May-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

2016 Microchip Technology Inc. DS50002504A

CEC1302 Crypto API

User’s Guide

Page 2: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

2016 Microchip Technology Inc. DS50002504A-page 2

Information contained in this publication regarding device applications and the like is provided only for your convenience and may besuperseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NOREPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OROTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Micro-chip devices in life support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend, indemnify and holdharmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly orotherwise, under any Microchip intellectual property rights unless otherwise stated.

Trademarks

The Microchip name and logo, the Microchip logo, AnyRate, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq, KeeLoq logo, Kleer, LANCheck, LINK MD, MediaLB, MOST, MOST logo, MPLAB, OptoLyzer, PIC, PICSTART, PIC32 logo, RightTouch, SpyNIC, SST, SST Logo, SuperFlash and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

ClockWorks, The Embedded Control Solutions Company, ETHERSYNCH, Hyper Speed Control, HyperLight Load, IntelliMOS, mTouch, Precision Edge, and QUIET-WIRE are registered trademarks of Microchip Technology Incorporated in the U.S.A.

Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom, chipKIT, chipKIT logo, CodeGuard, dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, MiWi, motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, RightTouch logo, REAL ICE, Ripple Blocker, Serial Quad I/O, SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.

Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.

GestIC is a registered trademarks of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries.

All other trademarks mentioned herein are property of their respective companies.

© 2016, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.

ISBN: 9781522405658

Note the following details of the code protection feature on Microchip devices:

• Microchip products meet the specification contained in their particular Microchip Data Sheet.

• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions.

• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property.

• Microchip is willing to work with the customer who is concerned about the integrity of their code.

• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as “unbreakable.”

Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of ourproducts. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such actsallow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California and India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified.

QUALITYMANAGEMENTSYSTEMCERTIFIEDBYDNV

== ISO/TS16949==

Page 3: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 CRYPTO API USER’S GUIDE

Table of Contents

Chapter 1. Introduction1.1 Purpose .......................................................................................................... 81.2 Scope ............................................................................................................ 81.3 References ..................................................................................................... 81.4 Glossary of Terms and Acronyms .................................................................. 8

Chapter 2. Overall Description2.1 Product Perspective ....................................................................................... 92.2 Product Functions .......................................................................................... 92.3 User Classes and Characteristics .................................................................. 92.4 USER DOCUMENTATION ............................................................................. 92.5 DESIGN AND IMPLEMENTATION CONSTRAINTS ..................................... 92.6 Assumptions and Dependencies .................................................................... 92.7 OPERATING ENVIRONMENT ....................................................................... 9

Chapter 3. External Interface Requirements3.1 User Interfaces ............................................................................................. 103.2 Hardware Interfaces ..................................................................................... 103.3 SOFTWARE INTERFACES ......................................................................... 10

Chapter 4. API Usage4.1 SHA APIs ..................................................................................................... 114.2 SHA12 APIs ................................................................................................. 114.3 RSA APIs ..................................................................................................... 114.4 AES .............................................................................................................. 124.5 Random number generator .......................................................................... 13

Chapter 4. APIs4.1 AES Functions .............................................................................................. 15

4.1.1 aes_hash_power ....................................................................................... 154.1.2 aes_hash_reset ......................................................................................... 154.1.3 aes_busy ................................................................................................... 164.1.4 aes_status ................................................................................................. 164.1.5 aes_done_status ....................................................................................... 164.1.6 aes_stop .................................................................................................... 174.1.7 aes_start .................................................................................................... 174.1.8 aes_iclr ...................................................................................................... 174.1.9 aes_set_key .............................................................................................. 184.1.10 aes_crypt ................................................................................................. 19

4.2 RSA Functions ............................................................................................. 204.2.1 rsa_load_key ............................................................................................. 204.2.2 rsa_load_crt_params ................................................................................. 224.2.3 rsa_keygen ................................................................................................ 23

2016 Microchip Technology Inc. DS50002504A-page 3

Page 4: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

4.2.4 rsa_modular_exp .......................................................................................244.2.5 rsa_encrypt ................................................................................................254.2.6 rsa_decrypt ...............................................................................................264.2.7 rsa_crt_gen_params ..................................................................................274.2.8 rsa_crt_decrypt ..........................................................................................284.2.9 rsa_signature_gen .....................................................................................294.2.10 rsa_signature_verify .................................................................................30

4.3 PKE Functions .............................................................................................. 314.3.1 pke_power .................................................................................................314.3.2 pke_reset ...................................................................................................314.3.3 pke_status ..................................................................................................314.3.4 pke_done_status ........................................................................................324.3.5 pke_start ....................................................................................................324.3.6 pke_ists_clear ............................................................................................334.3.7 pke_busy ....................................................................................................334.3.8 pke_set_operand_slot ................................................................................334.3.9 pke_get_operand_slot ...............................................................................344.3.10 pke_set_operand_slots ............................................................................344.3.11 pke_get_slot_addr ...................................................................................354.3.12 pke_fill_slot ..............................................................................................354.3.13 pke_clear_scm .........................................................................................354.3.14 pke_scm_clear_slot .................................................................................364.3.15 pke_read_scm .........................................................................................364.3.16 pke_write_scm .........................................................................................37

4.4 RNG Functions ............................................................................................. 384.4.1 rng_power ..................................................................................................384.4.2 rng_reset: ...................................................................................................384.4.3 rng_mode ...................................................................................................384.4.4 rng_is_on ...................................................................................................394.4.5 rng_start .....................................................................................................394.4.6 rng_stop .....................................................................................................394.4.7 rng_get_fifo_level .......................................................................................404.4.8 rng_get_bytes ............................................................................................404.4.9 rng_get_words ...........................................................................................41

4.5 HASH Functions ........................................................................................... 424.5.1 hash_status ................................................................................................424.5.2 hash_busy ..................................................................................................424.5.3 hash_start ..................................................................................................424.5.4 hash_done_status ......................................................................................43

4.6 SHA Functions ............................................................................................. 444.6.1 sha12_init ...................................................................................................444.6.2 sha12_update ............................................................................................454.6.3 sha12_finalize ............................................................................................464.6.4 sha256_pad_fill ..........................................................................................474.6.5 sha256_raw ...............................................................................................474.6.6 sha256_raw_init .........................................................................................484.6.7 sha256_raw_update ..................................................................................484.6.8 hash_iclr .....................................................................................................494.6.9 sha_init .......................................................................................................494.6.10 sha_update ..............................................................................................50

DS50002504A-page 4 2016 Microchip Technology Inc.

Page 5: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

4.6.11 sha_final .................................................................................................. 51

Chapter 6. PKE Slot Usage

Chapter 7. Timing Analysis7.1 AES ................................................................................................................ 37.2 PKE ................................................................................................................ 37.3 SHA ................................................................................................................ 37.4 RNG ............................................................................................................... 3

Worldwide Sales and Service ...................................................................................... 4

2016 Microchip Technology Inc. DS50002504A-page 5

Page 6: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 CRYPTO API USER’S GUIDE

Preface

INTRODUCTION

This chapter contains general information that will be useful to know before using the CEC1302 Crypto API. Items discussed in this chapter include:

• Document Layout

• Conventions Used in this Guide

• The Microchip Web Site

• Development Systems Customer Change Notification Service

• Customer Support

• Document Revision History

DOCUMENT LAYOUT

This document describes how to use the CEC1302 Crypto API as a development tool with the CEC1302. The manual layout is as follows:

• Chapter 1. “Introduction” – Explains purpose and scope of this guide.

• Chapter 2. “Overall Description” – Includes instructions on how to get started with the CEC1302 Crypto API.

• Chapter 3. “External Interface Requirements” – Provides external interface requirements for the CEC1302 Crypto API.

• Chapter 4. “API Usage” - This section lists the APIs available and their usage.

• Chapter 5. “APIs” - Describes the user interface to the Crypto Library.

• Chapter 6. “PKE Slot Usage” - Contains a table that lists the usage of the slots for various operations.

• Chapter 7. “Timing Analysis” - Explains timing measurement.

NOTICE TO CUSTOMERS

All documentation becomes dated, and this manual is no exception. Microchip tools and documentation are constantly evolving to meet customer needs, so some actual dialogs and/or tool descriptions may differ from those in this document. Please refer to our web site (www.microchip.com) to obtain the latest documentation available.

Documents are identified with a “DS” number. This number is located on the bottom of each page, in front of the page number. The numbering convention for the DS number is “DSXXXXXA”, where “XXXXX” is the document number and “A” is the revision level of the document.

For the most up-to-date information on development tools, see the MPLAB® IDE online help. Select the Help menu, and then Topics to open a list of available online help files.

2016 Microchip Technology Inc. DS50002504A-page 6

Page 7: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

Preface

CONVENTIONS USED IN THIS GUIDE

This manual uses the following documentation conventions:

DOCUMENTATION CONVENTIONS

Description Represents Examples

Arial font:

Italic characters Referenced books MPLAB® IDE User’s Guide

Emphasized text ...is the only compiler...

Initial caps A window the Output window

A dialog the Settings dialog

A menu selection select Enable Programmer

Quotes A field name in a window or dialog

“Save project before build”

Underlined, italic text with right angle bracket

A menu path File>Save

Bold characters A dialog button Click OK

A tab Click the Power tab

N‘Rnnnn A number in verilog format, where N is the total number of digits, R is the radix and n is a digit.

4‘b0010, 2‘hF1

Text in angle brackets < > A key on the keyboard Press <Enter>, <F1>

Courier New font:

Plain Courier New Sample source code #define START

Filenames autoexec.bat

File paths c:\mcc18\h

Keywords _asm, _endasm, static

Command-line options -Opa+, -Opa-

Bit values 0, 1

Constants 0xFF, ‘A’

Italic Courier New A variable argument file.o, where file can be any valid filename

Square brackets [ ] Optional arguments mcc18 [options] file [options]

Curly brackets and pipe character: { | }

Choice of mutually exclusive arguments; an OR selection

errorlevel {0|1}

Ellipses... Replaces repeated text var_name [, var_name...]

Represents code supplied by user

void main (void){ ...}

2016 Microchip Technology Inc. DS50002504A-page 7

Page 8: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

THE MICROCHIP WEB SITE

Microchip provides online support via our web site at www.microchip.com. This web site is used as a means to make files and information easily available to customers. Accessible by using your favorite Internet browser, the web site contains the following information:

• Product Support – Data sheets and errata, application notes and sample programs, design resources, user’s guides and hardware support documents, latest software releases and archived software

• General Technical Support – Frequently Asked Questions (FAQs), technical support requests, online discussion groups, Microchip consultant program member listing

• Business of Microchip – Product selector and ordering guides, latest Microchip press releases, listing of seminars and events, listings of Microchip sales offices, distributors and factory representatives

DEVELOPMENT SYSTEMS CUSTOMER CHANGE NOTIFICATION SERVICE

Microchip’s customer notification service helps keep customers current on Microchip products. Subscribers will receive e-mail notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest.

To register, access the Microchip web site at www.microchip.com, click on Customer Change Notification and follow the registration instructions.

The Development Systems product group categories are:• Compilers – The latest information on Microchip C compilers, assemblers, linkers

and other language tools. These include all MPLAB C compilers; all MPLAB assemblers (including MPASM assembler); all MPLAB linkers (including MPLINK object linker); and all MPLAB librarians (including MPLIB object librarian).

• Emulators – The latest information on Microchip in-circuit emulators.This includes the MPLAB REAL ICE and MPLAB ICE 2000 in-circuit emulators.

• In-Circuit Debuggers – The latest information on the Microchip in-circuit debuggers. This includes MPLAB ICD 3 in-circuit debuggers and PICkit 3 debug express.

• MPLAB IDE – The latest information on Microchip MPLAB IDE, the Windows Integrated Development Environment for development systems tools. This list is focused on the MPLAB IDE, MPLAB IDE Project Manager, MPLAB Editor and MPLAB SIM simulator, as well as general editing and debugging features.

• Programmers – The latest information on Microchip programmers. These include production programmers such as MPLAB REAL ICE in-circuit emulator, MPLAB ICD 3 in-circuit debugger and MPLAB PM3 device programmers. Also included are nonproduction development programmers such as PICSTART Plus and PIC-kit 2 and 3.

DS50002504A-page 8 2016 Microchip Technology Inc.

Page 9: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

Preface

CUSTOMER SUPPORT

Users of Microchip products can receive assistance through several channels:

• Distributor or Representative

• Local Sales Office

• Field Application Engineer (FAE)

• Technical Support

Customers should contact their distributor, representative or field application engineer (FAE) for support. Local sales offices are also available to help customers. A listing of sales offices and locations is included in the back of this document.

Technical support is available through the web site at: http://www.microchip.com/support

DOCUMENT REVISION HISTORY

Revision Section/Figure/Entry Correction

DS50002504A (05-1016) Initial document release

2016 Microchip Technology Inc. DS50002504A-page 9

Page 10: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 1. Introduction

1.1 PURPOSE

This document illustrates the usage of Crypto APIs available in the CEC1302.

1.2 SCOPE

This document will serve as a usage manual for the functions provided by the functions in the library. It presents the reader with the function header, a description of the func-tion’s operations and its input and output parameters. It also details the pseudo code and steps to use the APIs provided in the CEC1302 Crypto Lib.

1.3 REFERENCES

1.4 GLOSSARY OF TERMS AND ACRONYMS

SPI – Serial Peripheral Interface

AES – Advanced encryption Standard

RSA - Rivest-Shamir-Adleman cryptosystem

PKE – Public Key Encryption

SHA – Secure Hash Algorithm

RNG – Random Number Generator

SCM – Shared Crypto Memory

CRT – Chinese Remainder Theorem

2016 Microchip Technology Inc. DS50002504A-page 10

Page 11: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 2. Overall Description

2.1 PRODUCT PERSPECTIVE

The Crypto API allows software access to certain hardware features that facilitate easy development of applications on the CEC1302 platform. These APIs serve the function of providing easy access to the underlying hardware features.

2.2 PRODUCT FUNCTIONS

The Crypto APIs provide software access to features like access to SPI/FLASH, AES encryption, RSA Crypt engine and Public Key Encryption. All operations are abstracted by the Application Programming Interfaces and the programmer need only use the APIs to leverage these device specific operations.

2.3 USER CLASSES AND CHARACTERISTICS

This document is intended for programmers and users of the CEC1302 product. It illus-trates the use of certain software features that would facilitate easy development of applications.

2.4 USER DOCUMENTATION

None

2.5 DESIGN AND IMPLEMENTATION CONSTRAINTS

Many of the APIs require buffers and memory to be specified. The onus of maintaining the proper buffers and memory is on the caller of the APIs.User Documentation

2.6 ASSUMPTIONS AND DEPENDENCIES

The efficacy of this user manual may be contingent upon the knowledge of the CEC1302 target hardware. Certain references in this document may require the usage of device data sheets.

2.7 OPERATING ENVIRONMENT

The pseudo code provided is OS independent and can only be used with the CEC1302.

2016 Microchip Technology Inc. DS50002504A-page 11

Page 12: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 3. External Interface Requirements

3.1 USER INTERFACES

This document will describe the user interface to the Crypto Library.

3.2 HARDWARE INTERFACES

Refer to the CEC1302 Data Sheet.

3.3 SOFTWARE INTERFACES

Keil Compiler IDE-Version:

μVision V5.15

Tool Version Numbers:

Toolchain: MDK-ARM Standard Cortex-M Version: 5.15.0

Toolchain Path: C:\Keil_v5\ARM\ARMCC\Bin

C Compiler: Armcc.exe V5.05 update 2 (build 169)

Assembler: Armasm.exe V5.05 update 2 (build 169)

Linker/Locator: ArmLink.exe V5.05 update 2 (build 169)

Library Manager: ArmAr.exe V5.05 update 2 (build 169)

Hex Converter: FromElf.exe V5.05 update 2 (build 169)

CPU DLL: SARMCM3.DLL V5.15.0

Dialog DLL: DCM.DLL V1.13.2.0

Target DLL: ULP2CM3.DLL V2.200.17.0

Dialog DLL: TCM.DLL V1.14.5.0

2016 Microchip Technology Inc. DS50002504A-page 12

Page 13: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 4. API Usage

The following section lists the APIs available and their usage.

4.1 SHA APIS

1. Check if Hash Block is busy, if not busy proceed. If busy, wait until busy status is false (rom_hash_busy())

2. Run sha_init with the required mode (1,256) and the pointer to the buffer where the digest will be stored. The buffer must be 4 byte aligned for SHA1 and SHA256.

3. Run sha_update with a buffer pointer to message on which digest is to be calcu-lated. The message must align with the block size requirement for the mode being calculated (64 byte block for SHA1 and SHA256). The number of blocks of data provided must be mentioned.

4. If start hash block was not specified in the flags in the previous call, call hash_start() to start the Hash engine.

5. Wait until hash operation is complete (hash_busy())

6. Run sha_final with a buffer of size at least one block and message length.

7. Wait until hash operation is complete (hash_busy()).

8. The digest calculated will be in the buffer specified in step 2.

4.2 SHA12 APIS

1. Check if Hash Block is busy, if not busy proceed. If busy, wait until busy status is false (hash_busy())

2. Call sha12_init with the SHA12_CONTEXT_T data structure and the mode required. SHA12 APIs only support SHA1 and SHA256.

3. Call sha12_update with the data structure, the message for which digest is to be calculated and the length of the message. The input need not adhere to block size requirement.

4. Wait on hash busy status (hash_busy()). Once hash block is free, proceed.

5. Call sha12_finalize with the data structure provided previously.

6. Wait for Hash operation to complete (hash_busy()).

7. The calculated digest will be in the data structure provided (sha12_ctx.hash.b or sha12_ctx.hash.w).

2016 Microchip Technology Inc. DS50002504A-page 13

Page 14: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

4.3 RSA APIS

1. Power on PKE block with pke_power().

2. Call the rsa_load_key() to load Public-Private Key pairs into the PKE engine. Specify the RSA bite length (1024, 2048) and the byte order of data provided. 4 combinations are possible according to which keys may be loaded. The slot numbers are handled by the api. Keys can be explicitly programmed with pke_write_scm() calls.

a) RSA Encryption with Public Key

i. Pointer to private exponent = Not usedii. Pointer to public modulus = your public key modulus -> Slot 0iii.Pointer to public exponent = your public key exponent -> iv.Slot 8

b) RSA Decryption with Private Key

i. Pointer to private modulus = your private key modulus -> Slot 6

ii. Pointer to public modulus = your public key modulus -> Slot 0iii.Pointer to public exponent = your public key exponent -> Slot

8c) RSA Encryption with Private Key

i. Pointer to private exponent = Not usedii. Pointer to public modulus = your public key modulus -> Slot 0iii.Pointer to public exponent = your private key exponent ->

Slot 8d) RSA Decryption with Public Key

i. Pointer to private exponent = your private exponent -> Slot 6ii. Pointer to public modulus = your public modulus -> Slot 0iii.Pointer to public exponent = Not used

3. If data is to be encrypted, call rsa_encrypt() with the rsa bit len (1024, 2048), input data, byte length of input data. Pke engine can be started by specifying the appropriate flag parameter. If not started, the pke engine may be explicitly started with pke_start().

4. Wait for PKE engine to complete operation pke_busy()). Once complete, the encrypted message can be found in slot 5 of crypto memory. The data can be read into a local buffer with pke_read_scm().

5. If data is to be decrypted, call rsa_decrypt() with rsa bit len (1024, 2048), input data, byte length of input data. Pke engine can be started by specifying the appropriate flag parameter. If not started, the pke engine may be explicitly started with pke_start().

6. Wait for PKE engine to complete operation pke_busy()). Once complete, the decrypted data will be in slot 5 of Shared Crypto memory. This data can be read into a local buffer with pke_read_scm().

DS50002504A-page 14 2016 Microchip Technology Inc.

Page 15: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

API Usage

4.4 AES

1. Power on AES block with aes_hash_power ().

2. Reset the AES hash block with aes_hash_reset().

3. Check for AES block busy with aes_busy():If not busy perform the following

4. Set AES Private key LSB first random generated and optional initialization vector LSB first, also specify the AES key length used using api aes_set_key()

5. Check for AES status with rom API aes_status()

6. clear status for any leftover status bits if any using API aes_iclr()

7. Call API aes_crypt() for encryption or decryption providing the message should be aligned input data buffer and pointer buffer to load aligned output data buffer and mode of operation ; supported mode ECB CBC CTR CFB OFB

8. Start the AES operation to be performed by calling function aes_start()

9. Wait for the done status by calling API aes_done_status()

10. Once done the operated data output will be in the buffer provided via API aes_crypt()

11. Stop AES block using the API aes_stop()

12. For power saving and having AES block to sleep state using the routine aes_hash_power(false);

4.5 RANDOM NUMBER GENERATOR

1. Power on RND HW block with API rng_power (true).

2. Reset the RND HW block with rng_reset()

3. Two modes of random number are generated asynchronous/true random mode and Non-zero(pseudo-random mode)

4. For random number generation use the function call as follows

5. Select the mode of operation by calling rng_mode(mode)

a) 0 – asynchronous

b) 1 - pseudo-random mode

6. Start the HW block run state by calling function rng_start();

7. Wait for operation completion by polling rng_get_fifo_level() for data in the inter-nal buffer

8. Once completed the internal buffer will have 1Kbits of random data use API

a) rng_get_bytes() number of random bytes to retrieve. Must be less or equal to the size of the buffer or

b) rng_get_words() Reads the FIFO level register and return the number of 32-bit words of random data currently in the FIFO – max value supported in 1024 bits

2016 Microchip Technology Inc. DS50002504A-page 15

Page 16: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 5. APIs

5.1 AES FUNCTIONS

The document will describe the user interface to the Crypto Library.

5.1.1 aes_hash_power

Function Header:

Void aes_hash_power(uint8_t pwr_state);

Description:

This function is used to enable or disable AES and Hash Hardware Block.

Inputs:

Outputs: None

5.1.2 aes_hash_reset

Function Header:

Void aes_hash_reset(void);

Description:

This function is used to reset the AES and Hash block.

Inputs: None

Outputs: None

Note: AES and Hash hardware accelerators do not implement a block level clock gate control and also share AHB resources (master, internal DMA, clock-ing). A single PCR sleep control will sleep both blocks. Before setting the PCR sleep bit, clear AES and Hash Control registers to stop both engines. To wake (ungated clocks) clear the PCR sleep enable bit.

Input Parameter Description

Pwr_state An unsigned 8-bit integer specifying the power state. pwr_state

· Non-zero = ungate clocks to block· 0 gate clocks to block.

2016 Microchip Technology Inc. DS50002504A-page 16

Page 17: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.1.3 aes_busy

Function Header:

Bool aes_busy(void);

Description:

This function is used to check if the AES block is running. “true” is returned if the block is running and “false” is returned if the block is not running.

Inputs: None

Outputs:

True if AES block is running, False otherwise.

5.1.4 aes_status

Function Header:

uint32_t aes_status(void);

Description:

This function is used to read the status of the AES block.

Inputs: none

Outputs:

The status of the AES block is reflected in the return value. The bit definitions of AES Status value is presented below.

5.1.5 aes_done_status

Function Header:

Bool aes_done_status(uint32_t * status_value);

Description:

The function updates the status value of the AES block in the pointer argument. The done status is evaluated and it is returned as a Boolean value.

Inputs:

Outputs:

The status of the AES is updated in the pointer argument.

The return value is TRUE is done status is set, FALSE otherwise.

Bit Number Definition (all bits are read only)

0 AES Busy Status (1 if busy, 0 otherwise)

1 AES CCM Mode MAC_T Calculation is valid (1 if valid, 0 otherwise; Not Supported)

2 DMA Error. (1 if error, indicates that the AES DMA Master received AHB Bus error).

Input Parameter Description

Status_value An unsigned 32 bit integer pointer where the status value will be stored. Refer aes_status for bit definition of status register.

2016 Microchip Technology Inc. DS50002504A-page 17

Page 18: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.1.6 aes_stop

Function Header:

Bool aes_stop(void);

Description:

This function is used to stop the AES block. The function accesses the AES control reg-ister to stop the AES Operations.

Inputs: None

Outputs:

The busy status of AES block is returned as a Boolean value.

5.1.7 aes_start

Function Header:

Bool aes_start(bool ien);

Description:

This function is used to start the AES Block. The input argument controls interrupt enable

Inputs:

Outputs:

The busy status of the AES block is returned as a Boolean value.

5.1.8 aes_iclr

Function Header:

Uint32_t aes_iclr(void);

Description:

The function is used to clear Hash interrupts. The function will return the status of AES Block. AES Status register is a read-to-clear register. If it is zero, no status is set.

Inputs: None

Outputs:

The AES Status will be reflected in the unsigned 32 bit return value. Refer aes_status for bit definitions.

Input Parameter Description

Ien Boolean value to specifying the interrupt status. This value, if true enables interrupt and if false, disables interrupt.

DS50002504A-page 18 2016 Microchip Technology Inc.

Page 19: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.1.9 aes_set_key

Function Header:

Uint8_t aes_set_key(const uint32_t *pkey,const uint32_t *piv,uint8_t key_len,bool msbf);

Description:

The function programs the AES accelerator with key and optional initialization. AES key size and pre-calculation mode are set. The AES engine is not started. Do not call this routine if the AES engine is busy.

Inputs:

Outputs:

AES_OK (Success), AES_ERR_BAD_POINTER (pkey is NULL), AES_ER-R_BAD_KEY_LEN (key len is not supported).

Input Parameter Description

Pkey Pointer to a word (32-bit) aligned buffer containing the AES key, LSB first

Piv Pointer to a word (32-bit) aligned buffer containing AES initialization vector, LSB first. NULL if no Initialization vector is required.

Key_len A uint8_t indicating the key length. The permitted values are · AES_KEYLEN_128· AES_KEYLEN_192· AES_KEYLEN_256.

Msbf A Boolean value indicating most significant bit first.

2016 Microchip Technology Inc. DS50002504A-page 19

Page 20: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.1.10 aes_crypt

Function Header:

Uint8_t aes_crypt (const uint32 *data_in, uint32_t *data_out,uint32_t num_128bit_blocks,uint8_t modes);

Description:

This function programs specified AES operation using currently programmed key. The hardware permits the following modes of operation; ECB, CBC, CTR and OFB modes.

Inputs:

Outputs:

AES_OK (AES HW Programmed), AES_ERR_BAD_POINTER (NULL pointers or number of blocks is 0 or buffers cross MEC140X DMA boundary).

Input Parameter Description

Data_in A pointer to a word (32-bit) aligned input data buffer.

Data_out A pointer to a word (32-bit) aligned output data buffer.

Num_128bit_blocks Size of input data as a number of 126-bit (16-byte) blocks.

Modes 8 bit integer indicating the mode of operation. The permitted modes of operation are

· AES_MODE_ECB· AES_MODE_CBC· AES_MODE_CTR· AES_MODE_OFB.

DS50002504A-page 20 2016 Microchip Technology Inc.

Page 21: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2 RSA FUNCTIONS

5.2.1 rsa_load_key

Function Header:

Uint8_t rsa_load_key(uint16_t rsa_bit_len,const uint8_t * private_exponent,const uint8_t * public_modulus,const uint8_t * public_exponent,uint16_t public_exponent_byte_len,bool msbf);

Description:

This function loads a given key into the PKE shared crypto memory. If moduli pointers are NULL, no copy operations are performed.

The shared crypto memory of CEC1302 is divided into 31 slots (slot0-slot30) each of 512 bytes.

The following modes of operation are possible:

RSA Encryption with Public Key

- Pointer to private exponent = Not used- Pointer to public modulus = your public key modulus -> - slot 0- Pointer to Public Exponent = your public key exponent -> - slot 8

RSA Decryption with Private Key

- Pointer to private exponent = your private key modulus -> slot 6- Pointer to public modulus = your public key modulus -> - slot 0- Pointer to Public Exponent = your public key exponent -> - slot 8

Alternate

RSA Encryption with Private Key

- Pointer to private exponent = Not used- Pointer to public modulus = your public key modulus -> - slot 0- Pointer to Public Exponent = your private key exponent -> - slot 8

RSA Encryption with Private Key

- Pointer to private exponent = your private exponent -> slot 6- Pointer to public modulus = your public modulus -> - slot 0- Pointer to Public Exponent = not used

2016 Microchip Technology Inc. DS50002504A-page 21

Page 22: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Inputs:

Outputs:

The return values and their description is presented below.

PKE_RET_ERR_BUSY – if the RSA module is busy

PKE_RET_ERR_INVALID_BIT_LENGTH – if the rsa_bit_len parameter has a value that is not permitted.

PKE_RET_OK – if the operation requested was successful.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer reflecting the bit size. The permitted values are 1024, 2048 or 4096 bits

Private_exponent An unsigned 8-bit pointer to a constant data consisting of RSA private exponent of length (rsa_bit_len/8) bytes

Public_modulus An unsigned 8-bit pointer to a constant data consisting of RSA Public Modulus of length (rsa_bit_len/8) bytes

Public_exponent An unsigned 8-bit pointer to a constant data consisting of RSA Public Exponent

Public_exponent_byte_len

An unsigned 16 bit integer indicating the RSA public exponent byte length [1, (rsa_bit_len/8)]

Msbf A Boolean value if true, indicates Most significant byte first. If false, it indicates least significant byte first.

DS50002504A-page 22 2016 Microchip Technology Inc.

Page 23: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2.2 rsa_load_crt_params

Function Header:

uint8_t rsa_load_crt_params ( uint16_t rsa_bit_len,const uint8_t * dp,const uint8_t * dq,const uint8_t * I,bool msbf);

Description:

This routine loads CRT Key parameters. If a parameter pointer is not NULL load it into its SCM slot.

Inputs:

Outputs:

The return values and description is presented below.

PKE_RET_OK if successful

PKE_RET_ERR_BUSY if pke is busy

PKE_RET_ERR_INVALID_BIT_LENGTH if the rsa_bit_len has invalid value.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

Dp An unsigned 8 bit integer pointer to constant data containing RSA key CRT parameter dp of length (rsa_bit_len/8) bytes

Dq An unsigned 8 bit integer pointer to constant data containing RSA key CRT parameter dq of length (rsa_bit_len/8) bytes

I An unsigned 8 bit integer pointer to constant data containing RSA key CRT parameter I of length (rsa_bit_len/8) bytes

Msbf A Boolean value if true, indicates Most significant byte first. If false, it indicates least significant byte first.

2016 Microchip Technology Inc. DS50002504A-page 23

Page 24: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.2.3 rsa_keygen

Function Header:

uint8_t rsa_keygen(uint16_t rsa_bit_len,const uint8_t * p,const uint8_t * q,const uint8_t * e,uint16_t elen,uint8_t flags);

Description:

This routine uses PKE to generate the RSA Private exponent and public modulus given two RSA Compatible prime numbers and the public exponent.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – if the arguments p,q or e points to NULL, this error value is returned.

PKE_RET_ERR_BUSY – this error is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – This error is returned if rsa bit length is invalid.

PKE_RET_OK – requested operation successful.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

P An unsigned 8bit integer pointer to constant data containing RSA prime p of length (rsa_bit_len/8)

Q An unsigned 8bit integer pointer to constant data containing RSA prime q of length (rsa_bit_len/8)

E An unsigned 8 bit integer pointer to constant data containing RSA public exponent

Elen An unsigned 16 bit integer indicating the length of public exponent

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

DS50002504A-page 24 2016 Microchip Technology Inc.

Page 25: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2.4 rsa_modular_exp

Function header:

uint8_t rsa_modular_exp(uint16_t rsa_bit_len,const uint8_t * m,uin16_t mlen,const uint8_t * e,uint16_t elen,uint8_t flags);

Description:This function performs RSA modular Exponentiation C = M^e mod n.

Inputs:

Outputs:

The return values and their descriptions are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if either m or e points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

M An unsigned 8 bit integer pointer to constant data containing the base number

Mlen

E

Elen An unsigned 16 bit integer indicating the byte length of public exponent.

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

2016 Microchip Technology Inc. DS50002504A-page 25

Page 26: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.2.5 rsa_encrypt

Function Header:

uint8_t rsa_encrypt(uint16_t rsa_bit_len,const uint8_t * mesg,uint16_t mlen,uint8_t flags);

Description:

This routine starts the encryption process. It requires the RSA keys to have been pre-viously loaded. If encrypting with public key then, slot 8 must contain the public expo-nent and slot 0 contains the public modulus. If encrypting with a private key then slot 8 must contain the private exponent. Encrypted output is in slot 5. Message length is lim-ited due to PKCS#1 v1.5 (recommended way to pad input and output to/from RSA Algorithm) padding. The maximum message length is (rsa_bit_len/8) – 11.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if Mesg points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

Mesg An unsigned 8bit integer pointer to a constant data containing the message to be encrypted.

Mlen An unsigned 16 bit integer specifying the length of the message to be encrypted.

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

DS50002504A-page 26 2016 Microchip Technology Inc.

Page 27: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2.6 rsa_decrypt

Function Header:

Uint8_t rsa_decrypt (uint16_t rsa_bit_len,const uint8_t * encrypted_mesg,uint16_t mlen,uint8_t flags);

Description:

The routine is used to decrypt messages. It computes M = (encrypted_mesg)^(slot 6). Slot 6 contains either the private or public exponent. If the message was signed with a private key, then slot 6 should contain public exponent. If the message was signed with a public key then slot 6 should contain the private exponent. Switch the order of param-eters in rsa_load_keys() to store the appropriate exponent in slot 6. The decrypted out-put will be in slot 5 and will contain PKCS#1 v1.5 padding.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if Encrypted_mesg points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

Encrypted_mesg An unsigned 8bit integer pointer to a constant data containing the message to be decrypted.

Mlen An unsigned 16 bit integer specifying the length of the message to be encrypted.

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

2016 Microchip Technology Inc. DS50002504A-page 27

Page 28: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.2.7 rsa_crt_gen_params

Function Header:

Uint8_t rsa_crt_gen_params(uint16_t rsa_bit_len,const uint8_t * p,const uint8_t * q,uint8_t flags);

Description:

This routine is used for RSA CRT parameter generation. This routine requires RSA keys to have been previously loaded. Public Modulus must be in slot 0 and public expo-nent in slot 8. Private exponent must be in slot 6. Prime p will be loaded into slot 2 and prime q into slot 3. After the engine is done, the three output parameters are: dp in slot 0xA, dq in slot 0xB and I in slot 0xC.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if either p or q points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

P An unsigned 8 bit integer pointer to a constant data containing prime p

Q An unsigned 8 bit integer pointer to a constant data containing prime q

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

DS50002504A-page 28 2016 Microchip Technology Inc.

Page 29: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2.8 rsa_crt_decrypt

Function Header:

Uint8_t rsa_crt_decrypt(uint16_t rsa_bit_len,const uint8_t * encrypted_mesg,uint16_t mlen,uint8_t flags);

Description:

This routine performs RSA decryption using Chinese remainder theorem. It computes M = (encrypted_mesg) ^ (slot 6). Slot 6 contains wither the private or public exponent. If the message was signed with a private key then slot 6 should contain the public expo-nent. If the message was signed with a public key then slot 6 should c

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if Encrypted_mesg points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits

Encrypted_mesg An unsigned 8bit integer pointer to a constant data containing the message to be decrypted.

Mlen An unsigned 16 bit integer specifying the length of the message to be encrypted.

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

2016 Microchip Technology Inc. DS50002504A-page 29

Page 30: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.2.9 rsa_signature_gen

Function header:

Uint8_t rsa_signature_gen(uint16_t rsa_bit_len,const uint8_t * hash_digest,uint16_t hlen,uint8_t flags);

Description:

This routine is used for RSA signature generation. It computes M = (hash) ^ (Slot 6). Slot 6 contains either the RSA key, private or public exponent. The opposite exponent must be used for signature verification. Slot 0 contains the RSA key public modulus. The hash is loaded into slot 4. Signature output is in slot 5. The hash digest is truncated to (rsa_bit_len/8) bytes.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if hash_digest points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits.

Hash_digest An unsigned 8 bit integer pointer to constant data consisting of hash to sign

Hlen An unsigned 16 bit integer indicating the hash length in bytes

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

DS50002504A-page 30 2016 Microchip Technology Inc.

Page 31: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.2.10 rsa_signature_verify

Function Header:

uint8_t rsa_signature_verify(uint16_t rsa_bit_len,const uint8_t * signature,const uint8_t * hash_digest_pkcs15,uint16_t hlen,uint8_t flags);

Description:

This routine is used for RSA signature verification. It computes h = (signature) ^ (slot 8). Slot 8 contains either the RSA key private or public exponent. The opposite expo-nent must be used for signature generation. Slot 0 contains the RSA key public modu-lus. The expected hash is loaded into slot 0xC. Recovered hash digest output is in slot 5. PKE compares the contents of slot 5 with slot 0xC. The recovered hash digest will also contain PKCS#1 v1.5 padding. The expected hash digest must also contain the same padding. The expected hash digest must also contain the same padding.

Inputs:

Outputs:

The return values and their description are presented below.

PKE_RET_ERR_BAD_ADDR – this error value is returned if either signature or hash_digest_pkcs15 points to NULL.

PKE_RET_ERR_BUSY – This error value is returned if pke is busy.

PKE_RET_ERR_INVALID_BIT_LENGTH – this error value is returned if rsa_bit_len has invalid data.

PKE_RET_OK – this value indicates a successful execution of requested operation.

Input Parameter Description

Rsa_bit_len An unsigned 16 bit integer indicating the bit length. Permitted values are 1024 or 2048 bits.

Signature An unsigned 8 bit integer pointer to constant data consisting of the signature to be verified.

Hash_digest_pkcs15

An unsigned 8 bit integer pointer to constant data consisting of hash to verify

Hlen An unsigned 16 bit integer specifying the length of hash in bytes

Flags An unsigned 8 bit integer. The flag variable bit definitions are as followsBit NumberValues

00 (do not start), 1 (start after programming)

10 (do not enable pke done and error interrupts), 1(enable pke done and error interrupt)

2016 Microchip Technology Inc. DS50002504A-page 31

Page 32: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.3 PKE FUNCTIONS

5.3.1 pke_power

Function Header:

Void pke_power (bool pwr_on);

Description:

This routine controls the Gate on/off clocks to pke block. Before setting PCR PKE sleep enable, write 0 to the PKE control register to stop the engine.

Inputs:

Outputs:

None

5.3.2 pke_reset

Function Header:

Void pke_reset(void);

Description:

This routine resets the pke block.

Inputs:

None

Output:

None

5.3.3 pke_status

Function Header:

Uint32_t pke_status(void);

Description:

The routine returns the status of the PKE block.

Inputs:

None

Input Parameter Description

Pwr_on A Boolean input parameter. True wakes the block (ungates clocks), False puts the block in sleep mode (gates clock)

DS50002504A-page 32 2016 Microchip Technology Inc.

Page 33: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

Outputs:

The routine returns the pke block status. The important status bits are listed below.

5.3.4 pke_done_status

Function Header:

Bool pke_done_status (uint32_t * status_value);

Description:

The routine is used to check the done status of the pke block.

Inputs:

Outputs:

Returns True if done status is set, False Otherwise.

5.3.5 pke_start

Function Header:

void pke_start(bool ien);

Description:

The function starts the pke block

Inputs:

Outputs:

None

Bit Number Description

16 PKE Busy Bit. This applies to all operations. (1 if busy, 0 otherwise)

9 This bit only applies to signature verify operations. 0 if signature is valid, 1 otherwise.

5 This bit applies to Elliptic Curve Operations. 0 if EC point not at infinity, 1 otherwise

4 This bit applies to Elliptic Curve Operations. 0 if EC point is on curve, 1 otherwise.

Inputs Parameter Description

Status_value An unsigned 32 bit integer pointer where the status value will be stored. Refer pke_status for bit definitions.

Input Parameter Description

Ien A Boolean value specifying the interrupt status. The interrupt enable mask if set, enables done and error interrupts.

2016 Microchip Technology Inc. DS50002504A-page 33

Page 34: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.3.6 pke_ists_clear

Function Header:

uint32_t pke_ists_clear(void);

Description:

This routine reads and clears PKE status. Reading the PKE status register clears block interrupt signals. Reading does not clear status bits. PKE Status bits[31:17] are unused. The PKE GIRQ18[1:0] values are into bits [6:5].

Inputs: None

Outputs:

This routine returns the value in PKE status register in bits[16:0], GIRQ18.Source[1:0] in bits[6:5].

5.3.7 pke_busy

Function Header:

Bool pke_busy(void);

Description:

This routine returns the PKE busy status. PKE status register bits are read-only and are cleared upon reading. Reading also clears PKE block interrupt signal.

Inputs: none

Outputs:

This routine returns a true if PKE is busy, False otherwise.

5.3.8 pke_set_operand_slot

Function Header:

Void pke_set_operand_slot(uint8_t operand, uint8_t slot_num);

Description:

This routine is used to assign an operand or parameter to a particular SCM slot. This is used because not all PKE operations have their memory locations in SCM fixed. This routine allows the flexibility to assign an operand to a particular slot. This routine applies read/write to the PKE configuration register which controls the slots used for input parameters.

Inputs:

Outputs:

None

Input Parameter Description

Operand An unsigned 8 bit integer specifying the operand

Slot_num An unsigned 8 bit integer specifying the slot number

DS50002504A-page 34 2016 Microchip Technology Inc.

Page 35: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.3.9 pke_get_operand_slot

Function header:

Uint8_t pke_get_operand_slot(uint8_t operand);

Description:

This routine is used to assign an address of a particular SCM slot. This is used because not all PKE operations have their memory locations in SCM fixed. This routine allows the flexibility to assign an operand to a particular slot. This routine applies read/write to the PKE configuration register which controls the slots used for input parameters.

Inputs:

Outputs:

This function returns the slot number of the specified operand.

5.3.10 pke_set_operand_slots

Function Header:

void pke_set_operand_slots(uint32_t op_slots);

Description:

This routine allows the caller to set 3 slots in one call (slots A,B and C, where C = F(A,B)).

Inputs:

Outputs:

None

Input Parameter Descriptor

Operand An unsigned 8 bit integer specifying the operand

Note: Certain APIs require the Slot numbers for A,B and C to be set.

Input parameter Description

Op_slots An unsigned 32 bit integer specifying the operand slots

2016 Microchip Technology Inc. DS50002504A-page 35

Page 36: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.3.11 pke_get_slot_addr

Function header:

uint32_t pke_get_ slot_addr(uint8_t slot_num);

Description:

This routine returns the physical memory address in the SCM for the specified slot number. This function can be used to get a reference/pointer to a particular location in SCM.

Inputs:

Outputs:

The routine returns the address for the slot requested.

5.3.12 pke_fill_slot

Function Header:

Void pke_fill_slot(const uint8_t slot_num,const uint32_t fill_val);

Description:

This routine is used to fill a specified slot number with the specified value.

Inputs:

Outputs:

None

5.3.13 pke_clear_scm

Function Header:

Void pke_clear_scm(void);

Description:

This routine clears the PKE block’s shared crypto memory (1.e. This routine fills the SCM slot with 0’s). The caller must ensure the PKE engine is idle and not in sleep state (clock gated) before calling this routine.

Inputs:

None

Outputs:

None

Input Parameter Descriptor

Slot_num An unsigned 8 bit integer specifying the slot number

Input Parameter Description

Slot_num A const unsigned 8 bit integer specifying the slot

Fill_val A const unsigned 32 bit integer specifying the value to be filled

DS50002504A-page 36 2016 Microchip Technology Inc.

Page 37: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.3.14 pke_scm_clear_slot

Function Header:

void pke_scm_clear_slot(uint8_t slot_num);

Description:

This routine clears a specified slot in SCM. That is it fills the specified slot in SCM with 0’s.

Inputs:

Outputs:

None

5.3.15 pke_read_scm

Function Header:

uint16_t pke_read_scm (uint8_t * dest,uint16_t nbytes,uint8_t slot_num,bool reverse_byte_order);

Description:

This routine is used to read specified amount of data from a specified SCM slot.

Inputs:

Outputs:

The routine returns the number of bytes copied to the destination.

Input Parameter Description

slot_num An unsigned 8 bit integer specifying the slot number

Input Parameter Description

Dest An unsigned 8 bit integer pointer to the destination where data will be copied to

Nbytes An unsigned 16 bit integer specifying the number of bytes to be read

Slot_num An unsigned 8 bit integer specifying the slot from which data is to be read

Reverse_byte_order A Boolean flag which if true reads data in reverse byte order

2016 Microchip Technology Inc. DS50002504A-page 37

Page 38: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.3.16 pke_write_scm

Function Header:

void pke_write_scm(const uint8_t * pdata,uint16_t num_bytes,uint8_t slot_num,bool reverse_byte_order);

Description:

This routine is used to write specified amount of data to a specified SCM slot. PKE com-mand register operand size field must be programmed before this routine is called.

Inputs:

Outputs:

None

Input Parameter Description

Pdata An unsigned 8 bit integer pointer to data to be written

Num_bytes An unsigned 16 bit integer indicating the number of bytes to written

Slot_num An unsigned 8 bit integer indicating the slot num

Reverse_byte_order A Boolean flag which if true writes data in reverse byte order

DS50002504A-page 38 2016 Microchip Technology Inc.

Page 39: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.4 RNG FUNCTIONS

5.4.1 rng_power

Function Header:

Void rng_power(bool pwr_on);

Description:

This routine is used for power control of the RNG block.

Inputs:

Outputs:

None

5.4.2 rng_reset:

Function Header:

Void rng_reset(void);

Description:

This routine resets the RNG block.

Inputs:

None

Outputs:

None

5.4.3 rng_mode

Function Header:

Void rng_mode(uint8_t tmode_pseudo);

Description:

The function controls the mode of RNG. The possible modes are Asynchronous (true random mode), and pseudo random mode.

Inputs:

Outputs:

None

Input Parameter Description

Pwr_on A Boolean value if true puts the module to sleep (1 gate off clocks to block), if false enables the block (gate on clocks to block)

Input Parameter Description

Tmode_pseudo An 8 bit unsigned integer if zero, enables asynchronous mode and if 1 enables pseudo random mode

2016 Microchip Technology Inc. DS50002504A-page 39

Page 40: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.4.4 rng_is_on

Function Header:

Bool rng_is_on(void);

Description:

This function is used to check if the NDRNG block is powered on.

Inputs:

None

Outputs:

Returns true if block is on, false otherwise.

5.4.5 rng_start

Function Header:

Void rng_start(void);

Description:

This routine is used to start the NDRNG engine. Once started, the NDRNG will fill its internal 1Kbit internal FIFO with random bits. The NDRNG block will hang if its FIFO is read while empty. Firmware must poll the NDRNG’s FIFO level and only read data from the 32-bit FIFO data register when NOT empty.

Inputs:

None

Outputs:

None

5.4.6 rng_stop

Function Header:

Void rng_stop(void);

Description:

This routine stops the NDRNG engine. When the engine is stopped, the NDRNG will not re-fill its FIFO when data is removed.

Inputs:

None

Outputs:

None

DS50002504A-page 40 2016 Microchip Technology Inc.

Page 41: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.4.7 rng_get_fifo_level

Function Header:

Uint32_t rng_get_fifo_level(void);

Description:

This routine reads the NDRNG FIFO level register and returns the number of 32-bit words of random data currently in FIFO. This call must be issued before reading the FIFO and only read FIFO if this call returns a non-zero number.

Inputs:

None

Outputs:

The call returns the number of 32-bit words in the NDRNG FIFO. Maximum value is 32 (32x32 = 1024 bits).

5.4.8 rng_get_bytes

Function Header:

uint32_t rng_get_bytes( uint8_t * pbuff8,uint32_t num_bytes);

Description:

This routine fills a buffer with random bytes.

Inputs:

Output:

The number of bytes retrieved is returned.

Note: The API reads 32 bits at a time from FIFO. If bytes are requested, a 32 bitword is read and 4 bytes are retrieved. However, only the number of bytesrequested is returned.

Input Parameter Description

Pbuff8 An unsigned 8 bit integer pointer to a buffer where the data will be stored

Num_bytes An unsigned 32 bit integer indicating the number of random bytes to be retrieved.

2016 Microchip Technology Inc. DS50002504A-page 41

Page 42: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.4.9 rng_get_words

Function Header:

Uint32_t rng_get_words(uint32_t * pbuff32,uint32_t num_words);

Description:

This function reads a specified number of words (32-bit data) into the buffer specified by the caller. This function is an all-in-one routine. Powers on the NDRNG, starts the NDRNG, polls the FIFO level and reads words from the FIFO only if it is not empty. It loops until the specified number of words is read. No time out is implemented, if the NDRNG FIFO hardware stops filling the FIFO, this routine will loop forever.

Inputs:

Output:

Returns the actual number of bytes read.

Input Parameter Description

Pbuff32 A pointer to word (32-bit) aligned SRAM buffer

Num_words Number of 32-bit words of random data to read

DS50002504A-page 42 2016 Microchip Technology Inc.

Page 43: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.5 HASH FUNCTIONS

5.5.1 hash_status

Function Header:

uint32_t hash_status(void);

Description:

This routine returns the status of the hash block.

Inputs:

None

Outputs:

Returns the status of the HASH block. The status register is read only. The bit definition is provided below.

5.5.2 hash_busy

Function Header:

Bool hash_busy(void);

Description:

This routine is used to check if the HASH block is busy.

Inputs:

None

Outputs:

Returns a Boolean value which, if true, indicates that the block is busy.

5.5.3 hash_start

Function Header:

void hash_start(bool ien);

Description:

This routine is used to start the HASH engine. Once started, the GIRQ18 bit 9 reflects the done status (1 if done). It must be cleared after it is set.

Inputs:

Outputs:

None

Bit Number Description

0 This bit reflects AHB error. If 0, there is no error. If 1, it indicates that AHB error has occurred.

Input Parameter Description

Ien A Boolean value indicating the state of interrupts

2016 Microchip Technology Inc. DS50002504A-page 43

Page 44: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.5.4 hash_done_status

Function Header:

Bool hash_done_status(uint32_t * status_value);

Description:

This routine is used to check the done status of HASH block. The status register value is updated into the pointer passed by the buffer.

Input:

Outputs:

The return value is true if the done status, false otherwise.

Input Parameter Description

Status_value An unsigned 32 bit integer pointer where the status value will be stored. The bit definitions are listed below.Bit NumbersDescription

31:16Hash Status Register Value

15:0Hash Control Register Value

DS50002504A-page 44 2016 Microchip Technology Inc.

Page 45: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.6 SHA FUNCTIONS

5.6.1 sha12_init

Function Header:

uint8_t sha12_init(SHA12_CONTEXT_T * sha12_ctx,uint8_t sha_mode);

Description:

This routine initializes the SHA12_CONTEXT_T Data structure for the specified mode. This routine does not effect a change on the Hash Hardware.

Inputs:

Outputs:

The return values and their descriptions are presented below.

SHA_RET_ERR_BAD_ADDR – this error is returned if sha12_ctx points to NULL.

SHA_RET_ERR_UNSPPORTED – This error value is returned is sha_mode has a value other than the permitted ones.

SHA_RET_OK – This value is returned if the operation requested is successful.

Input Parameter Description

Sha12_ctx A pointer to the SHA context structure. The structure definition is given below.Struct SHA12_CONTEXT_T{SHA12_DIGEST_U hash; Union{ uint32_t w[ (SHA12_BLOCK_WLEN) * 2]; // SHA12_BLOCK_WLEN = 16 uint8_t b[(SHA12_BLOCK_BLEN)*2]; // SHA12_BLOCK_BLEN = 64 }block;Uint8_t mode;Uint8_t block_len;Uint8_t rsvd[2];Uint64_t total_msg_len;/uint32_t total_msg_len;};

The structure definition of SHA12_DIGEST_U is given below.Union{Uint32_t w[SHA2_WLEN]; //SHA2_WLEN = 8Uint8_t b[SHA2_BLEN]; //SHA2_BLEN = 32};

Sha_mode This indicates the mode of SHA. The permitted modes areSHA_MODE_256SHA_MODE_1

2016 Microchip Technology Inc. DS50002504A-page 45

Page 46: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.6.2 sha12_update

Function Header:

uint8_t sha12_update(SHA12_CONTEXT_T * sha12_ctx,const uint32_t * data,uint32_t data_byte_len);

Description:

This routine runs Hash block on data and updates the SHA12_CONTEXT_T data structure with the number of bytes processed. The data must be aligned to a 4-byte boundary for SHA1 or SHA256. If data length is not a multiple of 64-bytes, then the remaining bytes will be copied into SHA12_CONTEXT_T data structure to be pro-cessed by sha12_finalize.

Inputs:

Output:

The return values and description is presented below.

SHA_RET_ERR_BAD_ADDR – This error value is returned if either sha12_ctx or data points to NULL.

SHA_RET_ERR_BUSY – This error value is returned if the HASH module is busy.

SHA_RET_ERR_MAX_LEN – this error value is returned if total_msg_len is greater than SHA12_MSG_LEN_MAX.

SHA_RET_OK – this value is returned if the requested operation is successful.

Input Parameter Description

Sha12_ctx A pointer to the SHA context structure. The structure definition is given below.Struct SHA12_CONTEXT_T{SHA12_DIGEST_U hash; Union{ Uint32_t w[ (SHA12_BLOCK_WLEN) * 2]; // SHA12_BLOCK_WLEN = 16 Uint8_t b[(SHA12_BLOCK_BLEN)*2]; // SHA12_BLOCK_BLEN = 64 }block;Uint8_t mode;Uint8_t block_len;Uint8_t rsvd[2];Uint64_t total_msg_len;/uint32_t total_msg_len;};

The structure definition of SHA12_DIGEST_U is given below.Union{Uint32_t w[SHA2_WLEN]; //SHA2_WLEN = 8Uint8_t b[SHA2_BLEN]; //SHA2_BLEN = 32};

Data An unsigned 32 bit integer pointer to constant data consisting of data to be updated

Data_byte_len An unsigned 32 bit integer specifying the length of data.

DS50002504A-page 46 2016 Microchip Technology Inc.

Page 47: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.6.3 sha12_finalize

Function Header:

Uint8_t sha12_finalize(SHA12_CONTEXT_T * sha12_ctx);

Description:

This routine applies FIPS padding to SHA256 and performs final hash calculations. It must be used in sequence, sha256_init, sha256_update_start, wait for hash engine to finish, sha256_finalize. The SHA256_CONTEXT_T object will be filled in with nay remaining bytes on the last call to sha256_update_start. SHA engine is approximately 1 cycle per byte for SHA1 and SHA256. It is 64 cycles per 64-byte block.

If the original message length has greater than 56 remaining bytes, the API will need to hash two additional blocks, one for the remaining 56 bytes and one to hold the mes-sage bit length.

Inputs:

Outputs:

The return values and description is presented below.

SHA_RET_ERR_BAD_ADDR – this error value is returned if sha12_ctx points to NULL.

SHA_RET_ERR_BUSY – this error value is returned if hash block is busy.

SHA_RET_START – This value is returned if the operation was successful.

Input Parameter Description

Sha12_ctx A pointer to the SHA context structure. The structure definition is given below.Struct SHA12_CONTEXT_T{SHA12_DIGEST_U hash; Union{ Uint32_t w[ (SHA12_BLOCK_WLEN) * 2]; // SHA12_BLOCK_WLEN = 16 Uint8_t b[(SHA12_BLOCK_BLEN)*2]; // SHA12_BLOCK_BLEN = 64 }block;Uint8_t mode;Uint8_t block_len;Uint8_t rsvd[2];Uint64_t total_msg_len;/uint32_t total_msg_len;};

The structure definition of SHA12_DIGEST_U is given below.Union{Uint32_t w[SHA2_WLEN]; //SHA2_WLEN = 8Uint8_t b[SHA2_BLEN]; //SHA2_BLEN = 32};

2016 Microchip Technology Inc. DS50002504A-page 47

Page 48: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.6.4 sha256_pad_fill

Function Header:

void sha256_pad_fill(uint32_t * blocks64,uint32_t msg_byte_len);

Description:

This routine clears and fills 64-byte SHA256 pad block with FIP padding values. Mes-sage bit length is stored at the end in most significant byte first order. Block64 param-eter must point to an aligned memory buffer of at least 64 bytes.

Inputs:

Outputs:

None

5.6.5 sha256_raw

Function Header:

Uint8_t sha256_raw(uint32_t * data,uint32_t * digest,uint32_t num64b_blocks);

Description:

This routine is used to calculate SHA256 on data. This routine does not add FIPS pad-ding. Input data may or may not include FIPS padding.

Inputs:

Outputs:

The return values and their descriptions are presented below.

SHA_RET_ERR_BAD_ADDR – this error value is returned if either data or digest points to NULL.

SHA_RET_ERR_BUSY – this error value is returned if the hash block is busy.

SHA_RET_OK – this return value indicates that the operation was successful.

Input Parameter Description

Block64 An unsigned 32 bit integer pointer containing a memory aligned buffer (See Description)

Msg_byte_len ??

Input Parameter Description

Data An unsigned 32 bit integer pointer containing data on which SHA256 is to be calculated

Digest An unsigned 32 bit integer pointer containing the hash digest of the message

num64b_blocks An unsigned 32 bit integer specifying size in 64 bit blocks

DS50002504A-page 48 2016 Microchip Technology Inc.

Page 49: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.6.6 sha256_raw_init

Function Header:

Void sha256_raw_init(uint32_t * sha256_digest);

Description:

This routine is similar to the sha12 init function but does not use the SHA12_CONTEX-T_T structure. The caller must provide a properly aligned buffer of 2 * 64 bytes (twice the SHA12/SHA256 block length) to be used for padding.

Inputs:

Outputs:

None

5.6.7 sha256_raw_update

Function Header:

uint8_t sha256_raw_update(uint32_t * data,uint32_t * digest,uint32_t num64b_blocks);

Description:

This routine runs hash block on the data provided.

Inputs:

Outputs:

The return values and their descriptions are presented below.

SHA_RET_ERR_BAD_ADDR – This error value is returned if either data or digest points to NULL.

SHA_RET_ERR_BUSY – This error value is returned if hash block is busy.

SHA_RET_OK – this value is returned if operation is successful.

Input Parameter Description

Sha256_digest An unsigned 32 bit integer pointer consisting of 2 * 64 bytes aligned data

Input Parameter Description

Data An unsigned 32 bit integer pointer consisting of data

Digest An unsigned 32 bit integer pointer consisting of digest

Num64b_blocks An unsigned 32 bit integer specifying size in 64 bit blocks

2016 Microchip Technology Inc. DS50002504A-page 49

Page 50: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.6.8 hash_iclr

Function Header:

uint32_t hash_iclr(void);

Description:

This function clears hash interrupt.

Inputs:

None

Outputs:

This function returns the status of the hash block.

5.6.9 sha_init

Function Header:

Uint8_t sha_init(uint8_t mode, Uin32_t * digest);

Description:

This routine initializes the hash engine for SHA operation. Programs supported SHA operation’s initial value, digest address and operation (SHA1, SHA256 or SHA384). Hash engine does not need to be started. SHA1 and SHA256 require 4 byte alignment. SHA384 require 8-byte alignment.

Inputs:

Outputs:

The return values and their description is presented below.

0 = Success

1 = Hash Engine Busy

2 = Unsupported SHA operation

3 = Bad digest pointer, NULL or mis-aligned.

Input Parameter Description

Mode An unsigned 8 bit integer indicating the mode. Permitted modes are SHA_MODE_1, SHA_MODE_256.

Digest An unsigned 32 bit integer pointer to digest

DS50002504A-page 50 2016 Microchip Technology Inc.

Page 51: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

APIs

5.6.10 sha_update

Function Header:

uint8_t sha_update(uint32_t * pdata,uint16_t nblocks,uint8_t flags);

Description:

This routine programs hash engine with data address and the number of data blocks to process. Sha block must be initialized before this routine is called. SHA1 and SHA256 require 4 byte alignment. SHA384 require 8-byte alignment. Has engine is not started, if return value is non-zero caller must call hash_start().

Inputs:

Outputs:

The return values and their description is presented below.

0 = Success

1 = Hash engine busy

2 = pdata is a null pointer

3 = data is misaligned

Input Parameter Description

Pdata An unsigned 32 bit integer pointer to data

Nblocks An unsigned 16 bit integer specifying the number of blocks

Flags Bit 0 indicates clear status(1-clear), bit enable interrupt status(1-enable) and bit indicates start/stop (1-start)

2016 Microchip Technology Inc. DS50002504A-page 51

Page 52: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

5.6.11 sha_final

Function Header:

uint8_t sha_final(uint32_t * padbuf,uint32_t total_msg_len,const uint8_t * perm,uint8_t flags);

Description:

This routine implement the standard SHA padding described in the FIPS standard.

Inputs:

Outputs:

The return values and their descriptions are presented below.

0 = Success

1 = Hash engine busy

2 = pdata is a null pointer

3 = data is misaligned

Input Parameter Description

Padbuf An unsigned 32 bit integer pointer consisting of ??

Total_msg_len An unsigned 32 bit integer specifying the length of message.

Perm An unsigned 8 bit integer pointer to constant data consisting of …?

Flags An unsigned 8 bit integer. Bit 0 indicates clear status(1-clear), bit enable interrupt status(1-enable) and bit indicates start/stop (1-start)

DS50002504A-page 52 2016 Microchip Technology Inc.

Page 53: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 6. PKE Slot Usage

The CEC1302 has dedicated crypto SRAM (shared crypto memory - SCM) for PKE block usage. This memory is shared by various PKE operations and hence it limits the operations which can be carried out in parallel.

The SCM is used to program parameters and keys and to upload/download oper-ands/results from the host side. The shared crypto memory of MEC2016 is divided into 31 slots (slot0-slot30) each of 512 bytes.

The table below lists the usage of the slots for various operations.

Since the operations above share multiple slots these PKE crypto operations cannot be run in parallel.

RSA Encryption with Public Key Slot 8 - public exponentSlot 0 – public modulusSlot 5 – Encryption output

RSA Encryption with Private Key Slot 8 – private exponentSlot 0 – public modulusSlot 5 – Encryption output

RSA Decryption with Private key Slot 0 – public modulusSlot 6 – private exponentSlot 8 – public exponentSlot 5 – Decryption output

PKE clear slot Specified at run time

PKE read slot Specified at run time

PKE write slot Specified at run time

EC Modular arithmetic Specified at run time

2016 Microchip Technology Inc. DS50002504A-page 53

Page 54: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

CEC1302 Crypto API User’s Guide

Chapter 7. Timing Analysis

For all the crypto operations mentioned in Chapter 5. “APIs” timing measurement is done at a CPU clock speed of 48MHz and the results are shown below. Except RNG, all blocks work on 2K input message.

7.1 AES

7.2 PKE

7.3 SHA

7.4 RNG

Mode ECB CBC CTR CFB OFB

Encrypt Decrypt Encrypt Decrypt Encrypt Decrypt Encrypt Decrypt Encrypt Decrypt

Total Time (uSec)

663 616.5 655.62 616.59 655.62 616.2 655.56 616.26 655.44 616.2

RSA1 RSA2

Total time (Sec) 1.06 1.06

SHA1 SHA12

Total time (uSec) 535.47 440

True Random Number Pseudo Random Number

Total time (uSec) 70.83 135.93

2016 Microchip Technology Inc. DS50002504A-page 54

Page 55: CEC1302 Crypto API User's Guide - Microchip Technologyww1.microchip.com/downloads/en/DeviceDoc/50002504A.pdf · This document describes how to use the CEC1302 Crypto API as a development

DS50002504A-page 55 2016 Microchip Technology Inc.

AMERICASCorporate Office2355 West Chandler Blvd.Chandler, AZ 85224-6199Tel: 480-792-7200 Fax: 480-792-7277Technical Support: http://www.microchip.com/supportWeb Address: www.microchip.com

AtlantaDuluth, GA Tel: 678-957-9614 Fax: 678-957-1455

Austin, TXTel: 512-257-3370

BostonWestborough, MA Tel: 774-760-0087 Fax: 774-760-0088

ChicagoItasca, IL Tel: 630-285-0071 Fax: 630-285-0075

ClevelandIndependence, OH Tel: 216-447-0464 Fax: 216-447-0643

DallasAddison, TX Tel: 972-818-7423 Fax: 972-818-2924

DetroitNovi, MI Tel: 248-848-4000

Houston, TX Tel: 281-894-5983

IndianapolisNoblesville, IN Tel: 317-773-8323Fax: 317-773-5453

Los AngelesMission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608

New York, NY Tel: 631-435-6000

San Jose, CA Tel: 408-735-9110

Canada - TorontoTel: 905-673-0699 Fax: 905-673-6509

ASIA/PACIFICAsia Pacific OfficeSuites 3707-14, 37th FloorTower 6, The GatewayHarbour City, Kowloon

Hong KongTel: 852-2943-5100Fax: 852-2401-3431

Australia - SydneyTel: 61-2-9868-6733Fax: 61-2-9868-6755

China - BeijingTel: 86-10-8569-7000 Fax: 86-10-8528-2104

China - ChengduTel: 86-28-8665-5511Fax: 86-28-8665-7889

China - ChongqingTel: 86-23-8980-9588Fax: 86-23-8980-9500

China - DongguanTel: 86-769-8702-9880

China - HangzhouTel: 86-571-8792-8115 Fax: 86-571-8792-8116

China - Hong Kong SARTel: 852-2943-5100 Fax: 852-2401-3431

China - NanjingTel: 86-25-8473-2460Fax: 86-25-8473-2470

China - QingdaoTel: 86-532-8502-7355Fax: 86-532-8502-7205

China - ShanghaiTel: 86-21-5407-5533 Fax: 86-21-5407-5066

China - ShenyangTel: 86-24-2334-2829Fax: 86-24-2334-2393

China - ShenzhenTel: 86-755-8864-2200 Fax: 86-755-8203-1760

China - WuhanTel: 86-27-5980-5300Fax: 86-27-5980-5118

China - XianTel: 86-29-8833-7252Fax: 86-29-8833-7256

ASIA/PACIFICChina - XiamenTel: 86-592-2388138 Fax: 86-592-2388130

China - ZhuhaiTel: 86-756-3210040 Fax: 86-756-3210049

India - BangaloreTel: 91-80-3090-4444 Fax: 91-80-3090-4123

India - New DelhiTel: 91-11-4160-8631Fax: 91-11-4160-8632

India - PuneTel: 91-20-3019-1500

Japan - OsakaTel: 81-6-6152-7160 Fax: 81-6-6152-9310

Japan - TokyoTel: 81-3-6880- 3770 Fax: 81-3-6880-3771

Korea - DaeguTel: 82-53-744-4301Fax: 82-53-744-4302

Korea - SeoulTel: 82-2-554-7200Fax: 82-2-558-5932 or 82-2-558-5934

Malaysia - Kuala LumpurTel: 60-3-6201-9857Fax: 60-3-6201-9859

Malaysia - PenangTel: 60-4-227-8870Fax: 60-4-227-4068

Philippines - ManilaTel: 63-2-634-9065Fax: 63-2-634-9069

SingaporeTel: 65-6334-8870Fax: 65-6334-8850

Taiwan - Hsin ChuTel: 886-3-5778-366Fax: 886-3-5770-955

Taiwan - KaohsiungTel: 886-7-213-7828

Taiwan - TaipeiTel: 886-2-2508-8600 Fax: 886-2-2508-0102

Thailand - BangkokTel: 66-2-694-1351Fax: 66-2-694-1350

EUROPEAustria - WelsTel: 43-7242-2244-39Fax: 43-7242-2244-393

Denmark - CopenhagenTel: 45-4450-2828 Fax: 45-4485-2829

France - ParisTel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79

Germany - DusseldorfTel: 49-2129-3766400

Germany - KarlsruheTel: 49-721-625370

Germany - MunichTel: 49-89-627-144-0 Fax: 49-89-627-144-44

Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781

Italy - VeniceTel: 39-049-7625286

Netherlands - DrunenTel: 31-416-690399 Fax: 31-416-690340

Poland - WarsawTel: 48-22-3325737

Spain - MadridTel: 34-91-708-08-90Fax: 34-91-708-08-91

Sweden - StockholmTel: 46-8-5090-4654

UK - WokinghamTel: 44-118-921-5800Fax: 44-118-921-5820

Worldwide Sales and Service

07/14/15