1 /***************************************************************************//**
2 * \file cy_cryptolite_aes_ccm.c
3 * \version 2.20
4 *
5 * \brief
6 *  Provides API implementation of the Cryptolite AES CCM PDL driver.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright (c) 2023, Cypress Semiconductor Corporation (an Infineon company) or
11 * an affiliate of Cypress Semiconductor Corporation.
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 *    http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26 
27 #include "cy_device.h"
28 
29 #if defined (CY_IP_MXCRYPTOLITE)
30 
31 #include "cy_cryptolite_aes_ccm.h"
32 
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36 
37 
38 #if (CRYPTOLITE_AES_PRESENT == 1)
39 #if defined(CY_CRYPTOLITE_CFG_CIPHER_MODE_CCM)
40 
41 /*******************************************************************************
42 * Function Name: Cy_Cryptolite_Aes_Ccm_Init
43 ****************************************************************************//*
44 *
45 * Performs an AES CCM Init operation.
46 *
47 * \param base
48 * The pointer to the CRYPTOLITE instance.
49 *
50 * \param aesCcm_buffer The buffers should be a SAHB mapped addresses.
51 * The pointer to the memory buffers storage.
52 *
53 * \param aesState
54 * The pointer to the AES CCM state structure allocated by the user. The user
55 * must not modify anything in this structure.
56 *
57 * \return
58 * \ref cy_en_cryptolite_status_t
59 *
60 *******************************************************************************/
61 
Cy_Cryptolite_Aes_Ccm_Init(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_ccm_buffers_t * aesCcm_buffer,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)62 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Init(CRYPTOLITE_Type *base,
63                                             cy_stc_cryptolite_aes_ccm_buffers_t * aesCcm_buffer, cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
64 {
65     /* Input parameters verification */
66     if ((NULL == base) || (NULL == aesCcm_buffer) || (NULL == aesCcmState))
67     {
68         return CY_CRYPTOLITE_BAD_PARAMS;
69     }
70 
71     Cy_Cryptolite_Vu_memset((void *)aesCcm_buffer, 0u, sizeof(cy_stc_cryptolite_aes_ccm_buffers_t));
72     Cy_Cryptolite_Vu_memset((void *)aesCcmState, 0u, sizeof(cy_stc_cryptolite_aes_ccm_state_t));
73 
74     aesCcmState->aesCbcMacState.buffers = &aesCcm_buffer->aesCbcMac_buffer;
75     aesCcmState->aesCtrState.buffers = &aesCcm_buffer->aesCtr_buffer;
76     aesCcmState->temp = aesCcm_buffer->temp_buffer;
77     aesCcmState->ctr = aesCcm_buffer->ctr;
78     aesCcmState->y = aesCcm_buffer->y;
79 
80     return CY_CRYPTOLITE_SUCCESS;
81 }
82 
83 
84 
85 /*******************************************************************************
86 * Function Name: Cy_Cryptolite_Aes_Ccm_SetKey
87 ****************************************************************************//*
88 *
89 * Sets AES CCM Key for the operation.
90 *
91 * \param base
92 * The pointer to the CRYPTOLITE instance.
93 *
94 * \param key
95 * The pointer to the CCM key.
96 *
97 * \param aesCcmState
98 * The pointer to the AES CCM state structure allocated by the user. The user
99 * must not modify anything in this structure.
100 *
101 *
102 * \return
103 * \ref cy_en_cryptolite_status_t
104 *
105 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_SetKey(CRYPTOLITE_Type * base,uint8_t const * key,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)106 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_SetKey(CRYPTOLITE_Type *base,
107                                             uint8_t const *key, cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
108 {
109     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
110     uint8_t *keyRemap;
111 
112     /* Input parameters verification */
113     if ((NULL == base) || (NULL == key) || (NULL == aesCcmState))
114     {
115         return CY_CRYPTOLITE_BAD_PARAMS;
116     }
117 
118     keyRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(key);
119 
120     // Sets the AES Key for the CBC MAC operation
121     status = Cy_Cryptolite_Aes_Init(base, keyRemap, &aesCcmState->aesCbcMacState, aesCcmState->aesCbcMacState.buffers);
122     if(CY_CRYPTOLITE_SUCCESS != status)
123     {
124         return status;
125     }
126 
127     // Sets the AES Key for the CTR operation
128     status = Cy_Cryptolite_Aes_Init(base, keyRemap, &aesCcmState->aesCtrState, aesCcmState->aesCtrState.buffers);
129     if(CY_CRYPTOLITE_SUCCESS != status)
130     {
131         return status;
132     }
133 
134     return CY_CRYPTOLITE_SUCCESS;
135 }
136 
137 
138 
139 /*******************************************************************************
140 * Function Name: Cy_Cryptolite_Aes_Ccm_Initial_Block
141 ****************************************************************************//*
142 *
143 *  Performs the Initial CCM calculation.
144 *
145 * \param base
146 * The pointer to the CRYPTOLITE instance.
147 *
148 * \param aesCcmState
149 * The pointer to the AES CCM state structure allocated by the user. The user
150 * must not modify anything in this structure.
151 *
152 * \return
153 * \ref cy_en_cryptolite_status_t
154 *
155 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Initial_Block(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)156 static cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Initial_Block(CRYPTOLITE_Type *base,
157                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
158 {
159     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
160     uint32_t size_left = 0u;
161 
162     if(aesCcmState->isIvSet == false || aesCcmState->isLengthSet == false)
163     {
164         return CY_CRYPTOLITE_SUCCESS;
165     }
166 
167     status = Cy_Cryptolite_Aes_CbcMac_Setup(base, &aesCcmState->aesCbcMacState);
168     if(CY_CRYPTOLITE_SUCCESS != status)
169     {
170         return status;
171     }
172 
173     aesCcmState->y[0] |= (uint8_t)(aesCcmState->aadLength > 0u) << 6u;
174     aesCcmState->y[0] |= ((aesCcmState->tagLength - 2u) / 2u) << 3u;
175     aesCcmState->y[0] |= (uint8_t)aesCcmState->L - 1u;
176 
177     size_left = aesCcmState->textLength;
178     for (uint32_t i = 0u; i < aesCcmState->L; i++)
179     {
180         aesCcmState->y[15u - i] = (uint8_t)size_left & 0xffu ;
181          size_left >>= 8u;
182     }
183 
184     // Performs the CBC MAC update operation for the Initial Block data formated with flags and length
185     status = Cy_Cryptolite_Aes_CbcMac_Update(base, CY_CRYPTOLITE_AES_BLOCK_SIZE, aesCcmState->y, &aesCcmState->aesCbcMacState);
186     if(CY_CRYPTOLITE_SUCCESS != status)
187     {
188         return status;
189     }
190 
191     if (aesCcmState->aadLength > 0u)
192     {
193         aesCcmState->temp[0] = (uint8_t)(aesCcmState->aadLength >> 8u) & 0xffu;
194         aesCcmState->temp[1] = (uint8_t)(aesCcmState->aadLength & 0xffu);
195         status = Cy_Cryptolite_Aes_CbcMac_Update(base, 2u, aesCcmState->temp, &aesCcmState->aesCbcMacState);
196 
197         if(CY_CRYPTOLITE_SUCCESS != status)
198         {
199             return status;
200         }
201 
202         aesCcmState->aadLengthProcessed += 2u;
203     }
204 
205     return CY_CRYPTOLITE_SUCCESS;
206 }
207 
208 /*******************************************************************************
209 * Function Name: Cy_Cryptolite_Aes_Ccm_Set_Length
210 ****************************************************************************//*
211 *
212 * Sets the length for Additional authentication data, plain text and Tag for AES CCM operation.
213 *
214 * \param base
215 * The pointer to the CRYPTOLITE instance.
216 *
217 * \param aadSize
218 * The Size of the Additional Authentication Data.
219 *
220 * \param textSize
221 * The Size of the Text.
222 *
223 * \param tagLength
224 * The Size of the Tag.
225 *
226 * \param aesCcmState
227 * The pointer to the AES CCM state structure allocated by the user. The user
228 * must not modify anything in this structure.
229 *
230 *
231 * \return
232 * \ref cy_en_cryptolite_status_t
233 *
234 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Set_Length(CRYPTOLITE_Type * base,uint32_t aadSize,uint32_t textSize,uint32_t tagLength,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)235 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Set_Length(CRYPTOLITE_Type *base,
236                                             uint32_t aadSize,  uint32_t textSize, uint32_t tagLength,
237                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
238 {
239     /* Input parameters verification */
240     if ((NULL == base) || (NULL == aesCcmState))
241     {
242         return CY_CRYPTOLITE_BAD_PARAMS;
243     }
244 
245     if (tagLength < 3u || tagLength > CY_CRYPTOLITE_AES_BLOCK_SIZE || (tagLength % 2u != 0u))
246     {
247         return CY_CRYPTOLITE_BAD_PARAMS;
248     }
249 
250     if( aadSize >= 0xFF00UL )
251     {
252         return CY_CRYPTOLITE_BAD_PARAMS;
253     }
254 
255     aesCcmState->textLength = textSize;
256     aesCcmState->aadLength = aadSize;
257     aesCcmState->tagLength = (uint8_t)tagLength;
258     aesCcmState->aadLengthProcessed = 0u;
259     aesCcmState->isAadProcessed = false;
260     aesCcmState->isLengthSet = true;
261 
262     return Cy_Cryptolite_Aes_Ccm_Initial_Block(base, aesCcmState);
263 }
264 
265 
266 /*******************************************************************************
267 * Function Name: Cy_Cryptolite_Aes_Ccm_Start
268 ****************************************************************************//*
269 *
270 * Function to set IV for the AES CCM operation.
271 *
272 * \param base
273 * The pointer to the CRYPTOLITE instance.
274 *
275 * \param dirMode
276 * Can be \ref CY_CRYPTOLITE_ENCRYPT or \ref CY_CRYPTOLITE_DECRYPT
277 * (\ref cy_en_cryptolite_dir_mode_t)
278 *
279 * \param ivSize
280 * The size of the IV.
281 *
282 * \param iv
283 * The pointer to the IV.
284 *
285 * \param aesCcmState
286 * The pointer to the AES CCM state structure allocated by the user. The user
287 * must not modify anything in this structure.
288 *
289 * \return
290 * \ref cy_en_cryptolite_status_t
291 *
292 *******************************************************************************/
293 
Cy_Cryptolite_Aes_Ccm_Start(CRYPTOLITE_Type * base,cy_en_cryptolite_dir_mode_t dirMode,uint32_t ivSize,uint8_t const * iv,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)294 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Start(CRYPTOLITE_Type *base,
295                                             cy_en_cryptolite_dir_mode_t dirMode,
296                                              uint32_t ivSize, uint8_t const * iv,
297                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
298 {
299     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
300     uint8_t *ivRemap;
301 
302     /* Input parameters verification */
303     if ((NULL == base) || (NULL == iv) || (NULL == aesCcmState))
304     {
305         return status;
306     }
307 
308     if (ivSize < 7u || ivSize > 13u)
309     {
310         return status;
311     }
312 
313     ivRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(iv);
314 
315     aesCcmState->dirMode = dirMode;
316     aesCcmState->L = CY_CRYPTOLITE_AES_BLOCK_SIZE - ivSize - 1u;
317     aesCcmState->isIvSet = true;
318 
319     Cy_Cryptolite_Vu_memset (aesCcmState->ctr, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
320     aesCcmState->ctr[0] = (uint8_t)aesCcmState->L - 1u;
321     aesCcmState->ctr[15] = 1u;
322     Cy_Cryptolite_Vu_memcpy((void*)&aesCcmState->ctr[1], (void*)ivRemap, (uint16_t)ivSize);
323 
324     Cy_Cryptolite_Vu_memset (aesCcmState->y, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
325     Cy_Cryptolite_Vu_memcpy((void*)&aesCcmState->y[1], (void*)ivRemap, (uint16_t)ivSize);
326 
327 
328     // Sets the IV for the AES CTR operation
329     status = Cy_Cryptolite_Aes_Ctr_Setup(base, &aesCcmState->aesCtrState);
330     if(CY_CRYPTOLITE_SUCCESS != status)
331     {
332         return status;
333     }
334 
335     status = Cy_Cryptolite_Aes_Ctr_Set_IV(base, aesCcmState->ctr, &aesCcmState->aesCtrState);
336     if(CY_CRYPTOLITE_SUCCESS != status)
337     {
338         return status;
339     }
340 
341     return Cy_Cryptolite_Aes_Ccm_Initial_Block(base, aesCcmState);
342 }
343 
344 
345 
346 /*******************************************************************************
347 * Function Name: Cy_Cryptolite_Aes_Ccm_Update_Aad
348 ****************************************************************************//*
349 *
350 * Performs an AES CCM update AAD Multistage  operation.
351 *
352 * \param base
353 * The pointer to the CRYPTOLITE instance.
354 *
355 * \param aadSize
356 * The size of the AAD.
357 *
358 * \param aad
359 * The pointer to a AAD.
360 *
361 * \param aesCcmState
362 * The pointer to the AES CCM state structure allocated by the user. The user
363 * must not modify anything in this structure.
364 *
365 * \return
366 * \ref cy_en_cryptolite_status_t
367 *
368 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Update_Aad(CRYPTOLITE_Type * base,uint32_t aadSize,uint8_t const * aad,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)369 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Update_Aad(CRYPTOLITE_Type *base,
370                                             uint32_t aadSize,
371                                             uint8_t const *aad,
372                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
373 
374 
375 {
376     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
377     uint8_t *aadRemap;
378 
379     if(0u == aadSize)
380     {
381         return CY_CRYPTOLITE_SUCCESS;
382     }
383 
384     if( aadSize >= 0xFF00UL )
385     {
386         return CY_CRYPTOLITE_BAD_PARAMS;
387     }
388 
389         /* Input parameters verification */
390     if ((NULL == base) || (NULL == aesCcmState) || (NULL == aad))
391     {
392         return CY_CRYPTOLITE_BAD_PARAMS;
393     }
394 
395     aadRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(aad);
396 
397     status = Cy_Cryptolite_Aes_CbcMac_Update(base, aadSize, aadRemap, &aesCcmState->aesCbcMacState);
398     if(CY_CRYPTOLITE_SUCCESS != status)
399     {
400         return status;
401     }
402 
403     aesCcmState->aadLengthProcessed += aadSize;
404 
405     return status;
406 }
407 
408 
409 /*******************************************************************************
410 * Function Name: Cy_Cryptolite_Aes_Ccm_Update
411 ****************************************************************************//*
412 *
413 * Performs an AES CCM Update Multistage update operation.
414 *
415 * \param base
416 * The pointer to the CRYPTOLITE instance.
417 *
418 * \param srcSize
419 * The size of the source block.
420 *
421 * \param dst
422 * The pointer to a destination block.
423 *
424 * \param src
425 * The pointer to a source block.
426 *
427 * \param aesCcmState
428 * The pointer to the AES CCM state structure allocated by the user. The user
429 * must not modify anything in this structure.
430 *
431 * \return
432 * \ref cy_en_cryptolite_status_t
433 *
434 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)435 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Update(CRYPTOLITE_Type *base,
436                                             uint32_t srcSize,
437                                             uint8_t *dst,
438                                             uint8_t const *src,
439                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
440 
441 
442 {
443     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
444     uint8_t *temp = NULL;
445     uint8_t *cipherText_ptr;
446     uint8_t *srcRemap;
447 
448     if(0u == srcSize)
449     {
450         return CY_CRYPTOLITE_SUCCESS;
451     }
452 
453         /* Input parameters verification */
454     if ((NULL == base) || (NULL == aesCcmState) || (NULL == dst) || (NULL == src))
455     {
456         return CY_CRYPTOLITE_BAD_PARAMS;
457     }
458 
459     temp = aesCcmState->temp;
460 
461 
462     srcRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
463 
464     Cy_Cryptolite_Vu_memset (temp, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
465 
466     if( aesCcmState->aadLengthProcessed % CY_CRYPTOLITE_AES_BLOCK_SIZE != 0u && aesCcmState->isAadProcessed == false)
467     {
468         status = Cy_Cryptolite_Aes_CbcMac_Update(base,  CY_CRYPTOLITE_AES_BLOCK_SIZE - aesCcmState->aadLengthProcessed % CY_CRYPTOLITE_AES_BLOCK_SIZE, temp, &aesCcmState->aesCbcMacState);
469         if(CY_CRYPTOLITE_SUCCESS != status)
470         {
471             return status;
472         }
473 
474         aesCcmState->isAadProcessed = true;
475     }
476 
477     status =  Cy_Cryptolite_Aes_Ctr_Update(base, srcSize, dst, srcRemap, &aesCcmState->aesCtrState);
478     if(CY_CRYPTOLITE_SUCCESS != status)
479     {
480         return status;
481     }
482 
483     if(aesCcmState->dirMode == CY_CRYPTOLITE_ENCRYPT)
484     {
485         cipherText_ptr = (uint8_t *)srcRemap;
486     }
487     else
488     {
489         cipherText_ptr = dst;
490     }
491 
492     status = Cy_Cryptolite_Aes_CbcMac_Update(base, srcSize, cipherText_ptr, &aesCcmState->aesCbcMacState);
493 
494     return status;
495 }
496 
497 
498 
499 /*******************************************************************************
500 * Function Name: Cy_Cryptolite_Aes_Ccm_Finish
501 ****************************************************************************//*
502 *
503 * Performs an AES CCM finish operation.
504 *
505 * \param base
506 * The pointer to the CRYPTOLITE instance.
507 *
508 * \param mac
509 * The pointer to the CCM Tag.
510 *
511 * \param aesCcmState
512 * The pointer to the AES CCM state structure allocated by the user. The user
513 * must not modify anything in this structure.
514 *
515 * \return
516 * \ref cy_en_cryptolite_status_t
517 *
518 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Finish(CRYPTOLITE_Type * base,uint8_t * tag,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)519 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Finish(CRYPTOLITE_Type *base, uint8_t *tag, cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
520 {
521     uint8_t *temp = NULL;
522     uint8_t y[CY_CRYPTOLITE_AES_BLOCK_SIZE] = {0u};
523     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
524 
525     /* Input parameters verification */
526     if ((NULL == base) || (NULL == aesCcmState))
527     {
528         return CY_CRYPTOLITE_BAD_PARAMS;
529     }
530 
531     temp = aesCcmState->temp;
532 
533     Cy_Cryptolite_Vu_memset (temp, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
534     Cy_Cryptolite_Vu_memset (y, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
535 
536     if (aesCcmState->textLength % CY_CRYPTOLITE_AES_BLOCK_SIZE  != 0u)
537     {
538         status = Cy_Cryptolite_Aes_CbcMac_Update(base, CY_CRYPTOLITE_AES_BLOCK_SIZE - aesCcmState->textLength % CY_CRYPTOLITE_AES_BLOCK_SIZE, temp, &aesCcmState->aesCbcMacState);
539         if(CY_CRYPTOLITE_SUCCESS != status)
540         {
541             return status;
542         }
543     }
544 
545     status = Cy_Cryptolite_Aes_CbcMac_Finish(base, temp, &aesCcmState->aesCbcMacState);
546     if(CY_CRYPTOLITE_SUCCESS != status)
547     {
548         return status;
549     }
550 
551     for (uint32_t i = 0u; i < aesCcmState->L; i++)
552     {
553         aesCcmState->aesCtrState.buffers->iv[15u-i] = 0u;
554     }
555 
556     aesCcmState->aesCtrState.unProcessedBytes = 0u;
557 
558     status = Cy_Cryptolite_Aes_Ctr_Update(base, CY_CRYPTOLITE_AES_BLOCK_SIZE,
559                                             y,
560                                             temp,
561                                             &aesCcmState->aesCtrState);
562 
563     if(CY_CRYPTOLITE_SUCCESS != status)
564     {
565         return status;
566     }
567 
568     Cy_Cryptolite_Vu_memcpy((void*)tag, (void*)y, aesCcmState->tagLength);
569     Cy_Cryptolite_Vu_memset (aesCcmState->ctr, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
570     Cy_Cryptolite_Vu_memset (aesCcmState->y, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE);
571     aesCcmState->isIvSet = false;
572     aesCcmState->isAadProcessed = false;
573 
574     return status;
575 }
576 
577 
578 
579 /*******************************************************************************
580 * Function Name: Cy_Cryptolite_Aes_Ccm
581 ****************************************************************************//*
582 *
583 * Performs an AES CCM operation.
584 * \note Cy_Cryptolite_Aes_Ccm_Init() and Cy_Cryptolite_Aes_Ccm_SetKey() should be called before calling this function.
585 *
586 * \param base
587 * The pointer to the CRYPTOLITE instance.
588 *
589 * \param dirMode
590 * Can be \ref CY_CRYPTOLITE_ENCRYPT or \ref CY_CRYPTOLITE_DECRYPT
591 * (\ref cy_en_cryptolite_dir_mode_t)
592 *
593 * \param ivSize
594 * The size of the IV.
595 *
596 * \param iv
597 * The pointer to the IV.
598 *
599 * \param aadSize
600 * The size of the AAD.
601 *
602 * \param aad
603 * The pointer to a AAD.
604 *
605 * \param srcSize
606 * The size of the source block.
607 *
608 * \param dst
609 * The pointer to a destination block.
610 *
611 * \param src
612 * The pointer to a source block.
613 *
614 * \param tagSize
615 * The size of the Tag.
616 *
617 * \param tag
618 * The pointer to the tags.
619 *
620 * \param aesCcmState
621 * The pointer to the AES CCM state structure allocated by the user. The user
622 * must not modify anything in this structure.
623 *
624 * \return
625 * \ref cy_en_cryptolite_status_t
626 *
627 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm(CRYPTOLITE_Type * base,cy_en_cryptolite_dir_mode_t dirMode,uint32_t ivSize,uint8_t const * iv,uint32_t aadSize,uint8_t const * aad,uint32_t srcSize,uint8_t * dst,uint8_t const * src,uint32_t tagSize,uint8_t * tag,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)628 static cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm(CRYPTOLITE_Type *base,
629                                             cy_en_cryptolite_dir_mode_t dirMode,
630                                             uint32_t ivSize, uint8_t const * iv,
631                                             uint32_t aadSize, uint8_t const *aad,
632                                             uint32_t srcSize, uint8_t *dst, uint8_t const *src,
633                                             uint32_t tagSize, uint8_t *tag,
634                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
635 
636 {
637     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
638     uint8_t *ivRemap;
639     uint8_t *aadRemap;
640     uint8_t *srcRemap;
641 
642      /* Input parameters verification */
643     if ((NULL == base) || ((NULL == iv) && (ivSize > 0u)) || ((NULL == aad) && (aadSize > 0u)) ||
644      ((srcSize > 0u) && ((NULL == dst) || (NULL == src)))|| (NULL == aesCcmState) || (NULL == tag))
645     {
646         return status;
647     }
648 
649     ivRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(iv);
650     aadRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(aad);
651     srcRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
652 
653     status = Cy_Cryptolite_Aes_Ccm_Set_Length(base, aadSize,  srcSize, tagSize, aesCcmState);
654 
655     if(CY_CRYPTOLITE_SUCCESS != status)
656     {
657         return status;
658     }
659 
660     status = Cy_Cryptolite_Aes_Ccm_Start(base, dirMode, ivSize, ivRemap, aesCcmState);
661 
662     if(CY_CRYPTOLITE_SUCCESS != status)
663     {
664         return status;
665     }
666 
667     status = Cy_Cryptolite_Aes_Ccm_Update_Aad(base, aadSize, aadRemap, aesCcmState);
668 
669     if(CY_CRYPTOLITE_SUCCESS != status)
670     {
671         return status;
672     }
673 
674     status = Cy_Cryptolite_Aes_Ccm_Update(base, srcSize, dst, srcRemap, aesCcmState);
675 
676     if(CY_CRYPTOLITE_SUCCESS != status)
677     {
678         return status;
679     }
680 
681     status = Cy_Cryptolite_Aes_Ccm_Finish(base, tag, aesCcmState);
682 
683     return status;
684 }
685 
686 
687 
688 
689 /*******************************************************************************
690 * Function Name: Cy_Cryptolite_Aes_Ccm_Encrypt_Tag
691 ****************************************************************************//*
692 *
693 * Performs an AES CCM Encrypt operation.
694 * \note Cy_Cryptolite_Aes_Ccm_Init() and Cy_Cryptolite_Aes_Ccm_SetKey() should be called before calling this function
695 *
696 * \param base
697 * The pointer to the CRYPTOLITE instance.
698 *
699 * \param ivSize
700 * The size of the IV.
701 *
702 * \param iv
703 * The pointer to the IV.
704 *
705 * \param aadSize
706 * The size of the AAD.
707 *
708 * \param aad
709 * The pointer to a AAD.
710 *
711 * \param srcSize
712 * The size of the source block.
713 *
714 * \param cipherTxt
715 * The pointer to a cipher text block.
716 *
717 * \param plainTxt
718 * The pointer to a plain text block.
719 *
720 * \param tagSize
721 * The size of the Tag.
722 *
723 * \param tag
724 * The pointer to the tags.
725 *
726 * \param aesCcmState
727 * The pointer to the AES CCM state structure allocated by the user. The user
728 * must not modify anything in this structure.
729 *
730 * \return
731 * \ref cy_en_cryptolite_status_t
732 *
733 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Encrypt_Tag(CRYPTOLITE_Type * base,uint32_t ivSize,uint8_t const * iv,uint32_t aadSize,uint8_t const * aad,uint32_t srcSize,uint8_t * cipherTxt,uint8_t const * plainTxt,uint32_t tagSize,uint8_t * tag,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)734 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Encrypt_Tag(CRYPTOLITE_Type *base,
735                                             uint32_t ivSize, uint8_t const * iv,
736                                             uint32_t aadSize, uint8_t const *aad,
737                                             uint32_t srcSize, uint8_t *cipherTxt, uint8_t const *plainTxt,
738                                             uint32_t tagSize, uint8_t *tag,
739                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
740 
741 {
742 
743 return Cy_Cryptolite_Aes_Ccm(base, CY_CRYPTOLITE_ENCRYPT, ivSize, iv, aadSize, aad,
744                                              srcSize, cipherTxt, plainTxt, tagSize, tag,
745                                             aesCcmState);
746 }
747 
748 
749 /*******************************************************************************
750 * Function Name: Cy_Cryptolite_Aes_Ccm_Decrypt
751 ****************************************************************************//*
752 *
753 * Performs an AES CCM Decrypt operation.
754 * \note Cy_Cryptolite_Aes_Ccm_Init() and Cy_Cryptolite_Aes_Ccm_SetKey() should be called before calling this function
755 *
756 * \param base
757 * The pointer to the CRYPTOLITE instance.
758 *
759 * \param ivSize
760 * The size of the IV.
761 *
762 * \param iv
763 * The pointer to the IV.
764 *
765 * \param aadSize
766 * The size of the AAD.
767 *
768 * \param aad
769 * The pointer to a AAD.
770 *
771 * \param srcSize
772 * The size of the source block.
773 *
774 * \param plainTxt
775 * The pointer to a plain text block.
776 *
777 * \param cipherTxt
778 * The pointer to a cipher text block.
779 *
780 * \param tagSize
781 * The size of the Tag.
782 *
783 * \param tag
784 * The pointer to the tags.
785 *
786 * \param isValid
787 * The pointer Store the authentication status.
788 *
789 * \param aesCcmState
790 * The pointer to the AES CCM state structure allocated by the user. The user
791 * must not modify anything in this structure.
792 *
793 * \return
794 * \ref cy_en_cryptolite_status_t
795 *
796 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Decrypt(CRYPTOLITE_Type * base,uint32_t ivSize,uint8_t const * iv,uint32_t aadSize,uint8_t const * aad,uint32_t srcSize,uint8_t * plainTxt,uint8_t const * cipherTxt,uint32_t tagSize,uint8_t * tag,cy_en_cryptolite_ccm_auth_result_t * isValid,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)797 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Decrypt(CRYPTOLITE_Type *base,
798                                             uint32_t ivSize, uint8_t const * iv,
799                                             uint32_t aadSize, uint8_t const *aad,
800                                             uint32_t srcSize, uint8_t *plainTxt, uint8_t const *cipherTxt,
801                                             uint32_t tagSize, uint8_t *tag, cy_en_cryptolite_ccm_auth_result_t *isValid,
802                                             cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
803 
804 {
805     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
806     uint8_t TagCal[CY_CRYPTOLITE_AES_BLOCK_SIZE] = {0u};
807     *isValid = CY_CRYPTOLITE_TAG_INVALID;
808 
809     status = Cy_Cryptolite_Aes_Ccm(base, CY_CRYPTOLITE_DECRYPT, ivSize, iv, aadSize, aad,
810                                                 srcSize, (uint8_t *)plainTxt, cipherTxt, tagSize, TagCal,
811                                                 aesCcmState);
812     if(CY_CRYPTOLITE_SUCCESS != status)
813     {
814         return status;
815     }
816 
817     if(Cy_Cryptolite_Vu_memcmp (tag, TagCal, tagSize) == 0u)
818     {
819         *isValid = CY_CRYPTOLITE_TAG_VALID;
820     }
821 
822     return CY_CRYPTOLITE_SUCCESS;
823 }
824 
825 
826 
827 /*******************************************************************************
828 * Function Name: Cy_Cryptolite_Aes_Ccm_Free
829 ****************************************************************************//*
830 *
831 * Clears AES CCM operation context.
832 *
833 * \param base
834 * The pointer to the CRYPTOLITE instance.
835 *
836 * \param aesCcmState
837 * The pointer to the AES CCM state structure allocated by the user. The user
838 * must not modify anything in this structure.
839 *
840 * \return
841 * \ref cy_en_cryptolite_status_t
842 *
843 *******************************************************************************/
Cy_Cryptolite_Aes_Ccm_Free(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_ccm_state_t * aesCcmState)844 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ccm_Free(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_ccm_state_t *aesCcmState)
845 {
846     (void)base;
847 
848     if(NULL != aesCcmState)
849     {
850         if(NULL != aesCcmState->aesCbcMacState.buffers)
851         {
852             Cy_Cryptolite_Vu_memset((void *)aesCcmState->aesCbcMacState.buffers, 0u, sizeof(cy_stc_cryptolite_aes_buffers_t));
853         }
854 
855         if(NULL != aesCcmState->aesCtrState.buffers)
856         {
857             Cy_Cryptolite_Vu_memset((void *)aesCcmState->aesCtrState.buffers, 0u, sizeof(cy_stc_cryptolite_aes_buffers_t));
858         }
859 
860         Cy_Cryptolite_Vu_memset((void *)aesCcmState, 0u, sizeof(cy_stc_cryptolite_aes_ccm_state_t));
861     }
862 
863     return CY_CRYPTOLITE_SUCCESS;
864 }
865 
866 
867 #endif /*CY_CRYPTOLITE_CFG_CIPHER_MODE_CCM*/
868 #endif /*CRYPTOLITE_AES_PRESENT*/
869 
870 #if defined(__cplusplus)
871 }
872 #endif
873 
874 
875 #endif /* CY_IP_MXCRYPTOLITE */
876 
877 
878 /* [] END OF FILE */
879