S32 SDK
csec_hal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
19 #ifndef CSEC_HAL_H
20 #define CSEC_HAL_H
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "device_registers.h"
25 
65 /*******************************************************************************
66  * Definitions
67  ******************************************************************************/
68 
69 #include <stddef.h>
70 #include "status.h"
71 
73 #define CSEC_STATUS_BUSY (0x1U)
74 
75 #define CSEC_STATUS_SECURE_BOOT (0x2U)
76 
78 #define CSEC_STATUS_BOOT_INIT (0x4U)
79 
82 #define CSEC_STATUS_BOOT_FINISHED (0x8U)
83 
85 #define CSEC_STATUS_BOOT_OK (0x10U)
86 
87 #define CSEC_STATUS_RND_INIT (0x20U)
88 
89 #define CSEC_STATUS_EXT_DEBUGGER (0x40U)
90 
92 #define CSEC_STATUS_INT_DEBUGGER (0x80U)
93 
101 typedef uint8_t csec_status_t;
102 
108 typedef enum {
131 } csec_cmd_t;
132 
139 typedef enum {
154  CSEC_RAM_KEY = 0xFU,
155  CSEC_KEY_11 = 0x14U,
166 } csec_key_id_t;
167 
175 typedef enum {
179 
185 typedef enum {
189 
192 #define CSEC_NO_ERROR 0x1U
193 #define CSEC_SEQUENCE_ERROR 0x2U
194 #define CSEC_KEY_NOT_AVAILABLE 0x4U
195 #define CSEC_KEY_INVALID 0x8U
196 #define CSEC_KEY_EMPTY 0x10U
197 #define CSEC_NO_SECURE_BOOT 0x20U
198 #define CSEC_KEY_WRITE_PROTECTED 0x40U
199 #define CSEC_KEY_UPDATE_ERROR 0x80U
200 #define CSEC_RNG_SEED 0x100U
201 #define CSEC_NO_DEBUGGING 0x200U
202 #define CSEC_MEMORY_FAILURE 0x400U
203 #define CSEC_GENERAL_ERROR 0x800U
204 
205 
206 /*******************************************************************************
207  * API
208  ******************************************************************************/
209 
210 #if defined(__cplusplus)
211 extern "C" {
212 #endif
213 
229 static inline void CSEC_HAL_WriteCommandHeader(csec_cmd_t funcId,
230  csec_func_format_t funcFormat,
231  csec_call_sequence_t callSeq,
232  csec_key_id_t keyId)
233 {
234  CSE_PRAM->RAMn[0].DATA_32 =
236  CSE_PRAM_RAMn_DATA_32_BYTE_1(funcFormat) |
239 }
240 
250 void CSEC_HAL_WriteCommandBytes(uint8_t offset, const uint8_t *bytes, uint8_t numBytes);
251 
262 void CSEC_HAL_WriteCommandHalfWord(uint8_t offset, uint16_t halfWord);
263 
272 void CSEC_HAL_WriteCommandByte(uint8_t offset, uint8_t byte);
273 
283 void CSEC_HAL_WriteCommandWords(uint8_t offset, const uint32_t *words, uint8_t numWords);
284 
294 void CSEC_HAL_ReadCommandBytes(uint8_t offset, uint8_t *bytes, uint8_t numBytes);
295 
306 uint16_t CSEC_HAL_ReadCommandHalfWord(uint8_t offset);
307 
316 uint8_t CSEC_HAL_ReadCommandByte(uint8_t offset);
317 
327 void CSEC_HAL_ReadCommandWords(uint8_t offset, uint32_t *words, uint8_t numWords);
328 
336 static inline void CSEC_HAL_WaitCommandCompletion(void)
337 {
338  while ((FTFC->FSTAT & FTFC_FSTAT_CCIF_MASK) == 0U)
339  {
340  /* Wait until the CCIF flag is set */
341  }
342 }
343 
353 static inline status_t CSEC_HAL_ReadErrorBits(void)
354 {
356  status_t stat;
357 
358  switch (errBits)
359  {
360  case CSEC_NO_ERROR:
361  stat = STATUS_SUCCESS;
362  break;
363  case CSEC_SEQUENCE_ERROR:
365  break;
368  break;
369  case CSEC_KEY_INVALID:
371  break;
372  case CSEC_KEY_EMPTY:
373  stat = STATUS_CSEC_KEY_EMPTY;
374  break;
375  case CSEC_NO_SECURE_BOOT:
377  break;
380  break;
383  break;
384  case CSEC_RNG_SEED:
385  stat = STATUS_CSEC_RNG_SEED;
386  break;
387  case CSEC_NO_DEBUGGING:
389  break;
390  case CSEC_MEMORY_FAILURE:
392  break;
393  case CSEC_GENERAL_ERROR:
394  default:
395  stat = STATUS_ERROR;
396  break;
397  }
398 
399  return stat;
400 }
401 
412 {
413  return (FTFC->FCSESTAT);
414 }
415 
433  csec_func_format_t funcFormat,
434  csec_call_sequence_t callSeq,
435  csec_key_id_t keyId)
437 
447 static inline void CSEC_HAL_SetInterrupt(bool enable)
448 {
449  uint8_t tmp = FTFC->FCNFG;
450 
451  FTFC->FCNFG = (uint8_t)((tmp & ~FTFC_FCNFG_CCIE_MASK) | FTFC_FCNFG_CCIE(enable ? 1U : 0U));
452 }
453 
454 #if defined(__cplusplus)
455 }
456 #endif
457 
460 #endif /* CSEC_HAL_H */
461 
462 /*******************************************************************************
463  * EOF
464  ******************************************************************************/
#define CSE_PRAM_RAMn_DATA_32_BYTE_1(x)
Definition: S32K144.h:2195
#define CSEC_KEY_WRITE_PROTECTED
Definition: csec_hal.h:198
static void CSEC_HAL_WaitCommandCompletion(void)
Waits for the completion of a CSEc command.
Definition: csec_hal.h:336
#define FTFC_FSTAT_CCIF_MASK
Definition: S32K144.h:3839
#define END_FUNCTION_DECLARATION_RAMSECTION
Definition: s32_core_cm4.h:147
static void CSEC_HAL_WriteCommandHeader(csec_cmd_t funcId, csec_func_format_t funcFormat, csec_call_sequence_t callSeq, csec_key_id_t keyId)
Writes the command header to CSE_PRAM.
Definition: csec_hal.h:229
START_FUNCTION_DECLARATION_RAMSECTION void CSEC_HAL_WriteCmdAndWait(csec_cmd_t funcId, csec_func_format_t funcFormat, csec_call_sequence_t callSeq, csec_key_id_t keyId) END_FUNCTION_DECLARATION_RAMSECTION static inline void CSEC_HAL_SetInterrupt(bool enable)
Writes the command header to CSE_PRAM and waits for completion.
Definition: csec_hal.h:432
#define CSE_PRAM_RAMn_DATA_32_BYTE_3(x)
Definition: S32K144.h:2187
#define FTFC_FCNFG_CCIE(x)
Definition: S32K144.h:3867
void CSEC_HAL_WriteCommandBytes(uint8_t offset, const uint8_t *bytes, uint8_t numBytes)
Writes command bytes to CSE_PRAM.
Definition: csec_hal.c:61
static csec_status_t CSEC_HAL_ReadStatus(void)
Reads the status of the CSEc module.
Definition: csec_hal.h:411
csec_cmd_t
CSEc commands which follow the same values as the SHE command definition.
Definition: csec_hal.h:108
#define CSEC_NO_SECURE_BOOT
Definition: csec_hal.h:197
#define START_FUNCTION_DECLARATION_RAMSECTION
Places a function in RAM.
Definition: s32_core_cm4.h:146
#define CSEC_KEY_UPDATE_ERROR
Definition: csec_hal.h:199
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
void CSEC_HAL_ReadCommandWords(uint8_t offset, uint32_t *words, uint8_t numWords)
Reads command words from CSE_PRAM.
Definition: csec_hal.c:258
void CSEC_HAL_ReadCommandBytes(uint8_t offset, uint8_t *bytes, uint8_t numBytes)
Reads command bytes from CSE_PRAM.
Definition: csec_hal.c:170
void CSEC_HAL_WriteCommandWords(uint8_t offset, const uint32_t *words, uint8_t numWords)
Writes command words to CSE_PRAM.
Definition: csec_hal.c:148
#define CSEC_RNG_SEED
Definition: csec_hal.h:200
#define CSEC_KEY_EMPTY
Definition: csec_hal.h:196
#define CSEC_KEY_NOT_AVAILABLE
Definition: csec_hal.h:194
#define CSE_PRAM_RAMn_DATA_32_BYTE_0(x)
Definition: S32K144.h:2199
#define FTFC
Definition: S32K144.h:3798
static status_t CSEC_HAL_ReadErrorBits(void)
Reads the error bits from PRAM.
Definition: csec_hal.h:353
#define CSEC_GENERAL_ERROR
Definition: csec_hal.h:203
csec_func_format_t
Specifies how the data is transferred to/from the CSE. There are two use cases. One is to copy all da...
Definition: csec_hal.h:175
uint8_t CSEC_HAL_ReadCommandByte(uint8_t offset)
Reads a command byte from CSE_PRAM.
Definition: csec_hal.c:224
uint8_t csec_status_t
Represents the status of the CSEc module. Provides one bit for each status code as per SHE specificat...
Definition: csec_hal.h:101
#define CSE_PRAM_RAMn_DATA_32_BYTE_2(x)
Definition: S32K144.h:2191
#define CSEC_SEQUENCE_ERROR
Definition: csec_hal.h:193
csec_call_sequence_t
Specifies if the information is the first or a following function call.
Definition: csec_hal.h:185
#define CSE_PRAM
Definition: S32K144.h:2168
#define FEATURE_CSEC_ERROR_BITS_OFFSET
CSE_PRAM offset of the error bits field contained by all commands.
#define CSEC_KEY_INVALID
Definition: csec_hal.h:195
#define CSEC_NO_ERROR
Represents the result of the execution of a command. Provides one bit for each error code as per SHE ...
Definition: csec_hal.h:192
#define CSEC_MEMORY_FAILURE
Definition: csec_hal.h:202
void CSEC_HAL_WriteCommandHalfWord(uint8_t offset, uint16_t halfWord)
Writes a command half word to CSE_PRAM.
Definition: csec_hal.c:91
csec_key_id_t
Specify the KeyID to be used to implement the requested cryptographic operation.
Definition: csec_hal.h:139
uint16_t CSEC_HAL_ReadCommandHalfWord(uint8_t offset)
Reads a command half word from CSE_PRAM.
Definition: csec_hal.c:201
#define CSEC_NO_DEBUGGING
Definition: csec_hal.h:201
#define FTFC_FCNFG_CCIE_MASK
Definition: S32K144.h:3864
void CSEC_HAL_WriteCommandByte(uint8_t offset, uint8_t byte)
Writes a command byte to CSE_PRAM.
Definition: csec_hal.c:118