1 /***************************************************************************//**
2 * \file cy_cryptolite_aes.c
3 * \version 2.50
4 *
5 * \brief
6 * Provides API implementation of the Cryptolite AES PDL driver.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright (c) 2022, 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.h"
32 #include "cy_cryptolite_vu.h"
33
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37
38
39 #if (CRYPTOLITE_AES_PRESENT == 1)
40 #if defined(CY_CRYPTOLITE_CFG_AES_C)
41
42 /*******************************************************************************
43 * Function Name: Cy_Cryptolite_Aes_ProcessBlock
44 ****************************************************************************//*
45 *
46 * Performs the AES block cipher.
47 *
48 * \param base
49 * The pointer to the CRYPTOLITE instance.
50 *
51 * \param aesState
52 * the pointer to the cy_stc_cryptolite_aes_state_t structure that stores all
53 * internal variables for Cryptolite driver.
54 *
55 * \param dstBlock
56 * The pointer to the cipher text.
57 *
58 * \param srcBlock
59 * The pointer to the plain text. 4-Byte aligned buffer results in a faster operation!
60 *
61 * \return
62 * \ref cy_en_cryptolite_status_t
63 *******************************************************************************/
Cy_Cryptolite_Aes_ProcessBlock(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState,uint32_t * dstBlock,uint32_t const * srcBlock)64 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_ProcessBlock(CRYPTOLITE_Type *base,
65 cy_stc_cryptolite_aes_state_t *aesState,
66 uint32_t *dstBlock,
67 uint32_t const *srcBlock)
68 {
69 bool isSrcAligned = false;
70 bool isDstAligned = false;
71 uint32_t src_ptr = (uint32_t)srcBlock;
72 uint32_t dst_ptr = (uint32_t)dstBlock;
73
74 /* Checking the address alignment */
75 if(((uint32_t)srcBlock & 0x03U) == 0u)
76 {
77 isSrcAligned = true;
78 }
79
80 if(((uint32_t)dstBlock & 0x03U) == 0u)
81 {
82 isDstAligned = true;
83 }
84
85 if(isSrcAligned == false)
86 {
87 Cy_Cryptolite_Vu_memcpy(aesState->buffers->src, srcBlock, CY_CRYPTOLITE_AES_BLOCK_SIZE);
88 src_ptr = (uint32_t)aesState->buffers->src;
89 }
90
91 if(isDstAligned == false)
92 {
93 Cy_Cryptolite_Vu_memcpy(aesState->buffers->dst, dstBlock, CY_CRYPTOLITE_AES_BLOCK_SIZE);
94 dst_ptr = (uint32_t)aesState->buffers->dst;
95 }
96
97 aesState->message_encrypt_struct.plainText = (uint32_t)src_ptr;
98 aesState->message_encrypt_struct.cipherText = (uint32_t)dst_ptr;
99
100 /*check if IP is busy*/
101 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) == 1U)
102 {
103 return CY_CRYPTOLITE_HW_BUSY;
104 }
105
106 REG_CRYPTOLITE_AES_DESCR(base) = (uint32_t)&(aesState->message_encrypt_struct);
107 while((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) == 1U){};
108
109 if((REG_CRYPTOLITE_INTR_ERROR(base) & CRYPTOLITE_INTR_ERROR_BUS_ERROR_Msk) == 1U)
110 {
111 REG_CRYPTOLITE_INTR_ERROR(base) = CRYPTOLITE_INTR_ERROR_BUS_ERROR_Msk;
112 return CY_CRYPTOLITE_BUS_ERROR;
113 }
114
115 if(isSrcAligned == false)
116 {
117 Cy_Cryptolite_Vu_memcpy((void *)srcBlock, (const void *)aesState->buffers->src, CY_CRYPTOLITE_AES_BLOCK_SIZE);
118 }
119
120 if(isDstAligned == false)
121 {
122 Cy_Cryptolite_Vu_memcpy((void *)dstBlock, (const void *)aesState->buffers->dst, CY_CRYPTOLITE_AES_BLOCK_SIZE);
123 }
124
125 return CY_CRYPTOLITE_SUCCESS;
126 }
127
128
129 /*******************************************************************************
130 * Function Name: Cy_Cryptolite_Aes_Init
131 ****************************************************************************//*
132 *
133 * Sets AES mode.
134 *
135 * \param base
136 * The pointer to the CRYPTOLITE instance.
137 *
138 * \param key
139 * The pointer to the encryption/decryption key.
140 *
141 * \param aesState
142 * The pointer to the AES state structure allocated by the user. The user
143 * must not modify anything in this structure.
144 *
145 * \param aesBuffers The buffers should be a SAHB mapped addresses.
146 * The pointer to the memory buffers storage.
147 *
148 * \return
149 * \ref cy_en_cryptolite_status_t
150 *
151 *******************************************************************************/
Cy_Cryptolite_Aes_Init(CRYPTOLITE_Type * base,uint8_t const * key,cy_stc_cryptolite_aes_state_t * aesState,cy_stc_cryptolite_aes_buffers_t * aesBuffers)152 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Init(CRYPTOLITE_Type *base,
153 uint8_t const *key,
154 cy_stc_cryptolite_aes_state_t *aesState,
155 cy_stc_cryptolite_aes_buffers_t *aesBuffers)
156 {
157 (void)base;
158 cy_en_cryptolite_status_t err = CY_CRYPTOLITE_BAD_PARAMS;
159 uint32_t address_offset=0u;
160 uint8_t *keyRemap;
161
162 /* Input parameters verification */
163 if ((NULL == base) || (NULL == key) || (NULL == aesState) || (NULL == aesBuffers))
164 {
165 return err;
166 }
167
168 /*check if IP is busy*/
169 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
170 {
171 return CY_CRYPTOLITE_HW_BUSY;
172 }
173
174
175 keyRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(key);
176
177 /*Aligning the buffer address by 4 bytes*/
178 if(((uint32_t)aesBuffers & 0x03U) != 0u)
179 {
180 address_offset = 4u - ((uint32_t)aesBuffers & 0x03U);
181 }
182
183 aesState->buffers = (cy_stc_cryptolite_aes_buffers_t *)((uint32_t)aesBuffers + address_offset);
184
185 /* Copy the cipher block to the Initialization Vector */
186 Cy_Cryptolite_Vu_memcpy((uint8_t *)aesState->buffers->key, keyRemap, CY_CRYPTOLITE_AES_128_KEY_SIZE);
187 aesState->message_encrypt_struct.key = (uint32_t)aesState->buffers->key;
188
189 return CY_CRYPTOLITE_SUCCESS;
190 }
191
192 /*******************************************************************************
193 * Function Name: Cy_Cryptolite_Aes_Free
194 ****************************************************************************//*
195 *
196 * Clears AES operation context.
197 *
198 * \param base
199 * The pointer to the CRYPTOLITE instance.
200 *
201 * \param aesState
202 * The pointer to the AES state structure allocated by the user. The user
203 * must not modify anything in this structure.
204 *
205 * \return
206 * \ref cy_en_cryptolite_status_t
207 *
208 *******************************************************************************/
Cy_Cryptolite_Aes_Free(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)209 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Free(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_state_t *aesState)
210 {
211 (void)base;
212
213 if(NULL != aesState)
214 {
215 if(NULL != aesState->buffers)
216 {
217 Cy_Cryptolite_Vu_memset((void *)aesState->buffers, 0u, sizeof(cy_stc_cryptolite_aes_buffers_t));
218 }
219 Cy_Cryptolite_Vu_memset((void *)aesState, 0u, sizeof(cy_stc_cryptolite_aes_state_t));
220 }
221
222 return CY_CRYPTOLITE_SUCCESS;
223 }
224
225 /*******************************************************************************
226 * Function Name: Cy_Cryptolite_Aes_Ecb
227 ****************************************************************************//*
228 *
229 * Performs an AES Encryption operation on one block.
230 *
231 * \param base
232 * The pointer to the CRYPTOLITE instance.
233 *
234 *
235 * \param dst
236 * The pointer to a destination cipher block, must be 4 byte aligned buffer results in a faster operation and should be a SAHB mapped addresses.
237 *
238 * \param src
239 * The pointer to a source block, must be 4 byte aligned results in a faster operation and should be a SAHB mapped addresses
240 *
241 *
242 * \param aesState
243 * The pointer to the AES state structure allocated by the user. The user
244 * must not modify anything in this structure.
245 *
246 * \return
247 * \ref cy_en_cryptolite_status_t
248 *
249 *******************************************************************************/
Cy_Cryptolite_Aes_Ecb(CRYPTOLITE_Type * base,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)250 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ecb(CRYPTOLITE_Type *base,
251 uint8_t *dst,
252 uint8_t const *src,
253 cy_stc_cryptolite_aes_state_t *aesState)
254 {
255
256 cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
257 uint8_t *srcRemap;
258
259 /* Input parameters verification */
260 if ((NULL == base) || (NULL == dst) || (NULL == src) || (NULL == aesState))
261 {
262 return status;
263 }
264
265 /*check if IP is busy*/
266 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
267 {
268 return CY_CRYPTOLITE_HW_BUSY;
269 }
270
271
272 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
273
274 status = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, (uint32_t*)((void*)dst), (uint32_t*)((void*)srcRemap));
275
276 return status;
277 }
278
279
280
281 /*******************************************************************************
282 * Function Name: Cy_Cryptolite_Aes_Ecb_Setup
283 ****************************************************************************//*
284 *
285 * Performs an AES ECB setup operation.
286 *
287 * \param base
288 * The pointer to the CRYPTOLITE instance.
289 *
290 * \param aesState
291 * The pointer to the AES state structure allocated by the user. The user
292 * must not modify anything in this structure.
293 *
294 * \return
295 * \ref cy_en_cryptolite_status_t
296 *
297 *******************************************************************************/
Cy_Cryptolite_Aes_Ecb_Setup(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)298 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ecb_Setup(CRYPTOLITE_Type *base,
299 cy_stc_cryptolite_aes_state_t *aesState)
300 {
301 /* Input parameters verification */
302 if ((NULL == base) || (NULL == aesState) || (NULL == aesState->buffers))
303 {
304 return CY_CRYPTOLITE_BAD_PARAMS;
305 }
306
307 aesState->unProcessedBytes = 0u;
308
309 return CY_CRYPTOLITE_SUCCESS;
310 }
311
312
313 /*******************************************************************************
314 * Function Name: Cy_Cryptolite_Aes_Ecb_Update
315 ****************************************************************************//*
316 *
317 * Performs an AES ECB Multistage update operation.
318 *
319 * \param base
320 * The pointer to the CRYPTOLITE instance.
321 *
322 * \param srcSize
323 * The size of the source block.
324 *
325 * \param dst
326 * The pointer to a destination cipher block.
327 *
328 * \param src
329 * The pointer to a source block.
330 *
331 * \param dstLength
332 * The size of the calculated cipher block.
333 *
334 * \param aesState
335 * The pointer to the AES state structure allocated by the user. The user
336 * must not modify anything in this structure.
337 *
338 * \return
339 * \ref cy_en_cryptolite_status_t
340 *
341 *******************************************************************************/
Cy_Cryptolite_Aes_Ecb_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * dst,uint8_t const * src,uint32_t * dstLength,cy_stc_cryptolite_aes_state_t * aesState)342 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ecb_Update(CRYPTOLITE_Type *base,
343 uint32_t srcSize,
344 uint8_t *dst,
345 uint8_t const *src,
346 uint32_t *dstLength,
347 cy_stc_cryptolite_aes_state_t *aesState)
348
349
350 {
351 cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
352 uint32_t bytes_to_copy=0u;
353 uint32_t outputLength=0u;
354 uint8_t *srcRemap;
355
356 if(NULL == dstLength)
357 {
358 return status;
359 }
360
361 *dstLength = outputLength;
362
363 if(0u == srcSize)
364 {
365 return CY_CRYPTOLITE_SUCCESS;
366 }
367
368 /* Input parameters verification */
369 if ((NULL == base) || (NULL == aesState) || (NULL == dst) || (NULL == src))
370 {
371 return CY_CRYPTOLITE_BAD_PARAMS;
372 }
373
374 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
375
376 if(aesState->unProcessedBytes + srcSize < CY_CRYPTOLITE_AES_BLOCK_SIZE)
377 {
378 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->unProcessedData[aesState->unProcessedBytes], (void*)srcRemap, (uint16_t)srcSize);
379 aesState->unProcessedBytes += srcSize;
380 status = CY_CRYPTOLITE_SUCCESS;
381 }
382 else
383 {
384 if(aesState->unProcessedBytes > 0u)
385 {
386 bytes_to_copy = CY_CRYPTOLITE_AES_BLOCK_SIZE - aesState->unProcessedBytes;
387
388 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->unProcessedData[aesState->unProcessedBytes], (void*)srcRemap, (uint16_t)bytes_to_copy);
389 status = Cy_Cryptolite_Aes_Ecb(base, dst, (void*)aesState->buffers->unProcessedData, aesState);
390
391 if(CY_CRYPTOLITE_SUCCESS != status)
392 {
393 return status;
394 }
395
396 srcSize -= bytes_to_copy;
397 srcRemap += bytes_to_copy;
398 dst += CY_CRYPTOLITE_AES_BLOCK_SIZE;
399 outputLength += CY_CRYPTOLITE_AES_BLOCK_SIZE;
400 aesState->unProcessedBytes = 0u;
401 }
402
403 while(srcSize >= CY_CRYPTOLITE_AES_BLOCK_SIZE)
404 {
405 status = Cy_Cryptolite_Aes_Ecb(base, dst, srcRemap, aesState);
406
407 if(CY_CRYPTOLITE_SUCCESS != status)
408 {
409 return status;
410 }
411
412 dst += CY_CRYPTOLITE_AES_BLOCK_SIZE;
413 srcRemap += CY_CRYPTOLITE_AES_BLOCK_SIZE;
414 srcSize -= CY_CRYPTOLITE_AES_BLOCK_SIZE;
415 outputLength += CY_CRYPTOLITE_AES_BLOCK_SIZE;
416 }
417
418 if(srcSize > 0u)
419 {
420 Cy_Cryptolite_Vu_memcpy((void*)aesState->buffers->unProcessedData, (void*)srcRemap, (uint16_t)srcSize);
421 aesState->unProcessedBytes = srcSize;
422 }
423 }
424
425 *dstLength = outputLength;
426
427 return status;
428 }
429
430
431 /*******************************************************************************
432 * Function Name: Cy_Cryptolite_Aes_Ecb_Finish
433 ****************************************************************************//*
434 *
435 * Performs an AES ECB finish operation.
436 *
437 * \param base
438 * The pointer to the CRYPTOLITE instance.
439 *
440 * \param aesState
441 * The pointer to the AES state structure allocated by the user. The user
442 * must not modify anything in this structure.
443 *
444 * \return
445 * \ref cy_en_cryptolite_status_t
446 *
447 *******************************************************************************/
Cy_Cryptolite_Aes_Ecb_Finish(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)448 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ecb_Finish(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_state_t *aesState)
449 {
450 /* Input parameters verification */
451 if ((NULL == base) || (NULL == aesState))
452 {
453 return CY_CRYPTOLITE_BAD_PARAMS;
454 }
455
456 if(aesState->unProcessedBytes > 0u)
457 {
458 return CY_CRYPTOLITE_BUFFER_NOT_ALIGNED;
459 }
460
461 return CY_CRYPTOLITE_SUCCESS;
462 }
463
464 #if defined(CY_CRYPTOLITE_CFG_CIPHER_MODE_CBC)
465 /*******************************************************************************
466 * Function Name: Cy_Cryptolite_Aes_Cbc
467 ****************************************************************************//*
468 *
469 * Performs AES Encryption operation on a plain text with Cipher Block Chaining (CBC).
470 *
471 * \param base
472 * The pointer to the CRYPTOLITE instance.
473 *
474 * \param srcSize
475 * The size of the source plain text.
476 *
477 * \param ivPtr
478 * The pointer to the initial vector.
479 *
480 * \param dst
481 * The pointer to a destination cipher text, must be 4 byte aligned buffer results in a faster operation and should be a SAHB mapped addresses.
482 *
483 * \param src
484 * The pointer to a source plain text.
485 *
486 * \param aesState
487 * The pointer to the AES state structure allocated by the user. The user
488 * must not modify anything in this structure.
489 *
490 * \return
491 * \ref cy_en_cryptolite_status_t
492 *
493 *******************************************************************************/
Cy_Cryptolite_Aes_Cbc(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)494 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cbc(CRYPTOLITE_Type *base,
495 uint32_t srcSize,
496 uint8_t *ivPtr,
497 uint8_t *dst,
498 uint8_t const *src,
499 cy_stc_cryptolite_aes_state_t *aesState)
500 {
501 uint32_t size = srcSize;
502 cy_stc_cryptolite_aes_buffers_t *aesBuffers = NULL;
503 uint32_t *srcBuff = NULL;
504 uint32_t *dstBuff = NULL;
505 uint32_t *tempBuff = NULL;
506 uint8_t *srcRemap;
507 uint8_t *ivRemap;
508
509 cy_en_cryptolite_status_t tmpResult = CY_CRYPTOLITE_BAD_PARAMS;
510
511 /* Input parameters verification */
512 if ((NULL == base) || (NULL == ivPtr) || (NULL == dst) || (NULL == src) || (NULL == aesState))
513 {
514 return tmpResult;
515 }
516
517 /*check if IP is busy*/
518 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
519 {
520 return CY_CRYPTOLITE_HW_BUSY;
521 }
522
523 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
524 ivRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(ivPtr);
525
526 aesBuffers = (cy_stc_cryptolite_aes_buffers_t*)aesState->buffers;
527 srcBuff = (uint32_t*)((void *)srcRemap);
528 dstBuff = (uint32_t*)((void *)dst);
529 tempBuff = (uint32_t*)(aesBuffers->block0);
530
531 tmpResult = CY_CRYPTOLITE_SIZE_NOT_X16;
532
533 /* Check whether the data size is multiple of CY_CRYPTOLITE_AES_BLOCK_SIZE */
534 if (0UL == (uint32_t)(size & (CY_CRYPTOLITE_AES_BLOCK_SIZE - 1U)))
535 {
536 /* Copy the Initialization Vector to the local buffer because it changes during calculation */
537 Cy_Cryptolite_Vu_memcpy((uint8_t *)tempBuff, ivRemap, CY_CRYPTOLITE_AES_BLOCK_SIZE);
538
539 while (size != 0UL)
540 {
541 tempBuff[0] = srcBuff[0] ^ tempBuff[0];
542 tempBuff[1] = srcBuff[1] ^ tempBuff[1];
543 tempBuff[2] = srcBuff[2] ^ tempBuff[2];
544 tempBuff[3] = srcBuff[3] ^ tempBuff[3];
545
546 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, dstBuff, tempBuff);
547
548 if(CY_CRYPTOLITE_SUCCESS != tmpResult){
549 break;
550 }
551
552 /* temporary cipher block */
553 Cy_Cryptolite_Vu_memcpy(tempBuff, dstBuff, CY_CRYPTOLITE_AES_BLOCK_SIZE);
554
555 srcBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
556 size -= CY_CRYPTOLITE_AES_BLOCK_SIZE;
557
558 if(aesState->isMac == false)
559 {
560 dstBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
561 }
562
563 }
564
565 if(CY_CRYPTOLITE_SUCCESS == tmpResult)
566 {
567 /* Copy the cipher block to the Initialization Vector */
568 Cy_Cryptolite_Vu_memcpy(ivRemap, tempBuff, CY_CRYPTOLITE_AES_BLOCK_SIZE);
569 }
570 }
571
572 return (tmpResult);
573 }
574
575 /*******************************************************************************
576 * Function Name: Cy_Cryptolite_Aes_Cbc_Setup
577 ****************************************************************************//*
578 *
579 * Performs an AES CBC setup operation.
580 *
581 * \param base
582 * The pointer to the CRYPTOLITE instance.
583 *
584 * \param aesState
585 * The pointer to the AES state structure allocated by the user. The user
586 * must not modify anything in this structure.
587 *
588 * \return
589 * \ref cy_en_cryptolite_status_t
590 *
591 *******************************************************************************/
592
Cy_Cryptolite_Aes_Cbc_Setup(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)593 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cbc_Setup(CRYPTOLITE_Type *base,
594 cy_stc_cryptolite_aes_state_t *aesState)
595 {
596 /* Input parameters verification */
597 if ((NULL == base) || (NULL == aesState) || (NULL == aesState->buffers))
598 {
599 return CY_CRYPTOLITE_BAD_PARAMS;
600 }
601
602 aesState->unProcessedBytes = 0u;
603 aesState->isMac = false;
604
605 return CY_CRYPTOLITE_SUCCESS;
606 }
607
608
609
610
611 /*******************************************************************************
612 * Function Name: Cy_Cryptolite_Aes_Cbc_Set_IV
613 ****************************************************************************//*
614 *
615 * Function to set AES CBC IV.
616 *
617 * \param base
618 * The pointer to the CRYPTOLITE instance.
619 *
620 * \param iv
621 * The pointer to the IV.
622 *
623 * \param aesState
624 * The pointer to the AES state structure allocated by the user. The user
625 * must not modify anything in this structure.
626 *
627 * \return
628 * \ref cy_en_cryptolite_status_t
629 *
630 *******************************************************************************/
631
Cy_Cryptolite_Aes_Cbc_Set_IV(CRYPTOLITE_Type * base,uint8_t const * iv,cy_stc_cryptolite_aes_state_t * aesState)632 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cbc_Set_IV(CRYPTOLITE_Type *base,
633 uint8_t const * iv,
634 cy_stc_cryptolite_aes_state_t *aesState)
635 {
636 uint8_t *ivRemap;
637
638 /* Input parameters verification */
639 if ((NULL == base) || (NULL == aesState) || (NULL == iv))
640 {
641 return CY_CRYPTOLITE_BAD_PARAMS;
642 }
643
644 ivRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(iv);
645
646 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->iv, (void*)ivRemap, (uint16_t)CY_CRYPTOLITE_AES_BLOCK_SIZE);
647
648 return CY_CRYPTOLITE_SUCCESS;
649 }
650
651
652 /*******************************************************************************
653 * Function Name: Cy_Cryptolite_Aes_Cbc_Update
654 ****************************************************************************//*
655 *
656 * Performs an AES CBC Multistage update operation.
657 *
658 * \param base
659 * The pointer to the CRYPTOLITE instance.
660 *
661 * \param srcSize
662 * The size of the source block.
663 *
664 * \param dst
665 * The pointer to a destination cipher block.
666 *
667 * \param src
668 * The pointer to a source block.
669 *
670 * \param dstLength
671 * The size of the calculated dst block.
672 *
673 * \param aesState
674 * The pointer to the AES state structure allocated by the user. The user
675 * must not modify anything in this structure.
676 *
677 * \return
678 * \ref cy_en_cryptolite_status_t
679 *
680 *******************************************************************************/
Cy_Cryptolite_Aes_Cbc_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * dst,uint8_t const * src,uint32_t * dstLength,cy_stc_cryptolite_aes_state_t * aesState)681 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cbc_Update(CRYPTOLITE_Type *base,
682 uint32_t srcSize,
683 uint8_t *dst,
684 uint8_t const *src,
685 uint32_t *dstLength,
686 cy_stc_cryptolite_aes_state_t *aesState)
687
688
689 {
690 cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
691 uint32_t bytes_to_copy=0u;
692 uint32_t cnt=0u;
693 uint32_t outputLength=0u;
694 uint8_t *srcRemap;
695
696 if(NULL == dstLength)
697 {
698 return status;
699 }
700
701 *dstLength = outputLength;
702
703 if(0u == srcSize)
704 {
705 return CY_CRYPTOLITE_SUCCESS;
706 }
707
708 /* Input parameters verification */
709 if ((NULL == base) || (NULL == aesState) || (NULL == dst) || (NULL == src))
710 {
711 return CY_CRYPTOLITE_BAD_PARAMS;
712 }
713
714 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
715
716 if(aesState->unProcessedBytes + srcSize < CY_CRYPTOLITE_AES_BLOCK_SIZE)
717 {
718 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->unProcessedData[aesState->unProcessedBytes], (void*)srcRemap, (uint16_t)srcSize);
719 aesState->unProcessedBytes += srcSize;
720 status = CY_CRYPTOLITE_SUCCESS;
721 }
722 else
723 {
724 if(aesState->unProcessedBytes > 0u)
725 {
726 bytes_to_copy = CY_CRYPTOLITE_AES_BLOCK_SIZE - aesState->unProcessedBytes;
727
728 if (srcSize < bytes_to_copy)
729 {
730 bytes_to_copy = srcSize;
731 }
732
733 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->unProcessedData[aesState->unProcessedBytes], (void*)srcRemap, (uint16_t)bytes_to_copy);
734
735 status = Cy_Cryptolite_Aes_Cbc(base, CY_CRYPTOLITE_AES_BLOCK_SIZE, aesState->buffers->iv,
736 dst, aesState->buffers->unProcessedData, aesState);
737 if(CY_CRYPTOLITE_SUCCESS != status)
738 {
739 return status;
740 }
741 srcSize -= bytes_to_copy;
742 srcRemap += bytes_to_copy;
743 aesState->unProcessedBytes = 0u;
744 outputLength += CY_CRYPTOLITE_AES_BLOCK_SIZE;
745
746 if(aesState->isMac == false)
747 {
748 dst += CY_CRYPTOLITE_AES_BLOCK_SIZE;
749 }
750 }
751
752 cnt = (uint32_t)(srcSize / CY_CRYPTOLITE_AES_BLOCK_SIZE);
753
754 if(cnt != 0u)
755 {
756 status = Cy_Cryptolite_Aes_Cbc(base, cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE, aesState->buffers->iv,
757 dst, srcRemap, aesState);
758 if(CY_CRYPTOLITE_SUCCESS != status)
759 {
760 return status;
761 }
762
763 srcRemap += cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
764 srcSize -= cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
765 outputLength += cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
766 }
767
768 if((srcSize > 0u) && (srcSize < CY_CRYPTOLITE_AES_BLOCK_SIZE))
769 {
770 Cy_Cryptolite_Vu_memcpy(aesState->buffers->unProcessedData, (void*)srcRemap, (uint16_t)srcSize);
771 aesState->unProcessedBytes = srcSize;
772 }
773 }
774
775 *dstLength = outputLength;
776
777 return status;
778 }
779
780
781
782 /*******************************************************************************
783 * Function Name: Cy_Cryptolite_Aes_Cbc_Finish
784 ****************************************************************************//*
785 *
786 * Performs an AES CBC finish operation.
787 *
788 * \param base
789 * The pointer to the CRYPTOLITE instance.
790 *
791 * \param aesState
792 * The pointer to the AES state structure allocated by the user. The user
793 * must not modify anything in this structure.
794 *
795 * \return
796 * \ref cy_en_cryptolite_status_t
797 *
798 *******************************************************************************/
Cy_Cryptolite_Aes_Cbc_Finish(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)799 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cbc_Finish(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_state_t *aesState)
800 {
801 /* Input parameters verification */
802 if ((NULL == base) || (NULL == aesState))
803 {
804 return CY_CRYPTOLITE_BAD_PARAMS;
805 }
806
807 if(aesState->unProcessedBytes > 0u)
808 {
809 return CY_CRYPTOLITE_BUFFER_NOT_ALIGNED;
810 }
811
812 return CY_CRYPTOLITE_SUCCESS;
813 }
814
815
816 #if defined(CY_CRYPTOLITE_CFG_CBC_MAC_C)
817 /*******************************************************************************
818 * Function Name: Cy_Cryptolite_Aes_CbcMac_Setup
819 ****************************************************************************//*
820 *
821 * Performs an AES CBC MAC setup operation.
822 *
823 * \param base
824 * The pointer to the CRYPTOLITE instance.
825 *
826 * \param aesState
827 * The pointer to the AES state structure allocated by the user. The user
828 * must not modify anything in this structure.
829 *
830 * \return
831 * \ref cy_en_cryptolite_status_t
832 *
833 *******************************************************************************/
834
Cy_Cryptolite_Aes_CbcMac_Setup(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)835 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_CbcMac_Setup(CRYPTOLITE_Type *base,
836 cy_stc_cryptolite_aes_state_t *aesState)
837 {
838 /* Input parameters verification */
839 if ((NULL == base) || (NULL == aesState) || (NULL == aesState->buffers))
840 {
841 return CY_CRYPTOLITE_BAD_PARAMS;
842 }
843
844 aesState->unProcessedBytes = 0u;
845 aesState->isMac = true;
846
847 Cy_Cryptolite_Vu_memset ((void*)&aesState->buffers->iv, 0u, (uint16_t)CY_CRYPTOLITE_AES_BLOCK_SIZE);
848 return CY_CRYPTOLITE_SUCCESS;
849 }
850
851
852 /*******************************************************************************
853 * Function Name: Cy_Cryptolite_Aes_CbcMac_Update
854 ****************************************************************************//*
855 *
856 * Performs an AES CBC MAC Multistage update operation.
857 *
858 * \param base
859 * The pointer to the CRYPTOLITE instance.
860 *
861 * \param srcSize
862 * The size of the source block.
863 *
864 * \param src
865 * The pointer to a source block.
866 *
867 * \param aesState
868 * The pointer to the AES state structure allocated by the user. The user
869 * must not modify anything in this structure.
870 *
871 * \return
872 * \ref cy_en_cryptolite_status_t
873 *
874 *******************************************************************************/
Cy_Cryptolite_Aes_CbcMac_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)875 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_CbcMac_Update(CRYPTOLITE_Type *base,
876 uint32_t srcSize,
877 uint8_t const *src,
878 cy_stc_cryptolite_aes_state_t *aesState)
879
880
881 {
882 uint32_t dstLength;
883 uint8_t *srcRemap;
884
885 if(0u == srcSize)
886 {
887 return CY_CRYPTOLITE_SUCCESS;
888 }
889
890 /* Input parameters verification */
891 if ((NULL == base) || (NULL == aesState) || (NULL == src))
892 {
893 return CY_CRYPTOLITE_BAD_PARAMS;
894 }
895
896 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
897
898 return Cy_Cryptolite_Aes_Cbc_Update(CRYPTOLITE,
899 srcSize,
900 aesState->buffers->temp,
901 srcRemap,
902 &dstLength,
903 aesState);
904 }
905
906
907
908 /*******************************************************************************
909 * Function Name: Cy_Cryptolite_Aes_CbcMac_Finish
910 ****************************************************************************//*
911 *
912 * Performs an AES CBC MAC finish operation.
913 *
914 * \param base
915 * The pointer to the CRYPTOLITE instance.
916 *
917 * \param mac
918 * The pointer to store the calculated mac.
919 *
920 * \param aesState
921 * The pointer to the AES state structure allocated by the user. The user
922 * must not modify anything in this structure.
923 *
924 * \return
925 * \ref cy_en_cryptolite_status_t
926 *
927 *******************************************************************************/
Cy_Cryptolite_Aes_CbcMac_Finish(CRYPTOLITE_Type * base,uint8_t * mac,cy_stc_cryptolite_aes_state_t * aesState)928 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_CbcMac_Finish(CRYPTOLITE_Type *base, uint8_t * mac, cy_stc_cryptolite_aes_state_t *aesState)
929 {
930 /* Input parameters verification */
931 if ((NULL == base) || (NULL == aesState) || (NULL == mac))
932 {
933 return CY_CRYPTOLITE_BAD_PARAMS;
934 }
935
936 if(aesState->unProcessedBytes > 0u)
937 {
938 return CY_CRYPTOLITE_BUFFER_NOT_ALIGNED;
939 }
940
941 Cy_Cryptolite_Vu_memcpy(mac, aesState->buffers->iv, (uint16_t)CY_CRYPTOLITE_AES_BLOCK_SIZE);
942
943 return CY_CRYPTOLITE_SUCCESS;
944 }
945
946 #endif /*CY_CRYPTOLITE_CFG_CBC_MAC_C*/
947
948 #endif /*CY_CRYPTOLITE_CFG_CIPHER_MODE_CBC*/
949
950
951 #if defined(CY_CRYPTOLITE_CFG_CIPHER_MODE_CFB)
952 /*******************************************************************************
953 * Function Name: Cy_Cryptolite_Aes_Cfb
954 ********************************************************************************
955 *
956 * Performs AES Encryption/Decryption operation on a data with the Cipher Feedback Block method (CFB).
957 *
958 * \param base
959 * The pointer to the CRYPTOLITE instance.
960 *
961 * \param dirMode
962 * Can be \ref CY_CRYPTOLITE_ENCRYPT or \ref CY_CRYPTOLITE_DECRYPT
963 * (\ref cy_en_cryptolite_dir_mode_t)
964 *
965 * \param srcSize
966 * The size of the source plain text.
967 *
968 * \param ivPtr
969 * The pointer to the initial vector.
970 *
971 * \param dst
972 * The pointer to a destination cipher text.
973 *
974 * \param src
975 * The pointer to a source plain text.
976 *
977 * \param aesState
978 * The pointer to the AES state structure allocated by the user. The user must
979 * must not modify anything in this structure.
980 *
981 * \return
982 * \ref cy_en_cryptolite_status_t
983 *
984 *******************************************************************************/
Cy_Cryptolite_Aes_Cfb(CRYPTOLITE_Type * base,cy_en_cryptolite_dir_mode_t dirMode,uint32_t srcSize,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)985 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cfb(CRYPTOLITE_Type *base,
986 cy_en_cryptolite_dir_mode_t dirMode,
987 uint32_t srcSize,
988 uint8_t *ivPtr,
989 uint8_t *dst,
990 uint8_t const *src,
991 cy_stc_cryptolite_aes_state_t *aesState)
992 {
993 uint32_t size = srcSize;
994 uint32_t *srcBuff = NULL;
995 uint32_t *dstBuff = NULL;
996 uint32_t *tempBuff = NULL;
997 uint32_t *tempBuff1 = NULL;
998
999 uint8_t *srcRemap;
1000 uint8_t *ivRemap;
1001
1002 cy_stc_cryptolite_aes_buffers_t *aesBuffers = NULL;
1003 cy_en_cryptolite_status_t tmpResult = CY_CRYPTOLITE_BAD_PARAMS;
1004
1005 /* Input parameters verification */
1006 if ((NULL == base) || (NULL == ivPtr) || (NULL == dst) || (NULL == src) || (NULL == aesState))
1007 {
1008 return tmpResult;
1009 }
1010
1011 /*check if IP is busy*/
1012 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
1013 {
1014 return CY_CRYPTOLITE_HW_BUSY;
1015 }
1016
1017 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
1018 ivRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(ivPtr);
1019
1020 tmpResult = CY_CRYPTOLITE_SIZE_NOT_X16;
1021
1022 aesBuffers = (cy_stc_cryptolite_aes_buffers_t*)aesState->buffers;
1023 srcBuff = (uint32_t*)((void *)srcRemap);
1024 dstBuff = (uint32_t*)((void *)dst);
1025 tempBuff = (uint32_t*)(aesBuffers->block0);
1026 tempBuff1 = (uint32_t*)(aesBuffers->block1);
1027
1028 /* Check whether the data size is multiple of CY_CRYPTOLITE_AES_BLOCK_SIZE */
1029 if (0UL == (size & (CY_CRYPTOLITE_AES_BLOCK_SIZE - 1U)))
1030 {
1031
1032 /* Copies the Initialization Vector to the local encode buffer. */
1033 Cy_Cryptolite_Vu_memcpy(tempBuff, ivRemap, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1034
1035 while (size != 0UL)
1036 {
1037 /* In this mode, (CFB) is always an encryption! */
1038 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, tempBuff1, tempBuff);
1039
1040 if(CY_CRYPTOLITE_SUCCESS != tmpResult){
1041 break;
1042 }
1043 Cy_Cryptolite_Vu_memcpy(tempBuff, tempBuff1, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1044
1045 // Cy_Cryptolite_Aes_Xor(base, aesState, dstBuff, srcBuff, dstBuff);
1046 dstBuff[0] = srcBuff[0] ^ tempBuff[0];
1047 dstBuff[1] = srcBuff[1] ^ tempBuff[1];
1048 dstBuff[2] = srcBuff[2] ^ tempBuff[2];
1049 dstBuff[3] = srcBuff[3] ^ tempBuff[3];
1050
1051 if (CY_CRYPTOLITE_DECRYPT == dirMode)
1052 {
1053 /* cipher text block */
1054 Cy_Cryptolite_Vu_memcpy(tempBuff, srcBuff, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1055 }
1056 else
1057 {
1058 /* cipher text block */
1059 Cy_Cryptolite_Vu_memcpy(tempBuff, dstBuff, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1060 }
1061
1062 srcBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1063 dstBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1064
1065 size -= CY_CRYPTOLITE_AES_BLOCK_SIZE;
1066 }
1067
1068 if(CY_CRYPTOLITE_SUCCESS == tmpResult)
1069 {
1070 /* Copies the local encode buffer to the Initialization Vector. */
1071 Cy_Cryptolite_Vu_memcpy(ivRemap, tempBuff, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1072 }
1073 }
1074
1075 return (tmpResult);
1076 }
1077
1078
1079 /*******************************************************************************
1080 * Function Name: Cy_Cryptolite_Aes_Cfb_Setup
1081 ****************************************************************************//*
1082 *
1083 * Performs an AES CFB setup operation.
1084 *
1085 * \param base
1086 * The pointer to the CRYPTO instance.
1087 *
1088 * \param dirMode
1089 * Can be \ref CY_CRYPTOLITE_ENCRYPT or \ref CY_CRYPTO_DECRYPT
1090 * (\ref cy_en_cryptolite_dir_mode_t).
1091 *
1092 * \param aesState
1093 * The pointer to the AES state structure allocated by the user. The user
1094 * must not modify anything in this structure.
1095 *
1096 * \return
1097 * \ref cy_en_cryptolite_status_t
1098 *
1099 *******************************************************************************/
Cy_Cryptolite_Aes_Cfb_Setup(CRYPTOLITE_Type * base,cy_en_cryptolite_dir_mode_t dirMode,cy_stc_cryptolite_aes_state_t * aesState)1100 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cfb_Setup(CRYPTOLITE_Type *base,
1101 cy_en_cryptolite_dir_mode_t dirMode,
1102 cy_stc_cryptolite_aes_state_t *aesState)
1103 {
1104 /* Input parameters verification */
1105 if ((NULL == base) || (NULL == aesState) || (NULL == aesState->buffers))
1106 {
1107 return CY_CRYPTOLITE_BAD_PARAMS;
1108 }
1109
1110 aesState->dirMode = dirMode;
1111 aesState->unProcessedBytes = 0u;
1112
1113 return CY_CRYPTOLITE_SUCCESS;
1114 }
1115
1116
1117
1118
1119 /*******************************************************************************
1120 * Function Name: Cy_Cryptolite_Aes_Cfb_Set_IV
1121 ****************************************************************************//*
1122 *
1123 * Sets IV for AES CFB mode.
1124 *
1125 * \param base
1126 * The pointer to the CRYPTO instance.
1127 *
1128 * \param iv
1129 * The pointer to iv.
1130 *
1131 * \param aesState
1132 * The pointer to the AES state structure allocated by the user. The user
1133 * must not modify anything in this structure.
1134 *
1135 * \return
1136 * \ref cy_en_cryptolite_status_t
1137 *
1138 *******************************************************************************/
Cy_Cryptolite_Aes_Cfb_Set_IV(CRYPTOLITE_Type * base,uint8_t const * iv,cy_stc_cryptolite_aes_state_t * aesState)1139 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cfb_Set_IV(CRYPTOLITE_Type *base,
1140 uint8_t const * iv,
1141 cy_stc_cryptolite_aes_state_t *aesState)
1142 {
1143 uint8_t *ivRemap;
1144
1145 /* Input parameters verification */
1146 if ((NULL == base) || (NULL == aesState) || (NULL == iv))
1147 {
1148 return CY_CRYPTOLITE_BAD_PARAMS;
1149 }
1150
1151 ivRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(iv);
1152
1153 Cy_Cryptolite_Vu_memcpy((void*)&aesState->buffers->iv, (void*)ivRemap, (uint16_t)CY_CRYPTOLITE_AES_BLOCK_SIZE);
1154
1155 return CY_CRYPTOLITE_SUCCESS;
1156 }
1157
1158
1159
1160
1161 /*******************************************************************************
1162 * Function Name: Cy_Cryptolite_Aes_Cfb_Update
1163 ****************************************************************************//*
1164 *
1165 * Performs an AES CFB Multistage update operation.
1166 *
1167 * \param base
1168 * The pointer to the CRYPTO instance.
1169 *
1170 * \param srcSize
1171 * The size of the source block.
1172 *
1173 * \param dst
1174 * The pointer to a destination cipher block.
1175 *
1176 * \param src
1177 * The pointer to a source block.
1178 *
1179 * \param aesState
1180 * The pointer to the AES state structure allocated by the user. The user
1181 * must not modify anything in this structure.
1182 *
1183 * \return
1184 * \ref cy_en_cryptolite_status_t
1185 *
1186 *******************************************************************************/
Cy_Cryptolite_Aes_Cfb_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)1187 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cfb_Update(CRYPTOLITE_Type *base,
1188 uint32_t srcSize,
1189 uint8_t *dst,
1190 uint8_t const *src,
1191 cy_stc_cryptolite_aes_state_t *aesState)
1192 {
1193 uint32_t bytes_rem=0u;
1194 uint32_t cnt=0u;
1195 cy_en_cryptolite_status_t tmpResult = CY_CRYPTOLITE_SIZE_NOT_X16;
1196 uint8_t *srcRemap;
1197
1198 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
1199
1200 if(aesState->unProcessedBytes !=0u)
1201 {
1202 bytes_rem = CY_CRYPTOLITE_AES_BLOCK_SIZE - aesState->unProcessedBytes;
1203
1204 if (srcSize < bytes_rem)
1205 {
1206 bytes_rem = srcSize;
1207 }
1208
1209 for( uint32_t i=0u; i<bytes_rem ; i++)
1210 {
1211 dst[i] = srcRemap[i] ^ aesState->buffers->unProcessedData[aesState->unProcessedBytes + i];
1212 }
1213
1214 if(aesState->dirMode == CY_CRYPTOLITE_ENCRYPT)
1215 {
1216 Cy_Cryptolite_Vu_memcpy(&aesState->buffers->iv[aesState->unProcessedBytes], dst, (uint16_t)bytes_rem);
1217 }
1218 else
1219 {
1220 Cy_Cryptolite_Vu_memcpy(&aesState->buffers->iv[aesState->unProcessedBytes], srcRemap, (uint16_t)bytes_rem);
1221 }
1222
1223 aesState->unProcessedBytes += bytes_rem;
1224 srcSize -= bytes_rem;
1225 srcRemap += bytes_rem;
1226 dst += bytes_rem;
1227
1228 if(aesState->unProcessedBytes == CY_CRYPTOLITE_AES_BLOCK_SIZE)
1229 {
1230 aesState->unProcessedBytes = 0;
1231 }
1232
1233 tmpResult = CY_CRYPTOLITE_SUCCESS;
1234 }
1235
1236 cnt = (uint32_t)(srcSize / CY_CRYPTOLITE_AES_BLOCK_SIZE);
1237
1238 if(cnt != 0u)
1239 {
1240 tmpResult = Cy_Cryptolite_Aes_Cfb(base,aesState->dirMode, cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE,
1241 aesState->buffers->iv, dst, srcRemap, aesState);
1242
1243 if(CY_CRYPTOLITE_SUCCESS != tmpResult)
1244 {
1245 return tmpResult;
1246 }
1247 srcSize -= cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
1248 srcRemap += cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
1249 dst += cnt * CY_CRYPTOLITE_AES_BLOCK_SIZE;
1250 }
1251
1252 if(srcSize > 0u)
1253 {
1254 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, (uint32_t*)((void*)aesState->buffers->unProcessedData), (uint32_t*)((void*)aesState->buffers->iv));
1255
1256 for( uint32_t i=0u; i<srcSize ; i++)
1257 {
1258 dst[i] = srcRemap[i] ^ aesState->buffers->unProcessedData[i];
1259 }
1260
1261 if(aesState->dirMode == CY_CRYPTOLITE_ENCRYPT)
1262 {
1263 Cy_Cryptolite_Vu_memcpy(&aesState->buffers->iv, dst, (uint16_t)srcSize);
1264 }
1265 else
1266 {
1267 Cy_Cryptolite_Vu_memcpy(&aesState->buffers->iv, srcRemap, (uint16_t)srcSize);
1268 }
1269
1270 aesState->unProcessedBytes += srcSize;
1271 srcSize -= bytes_rem;
1272
1273 tmpResult = CY_CRYPTOLITE_SUCCESS;
1274 }
1275 return tmpResult;
1276 }
1277
1278
1279 /*******************************************************************************
1280 * Function Name: Cy_Cryptolite_Aes_Cfb_Finish
1281 ****************************************************************************//*
1282 *
1283 * Performs an AES CFB finish operation.
1284 *
1285 * \param base
1286 * The pointer to the CRYPTO instance.
1287 *
1288 * \param aesState
1289 * The pointer to the AES state structure allocated by the user. The user
1290 * must not modify anything in this structure.
1291 *
1292 * \return
1293 * \ref cy_en_cryptolite_status_t
1294 *
1295 *******************************************************************************/
Cy_Cryptolite_Aes_Cfb_Finish(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)1296 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Cfb_Finish(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_state_t *aesState)
1297 {
1298 /* Input parameters verification */
1299 if ((NULL == base) || (NULL == aesState))
1300 {
1301 return CY_CRYPTOLITE_BAD_PARAMS;
1302 }
1303
1304 return CY_CRYPTOLITE_SUCCESS;
1305 }
1306
1307 #endif /*CY_CRYPTOLITE_CFG_CIPHER_MODE_CBC*/
1308
1309
1310
1311 #if defined(CY_CRYPTOLITE_CFG_CIPHER_MODE_CTR)
1312 /*******************************************************************************
1313 * Function Name: Cy_Cryptolite_Aes_Ctr
1314 ********************************************************************************
1315 *
1316 * Performs an AES Encryption/Decryption operation on a data using the counter method (CTR).
1317 *
1318 * \param base
1319 * The pointer to the CRYPTOLITE instance.
1320 *
1321 * \param srcSize
1322 * The size of a source plain text.
1323 *
1324 * \param srcOffset
1325 * The size of an offset within the current block stream for resuming within the
1326 * current cipher stream.
1327 *
1328 * \param ivPtr
1329 * The 128-bit initial vector that contains a 64-bit nonce and 64-bit counter.
1330 *
1331 * \param dst
1332 * The pointer to a destination cipher text.
1333 *
1334 * \param src
1335 * The pointer to a source plain text.
1336 *
1337 * \param aesState
1338 * The pointer to the AES state structure allocated by the user. The user must
1339 * must not modify anything in this structure.
1340 *
1341 * \return
1342 * \ref cy_en_cryptolite_status_t
1343 *
1344 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr(CRYPTOLITE_Type * base,uint32_t srcSize,uint32_t * srcOffset,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)1345 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr(CRYPTOLITE_Type *base,
1346 uint32_t srcSize,
1347 uint32_t *srcOffset,
1348 uint8_t *ivPtr,
1349 uint8_t *dst,
1350 uint8_t const *src,
1351 cy_stc_cryptolite_aes_state_t *aesState)
1352 {
1353 uint32_t i;
1354 uint32_t cnt;
1355 uint64_t counter;
1356 uint32_t *blockCounter = NULL;
1357 cy_stc_cryptolite_aes_buffers_t *aesBuffers = NULL;
1358 cy_en_cryptolite_status_t tmpResult = CY_CRYPTOLITE_BAD_PARAMS;
1359 uint32_t *srcBuff = NULL;
1360 uint32_t *dstBuff = NULL;
1361 uint32_t *streamBuff = NULL;
1362 uint8_t *srcRemap;
1363
1364 /* Input parameters verification */
1365 if ((NULL == base) || (NULL == srcOffset) || (NULL == ivPtr) || (NULL == dst) || (NULL == src) || (NULL == aesState))
1366 {
1367 return tmpResult;
1368 }
1369
1370 /*check if IP is busy*/
1371 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
1372 {
1373 return CY_CRYPTOLITE_HW_BUSY;
1374 }
1375
1376 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
1377
1378 aesBuffers = (cy_stc_cryptolite_aes_buffers_t*)aesState->buffers;
1379 srcBuff = (uint32_t*)((void *)srcRemap);
1380 dstBuff = (uint32_t*)((void *)dst);
1381 streamBuff = (uint32_t*)(aesBuffers->block2);
1382 blockCounter = (uint32_t*)(aesBuffers->block3);
1383
1384 Cy_Cryptolite_Vu_memcpy(blockCounter, ivPtr, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1385
1386 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1387 counter = CY_SWAP_ENDIAN64(*(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS));
1388
1389 cnt = (uint32_t)(srcSize / CY_CRYPTOLITE_AES_BLOCK_SIZE);
1390
1391 for (i = 0UL; i < cnt; i++)
1392 {
1393
1394 /* In this mode, (CTR) is always an encryption! */
1395 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, (uint32_t*)((void*)streamBuff), (uint32_t*)((void*)blockCounter));
1396
1397 if(CY_CRYPTOLITE_SUCCESS != tmpResult){
1398 break;
1399 }
1400 /* Increment the block counter, at least 64Bits (from 128) is the counter part */
1401 counter++;
1402 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1403 *(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS) = CY_SWAP_ENDIAN64(counter);
1404
1405 // Cy_Cryptolite_Aes_Xor(base, aesState, dstBuff, srcBuff, streamBuff);
1406 dstBuff[0] = srcBuff[0] ^ streamBuff[0];
1407 dstBuff[1] = srcBuff[1] ^ streamBuff[1];
1408 dstBuff[2] = srcBuff[2] ^ streamBuff[2];
1409 dstBuff[3] = srcBuff[3] ^ streamBuff[3];
1410
1411 srcBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1412 dstBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1413 }
1414
1415 if(CY_CRYPTOLITE_SUCCESS == tmpResult)
1416 {
1417
1418 Cy_Cryptolite_Vu_memcpy(ivPtr, blockCounter, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1419
1420 /* Save the reminder of the last non-complete block */
1421 *srcOffset = (uint32_t)(srcSize % CY_CRYPTOLITE_AES_BLOCK_SIZE);
1422 }
1423
1424 return tmpResult;
1425 }
1426
1427
1428 /*******************************************************************************
1429 * Function Name: Cy_Cryptolite_Aes_Ctr_Setup
1430 ****************************************************************************//*
1431 *
1432 * Performs an AES CTR setup operation.
1433 *
1434 * \param base
1435 * The pointer to the CRYPTOLITE instance.
1436 *
1437 * \param aesState
1438 * The pointer to the AES state structure allocated by the user. The user
1439 * must not modify anything in this structure.
1440 *
1441 * \return
1442 * \ref cy_en_cryptolite_status_t
1443 *
1444 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr_Setup(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)1445 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr_Setup(CRYPTOLITE_Type *base,
1446 cy_stc_cryptolite_aes_state_t *aesState)
1447 {
1448 /* Input parameters verification */
1449 if ((NULL == base) || (NULL == aesState) || (NULL == aesState->buffers))
1450 {
1451 return CY_CRYPTOLITE_BAD_PARAMS;
1452 }
1453
1454 aesState->unProcessedBytes = 0u;
1455 return CY_CRYPTOLITE_SUCCESS;
1456 }
1457
1458
1459 /*******************************************************************************
1460 * Function Name: Cy_Cryptolite_Aes_Ctr_Set_IV
1461 ****************************************************************************//*
1462 *
1463 * Sets IV for the AES CTR operation.
1464 *
1465 * \param base
1466 * The pointer to the CRYPTOLITE instance.
1467 *
1468 * \param iv
1469 * The pointer to iv.
1470 *
1471 * \param aesState
1472 * The pointer to the AES state structure allocated by the user. The user
1473 * must not modify anything in this structure.
1474 *
1475 * \return
1476 * \ref cy_en_cryptolite_status_t
1477 *
1478 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr_Set_IV(CRYPTOLITE_Type * base,const uint8_t * iv,cy_stc_cryptolite_aes_state_t * aesState)1479 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr_Set_IV(CRYPTOLITE_Type *base,
1480 const uint8_t *iv,
1481 cy_stc_cryptolite_aes_state_t *aesState)
1482 {
1483 uint8_t *ivRemap;
1484
1485 /* Input parameters verification */
1486 if ((NULL == base) || (NULL == iv) || (NULL == aesState))
1487 {
1488 return CY_CRYPTOLITE_BAD_PARAMS;
1489 }
1490
1491 ivRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(iv);
1492
1493 Cy_Cryptolite_Vu_memcpy( (void*)aesState->buffers->iv, (void*)ivRemap, (uint16_t)CY_CRYPTOLITE_AES_BLOCK_SIZE);
1494 return CY_CRYPTOLITE_SUCCESS;
1495 }
1496
1497
1498 /*******************************************************************************
1499 * Function Name: Cy_Cryptolite_Aes_Ctr_Update
1500 ****************************************************************************//*
1501 *
1502 * Performs an AES CTR Multistage update operation.
1503 *
1504 * \param base
1505 * The pointer to the CRYPTOLITE instance.
1506 *
1507 * \param srcSize
1508 * The size of the source block.
1509 *
1510 * \param dst
1511 * The pointer to a destination cipher block.
1512 *
1513 * \param src
1514 * The pointer to a source block.
1515 *
1516 * \param aesState
1517 * The pointer to the AES state structure allocated by the user. The user
1518 * must not modify anything in this structure.
1519 *
1520 * \return
1521 * \ref cy_en_cryptolite_status_t
1522 *
1523 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr_Update(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)1524 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr_Update(CRYPTOLITE_Type *base,
1525 uint32_t srcSize,
1526 uint8_t *dst,
1527 uint8_t const *src,
1528 cy_stc_cryptolite_aes_state_t *aesState)
1529
1530
1531 {
1532 cy_en_cryptolite_status_t status = CY_CRYPTOLITE_BAD_PARAMS;
1533 uint8_t *srcRemap;
1534
1535 if(0u == srcSize)
1536 {
1537 return CY_CRYPTOLITE_SUCCESS;
1538 }
1539
1540 /* Input parameters verification */
1541 if ((NULL == base) || (NULL == aesState) || (NULL == dst) || (NULL == src))
1542 {
1543 return status;
1544 }
1545
1546
1547 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
1548
1549 uint32_t blockCounter[CY_CRYPTOLITE_AES_BLOCK_SIZE_U32] = { 0UL };
1550 uint64_t counter;
1551 uint32_t cnt;
1552 uint32_t srcOffset;
1553
1554 Cy_Cryptolite_Vu_memcpy(blockCounter, aesState->buffers->iv, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1555
1556 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1557 counter = CY_SWAP_ENDIAN64(*(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS));
1558
1559 while(srcSize>0u)
1560 {
1561 cnt = (uint32_t)(srcSize / CY_CRYPTOLITE_AES_BLOCK_SIZE);
1562
1563 if(aesState->unProcessedBytes == 0u && cnt==0u)
1564 {
1565 status = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, (uint32_t*)((void*)aesState->buffers->unProcessedData), blockCounter);
1566 if(CY_CRYPTOLITE_SUCCESS != status)
1567 {
1568 return status;
1569 }
1570 Cy_Cryptolite_Vu_memcpy(aesState->buffers->iv, blockCounter, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1571
1572 counter++;
1573 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1574 *(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS) = CY_SWAP_ENDIAN64(counter);
1575 }
1576
1577 if(aesState->unProcessedBytes == 0u && cnt>0u)
1578 {
1579 status = Cy_Cryptolite_Aes_Ctr(CRYPTOLITE, cnt*CY_CRYPTOLITE_AES_BLOCK_SIZE, &srcOffset, aesState->buffers->iv, dst, srcRemap, aesState);
1580 if(CY_CRYPTOLITE_SUCCESS != status)
1581 {
1582 return status;
1583 }
1584
1585 srcSize -= cnt*CY_CRYPTOLITE_AES_BLOCK_SIZE;
1586 srcRemap += cnt*CY_CRYPTOLITE_AES_BLOCK_SIZE;
1587 dst += cnt*CY_CRYPTOLITE_AES_BLOCK_SIZE;
1588
1589 Cy_Cryptolite_Vu_memcpy(blockCounter, aesState->buffers->iv, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1590 continue;
1591 }
1592
1593
1594 *dst++ = *srcRemap++ ^ aesState->buffers->unProcessedData[aesState->unProcessedBytes];
1595 aesState->unProcessedBytes = (aesState->unProcessedBytes + 1u) & 0x0Fu;
1596 --srcSize;
1597 status = CY_CRYPTOLITE_SUCCESS;
1598
1599 }
1600
1601 Cy_Cryptolite_Vu_memcpy( aesState->buffers->iv, blockCounter, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1602
1603 return status;
1604 }
1605 /*******************************************************************************
1606 * Function Name: Cy_Cryptolite_Aes_Ctr_Finish
1607 ****************************************************************************//*
1608 *
1609 * Performs an AES CTR Finish operation.
1610 *
1611 * \param base
1612 * The pointer to the CRYPTOLITE instance.
1613 *
1614 * \param aesState
1615 * The pointer to the AES state structure allocated by the user. The user
1616 * must not modify anything in this structure.
1617 *
1618 * \return
1619 * \ref cy_en_cryptolite_status_t
1620 *
1621 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr_Finish(CRYPTOLITE_Type * base,cy_stc_cryptolite_aes_state_t * aesState)1622 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr_Finish(CRYPTOLITE_Type *base, cy_stc_cryptolite_aes_state_t *aesState)
1623 {
1624 /* Input parameters verification */
1625 if ((NULL == base) || (NULL == aesState))
1626 {
1627 return CY_CRYPTOLITE_BAD_PARAMS;
1628 }
1629
1630 return CY_CRYPTOLITE_SUCCESS;
1631 }
1632
1633 /*******************************************************************************
1634 * Function Name: Cy_Cryptolite_Aes_Ctr_Zeropad
1635 ********************************************************************************
1636 *
1637 * Performs an AES Encryption operation with zero bytes padding for the last block(Not multiple of Aes block size) using the counter method (CTR).
1638 *
1639 * \param base
1640 * The pointer to the CRYPTOLITE instance.
1641 *
1642 * \param srcSize
1643 * The size of a source plain text.
1644 *
1645 * \param srcOffset
1646 * The size of an offset within the current block stream for resuming within the
1647 * current cipher stream.
1648 *
1649 * \param ivPtr
1650 * The 128-bit initial vector that contains a 64-bit nonce and 64-bit counter.
1651 *
1652 * \param dst
1653 * The pointer to a destination cipher text.
1654 *
1655 * \param src
1656 * The pointer to a source plain text.
1657 *
1658 * \param aesState
1659 * The pointer to the AES state structure allocated by the user. The user must
1660 * must not modify anything in this structure.
1661 *
1662 * \return
1663 * \ref cy_en_cryptolite_status_t
1664 *
1665 *******************************************************************************/
Cy_Cryptolite_Aes_Ctr_Zeropad(CRYPTOLITE_Type * base,uint32_t srcSize,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_cryptolite_aes_state_t * aesState)1666 cy_en_cryptolite_status_t Cy_Cryptolite_Aes_Ctr_Zeropad(CRYPTOLITE_Type *base,
1667 uint32_t srcSize,
1668 uint8_t *ivPtr,
1669 uint8_t *dst,
1670 uint8_t const *src,
1671 cy_stc_cryptolite_aes_state_t *aesState)
1672 {
1673 uint32_t i;
1674 uint32_t cnt;
1675 uint64_t counter;
1676 uint32_t *blockCounter = NULL;
1677 cy_stc_cryptolite_aes_buffers_t *aesBuffers = NULL;
1678 uint32_t *srcBuff = NULL;
1679 uint32_t *dstBuff = NULL;
1680 uint32_t *streamBuff = NULL;
1681 uint32_t *tempBuf = NULL;
1682 uint32_t *tempBuf1 = NULL;
1683 uint8_t *srcRemap;
1684 uint32_t srcRemainder = 0;
1685
1686 cy_en_cryptolite_status_t tmpResult = CY_CRYPTOLITE_SUCCESS;
1687
1688 /* Input parameters verification */
1689 if ((NULL == base) || (NULL == ivPtr) || (NULL == dst) || (NULL == src) || (NULL == aesState))
1690 {
1691 return CY_CRYPTOLITE_BAD_PARAMS;
1692 }
1693
1694 /*check if IP is busy*/
1695 if ((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0UL)
1696 {
1697 return CY_CRYPTOLITE_HW_BUSY;
1698 }
1699
1700 srcRemap = (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
1701
1702 aesBuffers = (cy_stc_cryptolite_aes_buffers_t*)aesState->buffers;
1703 srcBuff = (uint32_t*)((void*)srcRemap);
1704 dstBuff = (uint32_t*)((void*)dst);
1705 streamBuff = (uint32_t*)(aesBuffers->block0);
1706 tempBuf = (uint32_t*)(aesBuffers->block1);
1707 tempBuf1 = (uint32_t*)(aesBuffers->block2);
1708 blockCounter = (uint32_t*)(aesBuffers->block3);
1709
1710 Cy_Cryptolite_Vu_memcpy(blockCounter, ivPtr, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1711
1712 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1713 counter = CY_SWAP_ENDIAN64(*(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS));
1714
1715 cnt = (uint32_t)(srcSize / CY_CRYPTOLITE_AES_BLOCK_SIZE);
1716
1717 for (i = 0UL; i < cnt; i++)
1718 {
1719
1720 /* In this mode, (CTR) is always an encryption! */
1721 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, streamBuff, blockCounter);
1722
1723 if(CY_CRYPTOLITE_SUCCESS != tmpResult){
1724 break;
1725 }
1726 /* Increment the block counter, at least 64Bits (from 128) is the counter part */
1727 counter++;
1728 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1729 *(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS) = CY_SWAP_ENDIAN64(counter);
1730
1731 // Cy_Cryptolite_Aes_Xor(base, aesState, dstBuff, srcBuff, streamBuff);
1732 dstBuff[0] = srcBuff[0] ^ streamBuff[0];
1733 dstBuff[1] = srcBuff[1] ^ streamBuff[1];
1734 dstBuff[2] = srcBuff[2] ^ streamBuff[2];
1735 dstBuff[3] = srcBuff[3] ^ streamBuff[3];
1736
1737 srcBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1738 dstBuff += CY_CRYPTOLITE_AES_MAX_KEY_SIZE_U32;
1739 }
1740
1741 srcRemainder = (uint32_t)(srcSize % CY_CRYPTOLITE_AES_BLOCK_SIZE);
1742
1743 if ((srcRemainder != 0u) && (CY_CRYPTOLITE_SUCCESS == tmpResult))
1744 {
1745 /* source message block */
1746 Cy_Cryptolite_Vu_memcpy(tempBuf, srcBuff, srcRemainder);
1747
1748 /* zeropad source message block */
1749 Cy_Cryptolite_Vu_memset((uint8_t*)tempBuf + srcRemainder, 0u, CY_CRYPTOLITE_AES_BLOCK_SIZE - srcRemainder);
1750
1751 /* In this mode, (CTR) is always an encryption! */
1752 tmpResult = Cy_Cryptolite_Aes_ProcessBlock(base, aesState, streamBuff, blockCounter);
1753
1754 if(CY_CRYPTOLITE_SUCCESS == tmpResult)
1755 {
1756 /* Increment the block counter, at least 64Bits (from 128) is the counter part */
1757 counter++;
1758 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Intentional pointer type conversion');
1759 *(uint64_t*)(blockCounter + CY_CRYPTO_AES_CTR_CNT_POS) = CY_SWAP_ENDIAN64(counter);
1760
1761 // Cy_Cryptolite_Aes_Xor(base, aesState, dstBuff, srcBuff, streamBuff);
1762 tempBuf1[0] = tempBuf[0] ^ streamBuff[0];
1763 tempBuf1[1] = tempBuf[1] ^ streamBuff[1];
1764 tempBuf1[2] = tempBuf[2] ^ streamBuff[2];
1765 tempBuf1[3] = tempBuf[3] ^ streamBuff[3];
1766
1767 /* destination cipher text block */
1768 Cy_Cryptolite_Vu_memcpy(dstBuff, tempBuf1, srcRemainder);
1769 }
1770
1771 }
1772
1773 if(CY_CRYPTOLITE_SUCCESS == tmpResult)
1774 {
1775 Cy_Cryptolite_Vu_memcpy(ivPtr, blockCounter, CY_CRYPTOLITE_AES_BLOCK_SIZE);
1776 }
1777
1778 return tmpResult;
1779 }
1780
1781 #endif /*CY_CRYPTOLITE_CFG_CIPHER_MODE_CBC*/
1782
1783 #endif
1784 #endif
1785
1786 #if defined(__cplusplus)
1787 }
1788 #endif
1789
1790
1791 #endif /* CY_IP_MXCRYPTOLITE */
1792
1793
1794 /* [] END OF FILE */
1795