S32 SDK
csec_hal.c
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 #include "csec_hal.h"
20 
40 /*******************************************************************************
41  * Definitions
42  ******************************************************************************/
43 
44 #define CSEC_UPPER_HALF_MASK (0xFFFF0000U)
45 #define CSEC_UPPER_HALF_SHIFT (0x10U)
46 #define CSEC_LOWER_HALF_MASK (0xFFFFU)
47 #define CSEC_LOWER_HALF_SHIFT (0U)
48 
49 /*******************************************************************************
50  * Code
51  ******************************************************************************/
52 
53 /*FUNCTION**********************************************************************
54  *
55  * Function Name : CSEC_HAL_WriteCommandBytes
56  * Description : This function writes command bytes to CSE_PRAM at 32-bit
57  * aligned addresses.
58  *
59  * Implements : CSEC_HAL_WriteCommandBytes_Activity
60  *END**************************************************************************/
61 void CSEC_HAL_WriteCommandBytes(uint8_t offset, const uint8_t *bytes, uint8_t numBytes)
62 {
63  uint8_t i = 0U;
64 
65  DEV_ASSERT(bytes != NULL);
66 
67  while ((i + 3U) < numBytes)
68  {
69  CSE_PRAM->RAMn[(offset + i) >> 2U].DATA_32 =
71  CSE_PRAM_RAMn_DATA_32_BYTE_1(bytes[i + 1U]) |
72  CSE_PRAM_RAMn_DATA_32_BYTE_2(bytes[i + 2U]) |
73  CSE_PRAM_RAMn_DATA_32_BYTE_3(bytes[i + 3U]);
74  i = (uint8_t)(i + 4U);
75  }
76  while (i < numBytes)
77  {
78  CSEC_HAL_WriteCommandByte(offset + i, bytes[i]);
79  i++;
80  }
81 }
82 
83 /*FUNCTION**********************************************************************
84  *
85  * Function Name : CSEC_HAL_WriteCommandHalfWord
86  * Description : This function writes a command half word to CSE_PRAM at a
87  * 16-bit aligned offset.
88  *
89  * Implements : CSEC_HAL_WriteCommandHalfWord_Activity
90  *END**************************************************************************/
91 void CSEC_HAL_WriteCommandHalfWord(uint8_t offset, uint16_t halfWord)
92 {
93  uint32_t tmp;
94 
95  tmp = CSE_PRAM->RAMn[(offset >> 2U)].DATA_32;
96 
97  if ((offset & 2U) != 0U)
98  {
99  tmp = tmp & ~CSEC_LOWER_HALF_MASK;
100  tmp = tmp | ((((uint32_t) halfWord) << CSEC_LOWER_HALF_SHIFT) & CSEC_LOWER_HALF_MASK);
101  }
102  else
103  {
104  tmp = tmp & ~CSEC_UPPER_HALF_MASK;
105  tmp = tmp | ((((uint32_t) halfWord) << CSEC_UPPER_HALF_SHIFT) & CSEC_UPPER_HALF_MASK);
106  }
107 
108  CSE_PRAM->RAMn[(offset >> 2U)].DATA_32 = tmp;
109 }
110 
111 /*FUNCTION**********************************************************************
112  *
113  * Function Name : CSEC_HAL_WriteCommandByte
114  * Description : This function writes a single byte to CSE_PRAM.
115  *
116  * Implements : CSEC_HAL_WriteCommandByte_Activity
117  *END**************************************************************************/
118 void CSEC_HAL_WriteCommandByte(uint8_t offset, uint8_t byte)
119 {
120  switch (offset & 0x3U)
121  {
122  case 0x0U:
123  CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8HU = byte;
124  break;
125  case 0x1U:
126  CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8HL = byte;
127  break;
128  case 0x2U:
129  CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8LU = byte;
130  break;
131  case 0x3U:
132  CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8LL = byte;
133  break;
134  default:
135  /* Impossible to get here */
136  break;
137  }
138 }
139 
140 /*FUNCTION**********************************************************************
141  *
142  * Function Name : CSEC_HAL_WriteCommandWords
143  * Description : This function writes command words to CSE_PRAM at a 32-bit
144  * aligned offset.
145  *
146  * Implements : CSEC_HAL_WriteCommandWords_Activity
147  *END**************************************************************************/
148 void CSEC_HAL_WriteCommandWords(uint8_t offset, const uint32_t *words, uint8_t numWords)
149 {
150  uint8_t i = 0U;
151  uint8_t alignedOffset = (uint8_t)(offset >> 2U);
152 
153  DEV_ASSERT(words != NULL);
154 
155  while (i < numWords)
156  {
157  CSE_PRAM->RAMn[alignedOffset + i].DATA_32 = words[i];
158  i++;
159  }
160 }
161 
162 /*FUNCTION**********************************************************************
163  *
164  * Function Name : CSEC_HAL_ReadCommandBytes
165  * Description : This function reads command bytes from CSE_PRAM from a 32-bit
166  * aligned offset.
167  *
168  * Implements : CSEC_HAL_ReadCommandBytes_Activity
169  *END**************************************************************************/
170 void CSEC_HAL_ReadCommandBytes(uint8_t offset, uint8_t *bytes, uint8_t numBytes)
171 {
172  uint8_t i = 0U;
173 
174  DEV_ASSERT(bytes != NULL);
175 
176  while ((i + 3U) < numBytes)
177  {
178  uint32_t tmp = CSE_PRAM->RAMn[(offset + i) >> 2U].DATA_32;
179 
181  bytes[i + 1U] = (uint8_t)((tmp & CSE_PRAM_RAMn_DATA_32_BYTE_1_MASK) >> CSE_PRAM_RAMn_DATA_32_BYTE_1_SHIFT);
182  bytes[i + 2U] = (uint8_t)((tmp & CSE_PRAM_RAMn_DATA_32_BYTE_2_MASK) >> CSE_PRAM_RAMn_DATA_32_BYTE_2_SHIFT);
183  bytes[i + 3U] = (uint8_t)((tmp & CSE_PRAM_RAMn_DATA_32_BYTE_3_MASK) >> CSE_PRAM_RAMn_DATA_32_BYTE_3_SHIFT);
184  i = (uint8_t)(i + 4U);
185  }
186  while (i < numBytes)
187  {
188  bytes[i] = CSEC_HAL_ReadCommandByte(offset + i);
189  i++;
190  }
191 }
192 
193 /*FUNCTION**********************************************************************
194  *
195  * Function Name : CSEC_HAL_ReadCommandHalfWord
196  * Description : This function reads a command half word from CSE_PRAM from a
197  * 16-bit aligned offset.
198  *
199  * Implements : CSEC_HAL_ReadCommandHalfWord_Activity
200  *END**************************************************************************/
201 uint16_t CSEC_HAL_ReadCommandHalfWord(uint8_t offset)
202 {
203  uint16_t halfWord;
204 
205  if ((offset & 2U) != 0U)
206  {
207  halfWord = (uint16_t)((CSE_PRAM->RAMn[(offset >> 2U)].DATA_32 & CSEC_LOWER_HALF_MASK) >> CSEC_LOWER_HALF_SHIFT);
208  }
209  else
210  {
211  halfWord = (uint16_t)((CSE_PRAM->RAMn[(offset >> 2U)].DATA_32 & CSEC_UPPER_HALF_MASK) >> CSEC_UPPER_HALF_SHIFT);
212  }
213 
214  return halfWord;
215 }
216 
217 /*FUNCTION**********************************************************************
218  *
219  * Function Name : CSEC_HAL_ReadCommandByte
220  * Description : This function reads a single byte from CSE_PRAM.
221  *
222  * Implements : CSEC_HAL_ReadCommandByte_Activity
223  *END**************************************************************************/
224 uint8_t CSEC_HAL_ReadCommandByte(uint8_t offset)
225 {
226  uint8_t byte = 0;
227 
228  switch (offset & 0x3U)
229  {
230  case 0x0U:
231  byte = CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8HU;
232  break;
233  case 0x1U:
234  byte = CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8HL;
235  break;
236  case 0x2U:
237  byte = CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8LU;
238  break;
239  case 0x3U:
240  byte = CSE_PRAM->RAMn[offset >> 2U].ACCESS8BIT.DATA_8LL;
241  break;
242  default:
243  /* Impossible to get here */
244  break;
245  }
246 
247  return byte;
248 }
249 
250 /*FUNCTION**********************************************************************
251  *
252  * Function Name : CSEC_HAL_ReadCommandWords
253  * Description : This function reads command words from CSE_PRAM from a 32-bit
254  * aligned offset.
255  *
256  * Implements : CSEC_HAL_ReadCommandWords_Activity
257  *END**************************************************************************/
258 void CSEC_HAL_ReadCommandWords(uint8_t offset, uint32_t *words, uint8_t numWords)
259 {
260  uint8_t i = 0U;
261  uint8_t alignedOffset = (uint8_t)(offset >> 2U);
262 
263  DEV_ASSERT(words != NULL);
264 
265  while (i < numWords)
266  {
267  words[i] = CSE_PRAM->RAMn[alignedOffset + i].DATA_32;
268  i++;
269  }
270 }
271 
272 /*FUNCTION**********************************************************************
273  *
274  * Function Name : CSEC_HAL_WriteCmdAndWait
275  * Description : This function writes the header of a command and waits for
276  * completion.
277  *
278  * Implements : CSEC_HAL_WriteCmdAndWait_Activity
279  *END**************************************************************************/
282  csec_func_format_t funcFormat,
283  csec_call_sequence_t callSeq,
284  csec_key_id_t keyId)
285 {
286  CSE_PRAM->RAMn[0].DATA_32 =
288  CSE_PRAM_RAMn_DATA_32_BYTE_1(funcFormat) |
291 
292  while ((FTFC->FSTAT & FTFC_FSTAT_CCIF_MASK) == 0U)
293  {
294  /* Wait until the CCIF flag is set */
295  }
296 }
298 
299 /*******************************************************************************
300  * EOF
301  ******************************************************************************/
#define CSEC_UPPER_HALF_MASK
Definition: csec_hal.c:44
#define CSEC_LOWER_HALF_MASK
Definition: csec_hal.c:46
#define CSE_PRAM_RAMn_DATA_32_BYTE_1(x)
Definition: S32K144.h:2195
#define CSE_PRAM_RAMn_DATA_32_BYTE_1_MASK
Definition: S32K144.h:2192
#define FTFC_FSTAT_CCIF_MASK
Definition: S32K144.h:3839
#define CSEC_UPPER_HALF_SHIFT
Definition: csec_hal.c:45
#define CSE_PRAM_RAMn_DATA_32_BYTE_2_SHIFT
Definition: S32K144.h:2189
#define CSE_PRAM_RAMn_DATA_32_BYTE_2_MASK
Definition: S32K144.h:2188
#define CSE_PRAM_RAMn_DATA_32_BYTE_3(x)
Definition: S32K144.h:2187
#define CSE_PRAM_RAMn_DATA_32_BYTE_0_SHIFT
Definition: S32K144.h:2197
#define DEV_ASSERT(x)
Definition: devassert.h:78
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
#define CSE_PRAM_RAMn_DATA_32_BYTE_1_SHIFT
Definition: S32K144.h:2193
#define END_FUNCTION_DEFINITION_RAMSECTION
Definition: s32_core_cm4.h:158
csec_cmd_t
CSEc commands which follow the same values as the SHE command definition.
Definition: csec_hal.h:108
#define CSE_PRAM_RAMn_DATA_32_BYTE_0_MASK
Definition: S32K144.h:2196
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 START_FUNCTION_DEFINITION_RAMSECTION
Definition: s32_core_cm4.h:157
#define CSE_PRAM_RAMn_DATA_32_BYTE_0(x)
Definition: S32K144.h:2199
#define FTFC
Definition: S32K144.h:3798
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
#define CSE_PRAM_RAMn_DATA_32_BYTE_2(x)
Definition: S32K144.h:2191
#define CSEC_LOWER_HALF_SHIFT
Definition: csec_hal.c:47
csec_call_sequence_t
Specifies if the information is the first or a following function call.
Definition: csec_hal.h:185
#define CSE_PRAM_RAMn_DATA_32_BYTE_3_SHIFT
Definition: S32K144.h:2185
#define CSE_PRAM
Definition: S32K144.h:2168
START_FUNCTION_DEFINITION_RAMSECTION void CSEC_HAL_WriteCmdAndWait(csec_cmd_t funcId, csec_func_format_t funcFormat, csec_call_sequence_t callSeq, csec_key_id_t keyId)
Definition: csec_hal.c:281
#define CSE_PRAM_RAMn_DATA_32_BYTE_3_MASK
Definition: S32K144.h:2184
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
void CSEC_HAL_WriteCommandByte(uint8_t offset, uint8_t byte)
Writes a command byte to CSE_PRAM.
Definition: csec_hal.c:118