1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 /***********************************************************************************************************************
8 * Includes
9 **********************************************************************************************************************/
10
11 #include "bsp_api.h"
12 #include "hw_sce_aes_private.h"
13 #include "hw_sce_ra_private.h"
14
15 /***********************************************************************************************************************
16 * Macro definitions
17 ***********************************************************************************************************************/
18 #define HW_BYTE_TO_BIT_LENGTH(x) ((x) << 3)
19
20 #define SCE_AES_PRV_CCM_TLEN_VALUE_OFFSET (16 / 8)
21 #define SCE_AES_PRV_CCM_TLEN_MIN_VALUE (32 / 8)
22
23 #define SCE_AES_PRV_CCM_NLEN_VALUE_OFFSET (8 / 8)
24 #define SCE_AES_PRV_CCM_NLEN_MIN_VALUE (56 / 8)
25
26 #define HW_SCE_AES_CCM_B0_TLEN_32BITS_CFG (0x00000000)
27 #define HW_SCE_AES_CCM_B0_TLEN_48BITS_CFG (0x00000001)
28 #define HW_SCE_AES_CCM_B0_TLEN_64BITS_CFG (0x00000002)
29 #define HW_SCE_AES_CCM_B0_TLEN_80BITS_CFG (0x00000003)
30 #define HW_SCE_AES_CCM_B0_TLEN_96BITS_CFG (0x00000004)
31 #define HW_SCE_AES_CCM_B0_TLEN_112BITS_CFG (0x00000005)
32 #define HW_SCE_AES_CCM_B0_TLEN_128BITS_CFG (0x00000006)
33
34 #define HW_SCE_AES_CCM_B0_NLEN_56BITS_CFG (0x00000000)
35 #define HW_SCE_AES_CCM_B0_NLEN_64BITS_CFG (0x00000001)
36 #define HW_SCE_AES_CCM_B0_NLEN_72BITS_CFG (0x00000002)
37 #define HW_SCE_AES_CCM_B0_NLEN_80BITS_CFG (0x00000003)
38 #define HW_SCE_AES_CCM_B0_NLEN_88BITS_CFG (0x00000004)
39 #define HW_SCE_AES_CCM_B0_NLEN_96BITS_CFG (0x00000005)
40 #define HW_SCE_AES_CCM_B0_NLEN_104BITS_CFG (0x00000006)
41
42 #define PARAM_B0_PLEN_3BITS_MSB_OFFSET (29)
43 #define PARAM_B0_ALEN_3BITS_MSB_OFFSET (13)
44 #define PARAM_B0_B127_B96_MASK (0x0007FFF8)
45 #define PARAM_B0_B95_B64_MASK (0x00770000)
46 #define PARAM_B0_B63_B32_MASK (0x00000007)
47 #define PARAM_B0_B31_B0_MASK (0xFFFFFFF8)
48 #define PARAM_B0_B95_B64_TLEN_OFFSET (16)
49 #define PARAM_B0_B95_B64_NLEN_OFFSET (20)
50
51 #define HW_AES_BLOCK_LAST_WORD (3)
52
53 /***********************************************************************************************************************
54 * Typedef definitions
55 ***********************************************************************************************************************/
56 typedef struct B0_param
57 {
58 uint32_t B31_B0; /*!< B0[31:30]: bitfield of Plen */
59 uint32_t B63_B32; /*!< B0[63:32]: bitfield of Plen */
60 uint32_t B95_B64; /*!< B0[66:64]: bitfield of Plen, B0[79:67]: reserved, B0[82:80]: bitfield of Tlen,
61 * B0[83:83]: reserved, B0[86:84]: bitfield of Nlen, B0[95:87]: reserved */
62 uint32_t B127_B96; /*!< B0[114:96]: bitfield of Alen, B0[127:115]: reserved */
63 } st_AES_Ccm_B0_param_t;
64
65 /***********************************************************************************************************************
66 * Imported global variables and functions
67 ***********************************************************************************************************************/
68
69 /***********************************************************************************************************************
70 * Exported global variables
71 ***********************************************************************************************************************/
72
73 /***********************************************************************************************************************
74 * Private variables and functions
75 ***********************************************************************************************************************/
76 static uint32_t const g_aes_ccm_tlen_cfg_lut[] =
77 {
78 HW_SCE_AES_CCM_B0_TLEN_32BITS_CFG, HW_SCE_AES_CCM_B0_TLEN_48BITS_CFG, HW_SCE_AES_CCM_B0_TLEN_64BITS_CFG,
79 HW_SCE_AES_CCM_B0_TLEN_80BITS_CFG, HW_SCE_AES_CCM_B0_TLEN_96BITS_CFG, HW_SCE_AES_CCM_B0_TLEN_112BITS_CFG,
80 HW_SCE_AES_CCM_B0_TLEN_128BITS_CFG
81 };
82
83 static uint32_t const g_aes_ccm_nlen_cfg_lut[] =
84 {
85 HW_SCE_AES_CCM_B0_NLEN_56BITS_CFG, HW_SCE_AES_CCM_B0_NLEN_64BITS_CFG, HW_SCE_AES_CCM_B0_NLEN_72BITS_CFG,
86 HW_SCE_AES_CCM_B0_NLEN_80BITS_CFG, HW_SCE_AES_CCM_B0_NLEN_88BITS_CFG, HW_SCE_AES_CCM_B0_NLEN_96BITS_CFG,
87 HW_SCE_AES_CCM_B0_NLEN_104BITS_CFG
88 };
89
90 static void hw_aes_ccm_calculate_b0(st_AES_Ccm_B0_param_t * Out_ParamB0,
91 uint32_t plen,
92 uint32_t alen,
93 uint32_t tlen,
94 uint32_t nlen);
95 static void hw_aes_ccm_encrypt_init(uint32_t indata_cmd);
96
97 static void aes_block_swap_endian(uint32_t * InOut_Block, uint32_t MaxBlock);
98
99 /***********************************************************************************************************************
100 * Global variables and functions
101 ***********************************************************************************************************************/
102 void hw_aes_ccm_decrypt_init(uint32_t indata_cmd);
103 void HW_SCE_AesCcmEncryptKeyOperation(uint32_t * InData_KeyIndex,
104 uint32_t InData_KeyType,
105 uint32_t * InData_IV,
106 uint32_t InData_IVLength);
107 void HW_SCE_AesCcmDecryptKeyOperation(uint32_t * InData_KeyIndex,
108 uint32_t InData_KeyType,
109 uint32_t * InData_IV,
110 uint32_t InData_IVLength);
111 void HW_SCE_AesCcmEncryptCounterGenerate(uint32_t InData_TextLength,
112 uint32_t InData_Hdrlen,
113 uint32_t InData_MacLength,
114 uint32_t InData_IVLength);
115 void HW_SCE_AesCcmDecryptCounterGenerate(uint32_t InData_TextLength,
116 uint32_t InData_Hdrlen,
117 uint32_t InData_MacLength,
118 uint32_t InData_IVLength);
119 void HW_SCE_AesCcmEncryptInputAssociatedData(uint32_t * InData_Header, uint32_t InData_Hdrlen);
120 void HW_SCE_AesCcmDecryptInputAssociatedData(uint32_t * InData_Header, uint32_t InData_Hdrlen);
121 void HW_SCE_AesCcmEncryptPlaintextInputInit();
122 void HW_SCE_AesCcmEncryptGenerateTag(uint32_t * OutData_MAC);
123
124 /***********************************************************************************************************************
125 * @brief Swap byte-order of an AES block
126 *
127 * @param[in,out] InOut_Block pointer to blocks need to convert endianess
128 * @param[in] MaxBlock number of block need to convert
129 ***********************************************************************************************************************/
aes_block_swap_endian(uint32_t * InOut_Block,uint32_t MaxBlock)130 static void aes_block_swap_endian (uint32_t * InOut_Block, uint32_t MaxBlock)
131 {
132 uint32_t aes_block_tmp[SIZE_AES_BLOCK_BYTES / 4];
133 uint32_t * InBlock = InOut_Block;
134 uint32_t block_cnt;
135
136 for (block_cnt = 0; block_cnt < MaxBlock; block_cnt++)
137 {
138 uint32_t index;
139 for (index = 0; index <= HW_AES_BLOCK_LAST_WORD; index++)
140 {
141 aes_block_tmp[HW_AES_BLOCK_LAST_WORD - index] = __REV(*(InBlock + index));
142 }
143
144 memcpy(InBlock, aes_block_tmp, SIZE_AES_BLOCK_BYTES);
145 InBlock += 4;
146 }
147 }
148
149 /***********************************************************************************************************************
150 * @brief Implement common operation, input key and IV for encryption
151 *
152 * @param[in] InData_KeyIndex pointer to key to be input
153 * @param[in] InData_KeyType to determine which key type is input
154 * @param[in] InData_IV pointer to IV to be input
155 * @param[in] InData_IVLength to determine IV length in byte to be input
156 ***********************************************************************************************************************/
HW_SCE_AesCcmEncryptKeyOperation(uint32_t * InData_KeyIndex,uint32_t InData_KeyType,uint32_t * InData_IV,uint32_t InData_IVLength)157 void HW_SCE_AesCcmEncryptKeyOperation (uint32_t * InData_KeyIndex,
158 uint32_t InData_KeyType,
159 uint32_t * InData_IV,
160 uint32_t InData_IVLength)
161 {
162 uint8_t InData_IV_tmp[SIZE_AES_BLOCK_BYTES] = {0};
163
164 /* SCE AES hardware configuration for key operation */
165 if (SIZE_AES_128BIT_KEYLEN_BYTES == InData_KeyType)
166 {
167 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_128_ENC_COMMON_INIT);
168 }
169 else if (SIZE_AES_192BIT_KEYLEN_BYTES == InData_KeyType)
170 {
171 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_192_ENC_COMMON_INIT);
172 }
173 else
174 {
175 /* Input key is 256bits length */
176 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_256_ENC_COMMON_INIT);
177 }
178
179 /* Key input */
180 hw_aes_set_key((uint8_t *) InData_KeyIndex, InData_KeyType);
181
182 /* IV input */
183 memcpy(&InData_IV_tmp[SIZE_AES_BLOCK_BYTES - InData_IVLength], InData_IV, InData_IVLength);
184 hw_aes_set_iv(InData_IV_tmp);
185 }
186
187 /***********************************************************************************************************************
188 * @brief Implement common operation, input key and IV for decryption
189 *
190 * @param[in] InData_KeyIndex pointer to key to be input
191 * @param[in] InData_KeyType to determine which key type is input
192 * @param[in] InData_IV pointer to IV to be input
193 * @param[in] InData_IVLength to determine IV length in byte to be input
194 ***********************************************************************************************************************/
HW_SCE_AesCcmDecryptKeyOperation(uint32_t * InData_KeyIndex,uint32_t InData_KeyType,uint32_t * InData_IV,uint32_t InData_IVLength)195 void HW_SCE_AesCcmDecryptKeyOperation (uint32_t * InData_KeyIndex,
196 uint32_t InData_KeyType,
197 uint32_t * InData_IV,
198 uint32_t InData_IVLength)
199 {
200 uint8_t InData_IV_tmp[SIZE_AES_BLOCK_BYTES] = {0};
201
202 /* SCE AES hardware configuration for key operation */
203 if (SIZE_AES_128BIT_KEYLEN_BYTES == InData_KeyType)
204 {
205 hw_aes_ccm_decrypt_init(SCE_AES_IN_DATA_CMD_CCM_128_DEC_COMMON_INIT);
206 }
207 else if (SIZE_AES_192BIT_KEYLEN_BYTES == InData_KeyType)
208 {
209 hw_aes_ccm_decrypt_init(SCE_AES_IN_DATA_CMD_CCM_192_DEC_COMMON_INIT);
210 }
211 else
212 {
213 /* Input key is 256bits length */
214 hw_aes_ccm_decrypt_init(SCE_AES_IN_DATA_CMD_CCM_256_DEC_COMMON_INIT);
215 }
216
217 /* Key input */
218 hw_aes_set_key((uint8_t *) InData_KeyIndex, InData_KeyType);
219
220 /* IV input */
221 memcpy(&InData_IV_tmp[SIZE_AES_BLOCK_BYTES - InData_IVLength], InData_IV, InData_IVLength);
222 hw_aes_set_iv(InData_IV_tmp);
223 }
224
225 /***********************************************************************************************************************
226 * @brief Implement common operation to generate counter and input parameter B0
227 *
228 * @param[in] InData_TextLength length in byte of payload data to be input
229 * @param[in] InData_Hdrlen length in byte of associated data to be input
230 * @param[in] InData_MacLength length in byte of tag need to be generated
231 * @param[in] InData_IVLength length in byte of IV to be input
232 ***********************************************************************************************************************/
HW_SCE_AesCcmEncryptCounterGenerate(uint32_t InData_TextLength,uint32_t InData_Hdrlen,uint32_t InData_MacLength,uint32_t InData_IVLength)233 void HW_SCE_AesCcmEncryptCounterGenerate (uint32_t InData_TextLength,
234 uint32_t InData_Hdrlen,
235 uint32_t InData_MacLength,
236 uint32_t InData_IVLength)
237 {
238 st_AES_Ccm_B0_param_t param_B0;
239
240 /* SCE AES hardware configuration for parameter B0 input */
241 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_ENC_COUNTER_GENERATE);
242
243 /* Apply formatting function for parameter B0 */
244 hw_aes_ccm_calculate_b0(¶m_B0, InData_TextLength, InData_Hdrlen, InData_MacLength, InData_IVLength);
245
246 /* Swap endian to have parameter B0 with big endian */
247 aes_block_swap_endian((uint32_t *) ¶m_B0, sizeof(st_AES_Ccm_B0_param_t) / SIZE_AES_BLOCK_BYTES);
248
249 /* Input parameter B0 and apply counter generation function */
250 hw_aes_ccm_mode_start((uint8_t *) ¶m_B0, NULL, sizeof(st_AES_Ccm_B0_param_t) / SIZE_AES_BLOCK_BYTES);
251 }
252
253 /***********************************************************************************************************************
254 * @brief Implement common operation to generate counter and input parameter B0
255 *
256 * @param[in] InData_TextLength length in byte of payload data to be input
257 * @param[in] InData_Hdrlen length in byte of associated data to be input
258 * @param[in] InData_MacLength length in byte of tag need to be generated
259 * @param[in] InData_IVLength length in byte of IV to be input
260 ***********************************************************************************************************************/
HW_SCE_AesCcmDecryptCounterGenerate(uint32_t InData_TextLength,uint32_t InData_Hdrlen,uint32_t InData_MacLength,uint32_t InData_IVLength)261 void HW_SCE_AesCcmDecryptCounterGenerate (uint32_t InData_TextLength,
262 uint32_t InData_Hdrlen,
263 uint32_t InData_MacLength,
264 uint32_t InData_IVLength)
265 {
266 st_AES_Ccm_B0_param_t param_B0;
267
268 /* SCE AES hardware configuration for parameter B0 input */
269 hw_aes_ccm_decrypt_init(SCE_AES_IN_DATA_CMD_CCM_DEC_COUNTER_GENERATE);
270
271 /* Apply formatting function for parameter B0 */
272 hw_aes_ccm_calculate_b0(¶m_B0, InData_TextLength, InData_Hdrlen, InData_MacLength, InData_IVLength);
273
274 /* Swap endian to have parameter B0 with big endian */
275 aes_block_swap_endian((uint32_t *) ¶m_B0, sizeof(st_AES_Ccm_B0_param_t) / SIZE_AES_BLOCK_BYTES);
276
277 /* Input parameter B0 and apply counter generation function */
278 hw_aes_ccm_mode_start((uint8_t *) ¶m_B0, NULL, sizeof(st_AES_Ccm_B0_param_t) / SIZE_AES_BLOCK_BYTES);
279 }
280
281 /***********************************************************************************************************************
282 * @brief Implement common operation and input associated data
283 *
284 * @param[in] InData_Header pointer to associated data string need to be input
285 * @param[in] InData_Hdrlen length in byte of associated data string
286 ***********************************************************************************************************************/
HW_SCE_AesCcmEncryptInputAssociatedData(uint32_t * InData_Header,uint32_t InData_Hdrlen)287 void HW_SCE_AesCcmEncryptInputAssociatedData (uint32_t * InData_Header, uint32_t InData_Hdrlen)
288 {
289 /* SCE AES hardware configuration for associated data input */
290 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_ENC_FORMAT_A_DATA);
291
292 /* Input associated data */
293 uint8_t * p_a_data = (uint8_t *) InData_Header;
294 uint32_t block_cnt = HW_AES_DATA_FIT_TO_BLOCK_SIZE(InData_Hdrlen);
295 if (0 < block_cnt)
296 {
297 hw_aes_ccm_mode_start((uint8_t *) InData_Header, NULL, block_cnt / SIZE_AES_BLOCK_BYTES);
298 p_a_data += block_cnt;
299 }
300
301 uint32_t remain_data = HW_AES_DATA_GET_LAST_REMAINS(InData_Hdrlen);
302 if (0 < remain_data)
303 {
304 uint32_t a_data_tmp[SIZE_AES_BLOCK_BYTES / sizeof(uint32_t)] = {0};
305 memcpy(a_data_tmp, p_a_data, remain_data);
306 hw_aes_ccm_mode_start((uint8_t *) a_data_tmp, NULL, SIZE_AES_BLOCK_BYTES / SIZE_AES_BLOCK_BYTES);
307 }
308 }
309
310 /***********************************************************************************************************************
311 * @brief Implement common operation and input associated data
312 *
313 * @param[in] InData_Header pointer to associated data string need to be input
314 * @param[in] InData_Hdrlen length in byte of associated data string
315 ***********************************************************************************************************************/
HW_SCE_AesCcmDecryptInputAssociatedData(uint32_t * InData_Header,uint32_t InData_Hdrlen)316 void HW_SCE_AesCcmDecryptInputAssociatedData (uint32_t * InData_Header, uint32_t InData_Hdrlen)
317 {
318 /* SCE AES hardware configuration for associated data input */
319 hw_aes_ccm_decrypt_init(SCE_AES_IN_DATA_CMD_CCM_DEC_FORMAT_A_DATA);
320
321 /* Input associated data */
322 uint8_t * p_a_data = (uint8_t *) InData_Header;
323 uint32_t block_cnt = HW_AES_DATA_FIT_TO_BLOCK_SIZE(InData_Hdrlen);
324 if (0 < block_cnt)
325 {
326 hw_aes_ccm_mode_start((uint8_t *) InData_Header, NULL, block_cnt / SIZE_AES_BLOCK_BYTES);
327 p_a_data += block_cnt;
328 }
329
330 uint32_t remain_data = HW_AES_DATA_GET_LAST_REMAINS(InData_Hdrlen);
331 if (0 < remain_data)
332 {
333 uint32_t a_data_tmp[SIZE_AES_BLOCK_BYTES / sizeof(uint32_t)] = {0};
334 memcpy(a_data_tmp, p_a_data, remain_data);
335 hw_aes_ccm_mode_start((uint8_t *) a_data_tmp, NULL, SIZE_AES_BLOCK_BYTES / SIZE_AES_BLOCK_BYTES);
336 }
337 }
338
339 /***********************************************************************************************************************
340 * @brief Implement common operation before in put plaintext to be encrypted
341 *
342 ***********************************************************************************************************************/
HW_SCE_AesCcmEncryptPlaintextInputInit()343 void HW_SCE_AesCcmEncryptPlaintextInputInit ()
344 {
345 /* Hardware config to input plaintext */
346 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_ENC_CTR_ENCRYPT);
347 }
348
349 /***********************************************************************************************************************
350 * @brief Implement common operations and generate Tag
351 *
352 * @param[out] OutData_MAC pointer to memory to output Tag
353 ***********************************************************************************************************************/
HW_SCE_AesCcmEncryptGenerateTag(uint32_t * OutData_MAC)354 void HW_SCE_AesCcmEncryptGenerateTag (uint32_t * OutData_MAC)
355 {
356 uint32_t dummy_data[SIZE_AES_BLOCK_BYTES / sizeof(uint32_t)] = {0};
357
358 /* Generate Tag */
359 hw_aes_ccm_encrypt_init(SCE_AES_IN_DATA_CMD_CCM_ENC_GENERATE_TAG);
360 hw_aes_ccm_mode_start((uint8_t *) dummy_data, (uint8_t *) OutData_MAC, sizeof(dummy_data) / SIZE_AES_BLOCK_BYTES);
361 }
362
363 /***********************************************************************************************************************
364 * @brief Assign value for parameter B0 to setting HW SCE AES CCM mode, output parameter B0 as little endian order
365 *
366 * @param[in,out] Out_ParamB0 pointer to parameter B0 block
367 * @param[in] plen payload length in byte
368 * @param[in] alen associated length in byte
369 * @param[in] tlen tag length in byte
370 * @param[in] nlen nonce length in byte
371 ***********************************************************************************************************************/
hw_aes_ccm_calculate_b0(st_AES_Ccm_B0_param_t * Out_ParamB0,uint32_t plen,uint32_t alen,uint32_t tlen,uint32_t nlen)372 static void hw_aes_ccm_calculate_b0 (st_AES_Ccm_B0_param_t * Out_ParamB0,
373 uint32_t plen,
374 uint32_t alen,
375 uint32_t tlen,
376 uint32_t nlen)
377 {
378 uint32_t index;
379 memset(Out_ParamB0, 0, sizeof(st_AES_Ccm_B0_param_t));
380
381 /* Assign Plaintext length Bit */
382 Out_ParamB0->B31_B0 = HW_BYTE_TO_BIT_LENGTH(plen) & PARAM_B0_B31_B0_MASK;
383 Out_ParamB0->B63_B32 = (plen >> PARAM_B0_PLEN_3BITS_MSB_OFFSET) & PARAM_B0_B63_B32_MASK;
384
385 /* Assign Tag length Bit */
386 uint32_t b95_b64 = 0;
387 index = (tlen - SCE_AES_PRV_CCM_TLEN_MIN_VALUE) / SCE_AES_PRV_CCM_TLEN_VALUE_OFFSET;
388 b95_b64 = g_aes_ccm_tlen_cfg_lut[index] << PARAM_B0_B95_B64_TLEN_OFFSET;
389
390 /* Assign Nonce length Bit */
391 index = (nlen - SCE_AES_PRV_CCM_NLEN_MIN_VALUE) / SCE_AES_PRV_CCM_NLEN_VALUE_OFFSET;
392 b95_b64 |= g_aes_ccm_nlen_cfg_lut[index] << PARAM_B0_B95_B64_NLEN_OFFSET;
393 Out_ParamB0->B95_B64 = b95_b64 & PARAM_B0_B95_B64_MASK;
394
395 /* Assign Non-Encryption word length Bit */
396 Out_ParamB0->B127_B96 = HW_BYTE_TO_BIT_LENGTH(alen) & PARAM_B0_B127_B96_MASK;
397 }
398
399 /***********************************************************************************************************************
400 * @brief Configure AES module to run CCM mode encryption operation
401 *
402 * @param[in] indata_cmd to be determined which operation need to be initial
403 **********************************************************************************************************************/
hw_aes_ccm_encrypt_init(uint32_t indata_cmd)404 static void hw_aes_ccm_encrypt_init (uint32_t indata_cmd)
405 {
406 switch (indata_cmd)
407 {
408 case SCE_AES_IN_DATA_CMD_CCM_128_ENC_COMMON_INIT:
409 {
410 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
411 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_128_ENC;
412 break;
413 }
414
415 case SCE_AES_IN_DATA_CMD_CCM_192_ENC_COMMON_INIT:
416 {
417 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
418 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_192_ENC;
419 break;
420 }
421
422 case SCE_AES_IN_DATA_CMD_CCM_256_ENC_COMMON_INIT:
423 {
424 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
425 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_256_ENC;
426 break;
427 }
428
429 case SCE_AES_IN_DATA_CMD_CCM_ENC_COUNTER_GENERATE:
430 {
431 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
432 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_1 |
433 R_AES_AESDCNTL_FIST_SET |
434 R_AES_AESDCNTL_NEW_KEY_SET;
435 break;
436 }
437
438 case SCE_AES_IN_DATA_CMD_CCM_ENC_FORMAT_A_DATA:
439 {
440 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
441 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_3;
442 break;
443 }
444
445 case SCE_AES_IN_DATA_CMD_CCM_ENC_CTR_ENCRYPT:
446 {
447 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
448 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_4;
449 break;
450 }
451
452 case SCE_AES_IN_DATA_CMD_CCM_ENC_GENERATE_TAG:
453 {
454 R_AES_B->AESDCNTH = R_AES_AESDCNTH_EXECUTE_TAG_CALCULATION;
455 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_5;
456 break;
457 }
458
459 default:
460 {
461 break;
462 }
463 }
464 }
465
466 /***********************************************************************************************************************
467 * @brief Configure AES module to run CCM mode decryption operation
468 *
469 * @param[in] indata_cmd to be determined which operation need to be initial
470 **********************************************************************************************************************/
hw_aes_ccm_decrypt_init(uint32_t indata_cmd)471 void hw_aes_ccm_decrypt_init (uint32_t indata_cmd)
472 {
473 switch (indata_cmd)
474 {
475 case SCE_AES_IN_DATA_CMD_CCM_128_DEC_COMMON_INIT:
476 {
477 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
478 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_128_DEC;
479 break;
480 }
481
482 case SCE_AES_IN_DATA_CMD_CCM_192_DEC_COMMON_INIT:
483 {
484 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
485 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_192_DEC;
486 break;
487 }
488
489 case SCE_AES_IN_DATA_CMD_CCM_256_DEC_COMMON_INIT:
490 {
491 R_AES_B->AESCNTH = R_AES_AESCNTH_INIT;
492 R_AES_B->AESCNTL = R_AES_AESCNTL_CCM_256_DEC;
493 break;
494 }
495
496 case SCE_AES_IN_DATA_CMD_CCM_DEC_COUNTER_GENERATE:
497 {
498 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
499 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_1 |
500 R_AES_AESDCNTL_FIST_SET |
501 R_AES_AESDCNTL_NEW_KEY_SET;
502 break;
503 }
504
505 case SCE_AES_IN_DATA_CMD_CCM_DEC_CTR_DECRYPT:
506 {
507 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
508 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_2;
509 break;
510 }
511
512 case SCE_AES_IN_DATA_CMD_CCM_DEC_FORMAT_A_DATA:
513 {
514 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
515 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_3;
516 break;
517 }
518
519 case SCE_AES_IN_DATA_CMD_CCM_DEC_FORMAT_PAYLOAD:
520 {
521 R_AES_B->AESDCNTH = R_AES_AESDCNTH_NOT_EXECUTE_TAG_CALCULATION;
522 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_4;
523 break;
524 }
525
526 case SCE_AES_IN_DATA_CMD_CCM_DEC_GENERATE_TAG:
527 {
528 R_AES_B->AESDCNTH = R_AES_AESDCNTH_EXECUTE_TAG_CALCULATION;
529 R_AES_B->AESDCNTL = R_AES_AESDCNTL_ATTR_CCM_MODE_6;
530 break;
531 }
532
533 default:
534 {
535 break;
536 }
537 }
538 }
539