1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_ltc_edma.h"
10 
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.ltc_edma"
18 #endif
19 
20 /*<! Structure definition for ltc_edma_private_handle_t. The structure is private. */
21 typedef struct _ltc_edma_private_handle
22 {
23     LTC_Type *base;
24     ltc_edma_handle_t *handle;
25 } ltc_edma_private_handle_t;
26 
27 /*******************************************************************************
28  * Variables
29  ******************************************************************************/
30 
31 /*<! Private handle only used for internally. */
32 static ltc_edma_private_handle_t s_edmaPrivateHandle[FSL_FEATURE_SOC_LTC_COUNT];
33 
34 /* Array of LTC peripheral base address. */
35 static LTC_Type *const s_ltcBase[] = LTC_BASE_PTRS;
36 
37 /*******************************************************************************
38  * Variables
39  ******************************************************************************/
40 
41 /* State machine state.*/
42 #define LTC_SM_STATE_START  0x0000u
43 #define LTC_SM_STATE_FINISH 0xFFFFu
44 
45 #define LTC_FIFO_SZ_MAX_DOWN_ALGN (0xff0u)
46 
47 enum _ltc_edma_md_dk_bit_shift
48 {
49     kLTC_ModeRegBitShiftDK = 12U,
50 };
51 
52 /*******************************************************************************
53  * Prototypes
54  ******************************************************************************/
55 static uint32_t LTC_GetInstance(LTC_Type *base);
56 static void ltc_symmetric_process_EDMA(LTC_Type *base, uint32_t inSize, const uint8_t **inData, uint8_t **outData);
57 static status_t ltc_process_message_in_sessions_EDMA(LTC_Type *base, ltc_edma_handle_t *handle);
58 
59 /*******************************************************************************
60  * Code
61  ******************************************************************************/
62 
63 /*******************************************************************************
64  * LTC Common code static
65  ******************************************************************************/
66 
67 /*!
68  * @brief Splits the LTC job into sessions. Used for CBC, CTR, CFB, OFB cipher block modes.
69  *
70  * @param base LTC peripheral base address
71  * @param inData Input data to process.
72  * @param inSize Input size of the input buffer.
73  * @param outData Output data buffer.
74  */
ltc_process_message_in_sessions_EDMA(LTC_Type * base,ltc_edma_handle_t * handle)75 static status_t ltc_process_message_in_sessions_EDMA(LTC_Type *base, ltc_edma_handle_t *handle)
76 {
77     status_t retval;
78     bool exit_sm = false;
79 
80     handle->modeReg = base->MD;
81     retval          = kStatus_Success;
82 
83     if ((NULL == handle->inData) || (NULL == handle->outData))
84     {
85         handle->state = LTC_SM_STATE_FINISH; /* END */
86         retval        = kStatus_InvalidArgument;
87     }
88 
89     while (exit_sm == false)
90     {
91         switch (handle->state)
92         {
93             case LTC_SM_STATE_START:
94                 if (0U != handle->size)
95                 {
96                     uint32_t sz;
97 
98                     if (handle->size <= LTC_FIFO_SZ_MAX_DOWN_ALGN)
99                     {
100                         sz = handle->size;
101                     }
102                     else
103                     {
104                         sz = LTC_FIFO_SZ_MAX_DOWN_ALGN;
105                     }
106 
107                     /* retval = ltc_symmetric_process_data_EDMA(base, handle->inData, sz, handle->outData); */
108                     {
109                         uint32_t lastSize;
110                         uint32_t inSize = sz;
111 
112                         /* Write the data size. */
113                         base->DS = inSize;
114 
115                         /* Split the inSize into full 16-byte chunks and last incomplete block due to LTC AES OFIFO
116                          * errata */
117                         if (inSize <= 16u)
118                         {
119                             lastSize = inSize;
120                             inSize   = 0;
121                         }
122                         else
123                         {
124                             /* Process all 16-byte data chunks. */
125                             lastSize = inSize % 16u;
126                             if (lastSize == 0U)
127                             {
128                                 lastSize = 16;
129                                 inSize -= 16U;
130                             }
131                             else
132                             {
133                                 inSize -= lastSize; /* inSize will be rounded down to 16 byte boundary. remaining bytes
134                                                        in lastSize */
135                             }
136                         }
137 
138                         if (0U != inSize)
139                         {
140                             handle->size -= inSize;
141                             ltc_symmetric_process_EDMA(base, inSize, &handle->inData, &handle->outData);
142                             exit_sm = true;
143                         }
144                         else if (0U != lastSize)
145                         {
146                             ltc_symmetric_process(base, lastSize, &handle->inData, &handle->outData);
147                             retval = ltc_wait(base);
148                             handle->size -= lastSize;
149                         }
150                         else
151                         {
152                             /* Intentional empty */
153                         }
154                     }
155                 }
156                 else
157                 {
158                     handle->state = LTC_SM_STATE_FINISH;
159                 }
160                 break;
161             case LTC_SM_STATE_FINISH:
162             default:
163                 base->MD = handle->modeReg;
164 
165                 ltc_clear_all(base, false);
166 
167                 if (NULL != handle->callback)
168                 {
169                     handle->callback(base, handle, retval, handle->userData);
170                 }
171                 exit_sm = true;
172                 break;
173         }
174     }
175 
176     return retval;
177 }
178 
179 /*!
180  * @brief Splits the LTC job into sessions. Used for CBC, CTR, CFB, OFB cipher block modes.
181  *
182  * @param base LTC peripheral base address
183  * @param inData Input data to process.
184  * @param inSize Input size of the input buffer.
185  * @param outData Output data buffer.
186  */
ltc_process_message_in_sessions_ctr_EDMA(LTC_Type * base,ltc_edma_handle_t * handle)187 static status_t ltc_process_message_in_sessions_ctr_EDMA(LTC_Type *base, ltc_edma_handle_t *handle)
188 {
189     status_t retval;
190     bool exit_sm = false;
191 
192     handle->modeReg = base->MD;
193     retval          = kStatus_Success;
194 
195     if ((NULL == handle->inData) || (NULL == handle->outData))
196     {
197         handle->state = LTC_SM_STATE_FINISH;
198         retval        = kStatus_InvalidArgument;
199     }
200 
201     while (exit_sm == false)
202     {
203         switch (handle->state)
204         {
205             case LTC_SM_STATE_START:
206                 if (0U != handle->size)
207                 {
208                     uint32_t sz;
209 
210                     if (handle->size <= LTC_FIFO_SZ_MAX_DOWN_ALGN)
211                     {
212                         sz = handle->size;
213                     }
214                     else
215                     {
216                         sz = LTC_FIFO_SZ_MAX_DOWN_ALGN;
217                     }
218 
219                     /* retval = ltc_symmetric_process_data_EDMA(base, handle->inData, sz, handle->outData); */
220                     {
221                         uint32_t lastSize;
222                         uint32_t inSize = sz;
223 
224                         /* Write the data size. */
225                         base->DS = inSize;
226 
227                         /* Split the inSize into full 16-byte chunks and last incomplete block due to LTC AES OFIFO
228                          * errata */
229                         if (inSize <= 16u)
230                         {
231                             lastSize = inSize;
232                             inSize   = 0;
233                         }
234                         else
235                         {
236                             /* Process all 16-byte data chunks. */
237                             lastSize = inSize % 16u;
238                             if (lastSize == 0U)
239                             {
240                                 lastSize = 16;
241                                 inSize -= 16U;
242                             }
243                             else
244                             {
245                                 inSize -= lastSize; /* inSize will be rounded down to 16 byte boundary. remaining bytes
246                                                        in lastSize */
247                             }
248                         }
249 
250                         if (0U != inSize)
251                         {
252                             handle->size -= inSize;
253                             ltc_symmetric_process_EDMA(base, inSize, &handle->inData, &handle->outData);
254                             exit_sm = true;
255                         }
256                         else if (0U != lastSize)
257                         {
258                             ltc_symmetric_process(base, lastSize, &handle->inData, &handle->outData);
259                             retval = ltc_wait(base);
260                             handle->size -= lastSize;
261                         }
262                         else
263                         {
264                             /* Add this to fix MISRA C2012 rule15.7 issue: Empty else without comment. */
265                         }
266                     }
267                 }
268                 else
269                 {
270                     handle->state = LTC_SM_STATE_FINISH;
271                 }
272                 break;
273             case LTC_SM_STATE_FINISH:
274             default:
275                 base->MD = handle->modeReg;
276 
277                 /* CTR final phase.*/
278                 if (kStatus_Success == retval)
279                 {
280                     const uint8_t *input = handle->inData;
281                     uint8_t *output      = handle->outData;
282 
283                     if ((handle->counterlast != NULL) && (0U != handle->lastSize))
284                     {
285                         uint8_t zeroes[16] = {0};
286                         ltc_mode_t modeReg;
287 
288                         modeReg = (uint32_t)kLTC_AlgorithmAES | (uint32_t)kLTC_ModeCTR | (uint32_t)kLTC_ModeEncrypt;
289                         /* Write the mode register to the hardware. */
290                         base->MD = modeReg | (uint32_t)kLTC_ModeFinalize;
291 
292                         /* context is re-used (CTRi) */
293 
294                         /* Process data and return status. */
295                         retval = ltc_symmetric_process_data(base, input, handle->lastSize, output);
296                         if (kStatus_Success == retval)
297                         {
298                             if (NULL != handle->szLeft)
299                             {
300                                 *handle->szLeft = 16U - handle->lastSize;
301                             }
302 
303                             /* Initialize algorithm state. */
304                             base->MD = modeReg | (uint32_t)kLTC_ModeUpdate;
305 
306                             /* context is re-used (CTRi) */
307 
308                             /* Process data and return status. */
309                             retval = ltc_symmetric_process_data(base, zeroes, 16U, handle->counterlast);
310                         }
311                     }
312                     if (kStatus_Success == retval)
313                     {
314                         retval = ltc_get_context(base, &handle->counter[0], 16U, 4U);
315 
316                         ltc_clear_all(base, false);
317                     }
318                 }
319 
320                 if (NULL != handle->callback)
321                 {
322                     handle->callback(base, handle, retval, handle->userData);
323                 }
324 
325                 exit_sm = true;
326                 break;
327         }
328     }
329 
330     return retval;
331 }
332 
333 /*******************************************************************************
334  * AES Code public
335  ******************************************************************************/
336 
337 /*!
338  * brief Encrypts AES using the ECB block mode.
339  *
340  * Encrypts AES using the ECB block mode.
341  *
342  * param base LTC peripheral base address
343  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
344  * param plaintext Input plain text to encrypt
345  * param[out] ciphertext Output cipher text
346  * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
347  * param key Input key to use for encryption
348  * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
349  * return Status from encrypt operation
350  */
LTC_AES_EncryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t * key,uint32_t keySize)351 status_t LTC_AES_EncryptEcbEDMA(LTC_Type *base,
352                                 ltc_edma_handle_t *handle,
353                                 const uint8_t *plaintext,
354                                 uint8_t *ciphertext,
355                                 uint32_t size,
356                                 const uint8_t *key,
357                                 uint32_t keySize)
358 {
359     status_t retval;
360 
361     if (((uint32_t)(ltc_check_key_size(keySize)) == 0U) || (size < 16u) ||
362         (0U != (size % 16u))) /* ECB mode, size must be 16-byte multiple */
363     {
364         if (NULL != handle->callback)
365         {
366             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
367         }
368 
369         return kStatus_InvalidArgument;
370     }
371 
372     /* Initialize algorithm state. */
373     retval = ltc_symmetric_update(base, key, (uint8_t)keySize, kLTC_AlgorithmAES, kLTC_ModeECB, kLTC_ModeEncrypt);
374     if (kStatus_Success != retval)
375     {
376         return retval;
377     }
378 
379     /* Process data and return status. */
380     handle->inData        = &plaintext[0];
381     handle->outData       = &ciphertext[0];
382     handle->size          = size;
383     handle->state         = LTC_SM_STATE_START;
384     handle->state_machine = ltc_process_message_in_sessions_EDMA;
385     retval                = handle->state_machine(base, handle);
386     return retval;
387 }
388 
389 /*!
390  * brief Decrypts AES using ECB block mode.
391  *
392  * Decrypts AES using ECB block mode.
393  *
394  * param base LTC peripheral base address
395  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
396  * param ciphertext Input cipher text to decrypt
397  * param[out] plaintext Output plain text
398  * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
399  * param key Input key.
400  * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
401  * param keyType Input type of the key (allows to directly load decrypt key for AES ECB decrypt operation.)
402  * return Status from decrypt operation
403  */
LTC_AES_DecryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t * key,uint32_t keySize,ltc_aes_key_t keyType)404 status_t LTC_AES_DecryptEcbEDMA(LTC_Type *base,
405                                 ltc_edma_handle_t *handle,
406                                 const uint8_t *ciphertext,
407                                 uint8_t *plaintext,
408                                 uint32_t size,
409                                 const uint8_t *key,
410                                 uint32_t keySize,
411                                 ltc_aes_key_t keyType)
412 {
413     status_t status;
414 
415     if (((uint32_t)(ltc_check_key_size(keySize)) == 0u) || (size < 16u) ||
416         (0U != (size % 16u))) /* ECB mode, size must be 16-byte multiple */
417     {
418         if (NULL != handle->callback)
419         {
420             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
421         }
422 
423         return kStatus_InvalidArgument;
424     }
425 
426     /* Initialize algorithm state. */
427     status = ltc_symmetric_update(base, key, (uint8_t)keySize, kLTC_AlgorithmAES, kLTC_ModeECB, kLTC_ModeDecrypt);
428     if (kStatus_Success != status)
429     {
430         return status;
431     }
432 
433     /* set DK bit in the LTC Mode Register AAI field for directly loaded decrypt keys */
434     if (keyType == kLTC_DecryptKey)
435     {
436         uint32_t u32mask = 1;
437         base->MD |= (u32mask << (uint32_t)kLTC_ModeRegBitShiftDK);
438     }
439 
440     /* Process data and return status. */
441     handle->inData        = &ciphertext[0];
442     handle->outData       = &plaintext[0];
443     handle->size          = size;
444     handle->state         = LTC_SM_STATE_START;
445     handle->state_machine = ltc_process_message_in_sessions_EDMA;
446     status                = handle->state_machine(base, handle);
447 
448     return status;
449 }
450 
451 /*!
452  * brief Encrypts AES using CBC block mode.
453  *
454  * param base LTC peripheral base address
455  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
456  * param plaintext Input plain text to encrypt
457  * param[out] ciphertext Output cipher text
458  * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
459  * param iv Input initial vector to combine with the first input block.
460  * param key Input key to use for encryption
461  * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
462  * return Status from encrypt operation
463  */
LTC_AES_EncryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_AES_IV_SIZE],const uint8_t * key,uint32_t keySize)464 status_t LTC_AES_EncryptCbcEDMA(LTC_Type *base,
465                                 ltc_edma_handle_t *handle,
466                                 const uint8_t *plaintext,
467                                 uint8_t *ciphertext,
468                                 uint32_t size,
469                                 const uint8_t iv[LTC_AES_IV_SIZE],
470                                 const uint8_t *key,
471                                 uint32_t keySize)
472 {
473     status_t retval;
474 
475     if (!(ltc_check_key_size(keySize)) || (size < 16u) ||
476         (0U != (size % 16u))) /* CBC mode, size must be 16-byte multiple */
477     {
478         if (NULL != handle->callback)
479         {
480             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
481         }
482 
483         return kStatus_InvalidArgument;
484     }
485 
486     /* Initialize algorithm state. */
487     retval = ltc_symmetric_update(base, key, (uint8_t)keySize, kLTC_AlgorithmAES, kLTC_ModeCBC, kLTC_ModeEncrypt);
488     if (kStatus_Success != retval)
489     {
490         return retval;
491     }
492 
493     /* Write IV data to the context register. */
494     retval = ltc_set_context(base, &iv[0], LTC_AES_IV_SIZE, 0);
495     if (kStatus_Success != retval)
496     {
497         return retval;
498     }
499 
500     /* Process data and return status. */
501     handle->inData        = &plaintext[0];
502     handle->outData       = &ciphertext[0];
503     handle->size          = size;
504     handle->state         = LTC_SM_STATE_START;
505     handle->state_machine = ltc_process_message_in_sessions_EDMA;
506     retval                = handle->state_machine(base, handle);
507     return retval;
508 }
509 
510 /*!
511  * brief Decrypts AES using CBC block mode.
512  *
513  * param base LTC peripheral base address
514  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
515  * param ciphertext Input cipher text to decrypt
516  * param[out] plaintext Output plain text
517  * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
518  * param iv Input initial vector to combine with the first input block.
519  * param key Input key to use for decryption
520  * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
521  * param keyType Input type of the key (allows to directly load decrypt key for AES CBC decrypt operation.)
522  * return Status from decrypt operation
523  */
LTC_AES_DecryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_AES_IV_SIZE],const uint8_t * key,uint32_t keySize,ltc_aes_key_t keyType)524 status_t LTC_AES_DecryptCbcEDMA(LTC_Type *base,
525                                 ltc_edma_handle_t *handle,
526                                 const uint8_t *ciphertext,
527                                 uint8_t *plaintext,
528                                 uint32_t size,
529                                 const uint8_t iv[LTC_AES_IV_SIZE],
530                                 const uint8_t *key,
531                                 uint32_t keySize,
532                                 ltc_aes_key_t keyType)
533 {
534     status_t retval;
535 
536     if (((ltc_check_key_size(keySize)) == false) || (size < 16u) ||
537         (0U != (size % 16u))) /* CBC mode, size must be 16-byte multiple */
538     {
539         if (NULL != handle->callback)
540         {
541             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
542         }
543 
544         return kStatus_InvalidArgument;
545     }
546 
547     /* set DK bit in the LTC Mode Register AAI field for directly loaded decrypt keys */
548     if (keyType == kLTC_DecryptKey)
549     {
550         uint32_t u32mask = 1;
551         base->MD |= (u32mask << (uint32_t)kLTC_ModeRegBitShiftDK);
552     }
553 
554     /* Initialize algorithm state. */
555     retval = ltc_symmetric_update(base, key, (uint8_t)keySize, kLTC_AlgorithmAES, kLTC_ModeCBC, kLTC_ModeDecrypt);
556     if (kStatus_Success != retval)
557     {
558         return retval;
559     }
560 
561     /* Write IV data to the context register. */
562     retval = ltc_set_context(base, &iv[0], LTC_AES_IV_SIZE, 0);
563     if (kStatus_Success != retval)
564     {
565         return retval;
566     }
567 
568     /* Process data and return status. */
569     handle->inData        = &ciphertext[0];
570     handle->outData       = &plaintext[0];
571     handle->size          = size;
572     handle->state         = LTC_SM_STATE_START;
573     handle->state_machine = ltc_process_message_in_sessions_EDMA;
574     retval                = handle->state_machine(base, handle);
575     return retval;
576 }
577 
578 /*!
579  * brief Encrypts or decrypts AES using CTR block mode.
580  *
581  * Encrypts or decrypts AES using CTR block mode.
582  * AES CTR mode uses only forward AES cipher and same algorithm for encryption and decryption.
583  * The only difference between encryption and decryption is that, for encryption, the input argument
584  * is plain text and the output argument is cipher text. For decryption, the input argument is cipher text
585  * and the output argument is plain text.
586  *
587  * param base LTC peripheral base address
588  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
589  * param input Input data for CTR block mode
590  * param[out] output Output data for CTR block mode
591  * param size Size of input and output data in bytes
592  * param[in,out] counter Input counter (updates on return)
593  * param key Input key to use for forward AES cipher
594  * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
595  * param[out] counterlast Output cipher of last counter, for chained CTR calls. NULL can be passed if chained calls are
596  * not used.
597  * param[out] szLeft Output number of bytes in left unused in counterlast block. NULL can be passed if chained calls
598  * are not used.
599  * return Status from encrypt operation
600  */
LTC_AES_CryptCtrEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * input,uint8_t * output,uint32_t size,uint8_t counter[LTC_AES_BLOCK_SIZE],const uint8_t * key,uint32_t keySize,uint8_t counterlast[LTC_AES_BLOCK_SIZE],uint32_t * szLeft)601 status_t LTC_AES_CryptCtrEDMA(LTC_Type *base,
602                               ltc_edma_handle_t *handle,
603                               const uint8_t *input,
604                               uint8_t *output,
605                               uint32_t size,
606                               uint8_t counter[LTC_AES_BLOCK_SIZE],
607                               const uint8_t *key,
608                               uint32_t keySize,
609                               uint8_t counterlast[LTC_AES_BLOCK_SIZE],
610                               uint32_t *szLeft)
611 {
612     status_t retval;
613     uint32_t lastSize;
614 
615     if (!ltc_check_key_size(keySize))
616     {
617         if (NULL != handle->callback)
618         {
619             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
620         }
621         return kStatus_InvalidArgument;
622     }
623 
624     lastSize = 0U;
625     if (counterlast != NULL)
626     {
627         /* Split the size into full 16-byte chunks and last incomplete block due to LTC AES OFIFO errata */
628         if (size <= 16U)
629         {
630             lastSize = size;
631             size     = 0U;
632         }
633         else
634         {
635             /* Process all 16-byte data chunks. */
636             lastSize = size % 16U;
637             if (lastSize == 0U)
638             {
639                 lastSize = 16U;
640                 size -= 16U;
641             }
642             else
643             {
644                 size -= lastSize; /* size will be rounded down to 16 byte boundary. remaining bytes in lastSize */
645             }
646         }
647     }
648 
649     /* Initialize algorithm state. */
650     retval = ltc_symmetric_update(base, key, (uint8_t)keySize, kLTC_AlgorithmAES, kLTC_ModeCTR, kLTC_ModeEncrypt);
651     if (kStatus_Success != retval)
652     {
653         return retval;
654     }
655 
656     /* Write initial counter data to the context register.
657      * NOTE the counter values start at 4-bytes offset into the context. */
658     retval = ltc_set_context(base, &counter[0], 16U, 4U);
659     if (kStatus_Success != retval)
660     {
661         return retval;
662     }
663 
664     /* Process data and return status. */
665     handle->inData        = &input[0];
666     handle->outData       = &output[0];
667     handle->size          = size;
668     handle->state         = LTC_SM_STATE_START;
669     handle->state_machine = ltc_process_message_in_sessions_ctr_EDMA;
670 
671     handle->counter     = counter;
672     handle->key         = key;
673     handle->keySize     = keySize;
674     handle->counterlast = counterlast;
675     handle->szLeft      = szLeft;
676     handle->lastSize    = lastSize;
677     retval              = handle->state_machine(base, handle);
678 
679     return retval;
680 }
681 
682 #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES
683 /*******************************************************************************
684  * DES / 3DES Code static
685  ******************************************************************************/
ltc_des_process_EDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * input,uint8_t * output,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE],ltc_mode_symmetric_alg_t modeAs,ltc_mode_encrypt_t modeEnc)686 static status_t ltc_des_process_EDMA(LTC_Type *base,
687                                      ltc_edma_handle_t *handle,
688                                      const uint8_t *input,
689                                      uint8_t *output,
690                                      uint32_t size,
691                                      const uint8_t iv[LTC_DES_IV_SIZE],
692                                      const uint8_t key[LTC_DES_KEY_SIZE],
693                                      ltc_mode_symmetric_alg_t modeAs,
694                                      ltc_mode_encrypt_t modeEnc)
695 {
696     status_t retval;
697 
698     /* all but OFB, size must be 8-byte multiple */
699     if ((modeAs != kLTC_ModeOFB) && ((size < 8u) || (0U != (size % 8u))))
700     {
701         if (NULL != handle->callback)
702         {
703             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
704         }
705         return kStatus_InvalidArgument;
706     }
707 
708     /* Initialize algorithm state. */
709     retval = ltc_symmetric_update(base, &key[0], LTC_DES_KEY_SIZE, kLTC_AlgorithmDES, modeAs, modeEnc);
710     if (kStatus_Success != retval)
711     {
712         return retval;
713     }
714 
715     if ((modeAs != kLTC_ModeECB))
716     {
717         retval = ltc_set_context(base, iv, LTC_DES_IV_SIZE, 0);
718         if (kStatus_Success != retval)
719         {
720             return retval;
721         }
722     }
723 
724     /* Process data and return status. */
725     handle->inData        = input;
726     handle->outData       = output;
727     handle->size          = size;
728     handle->state         = LTC_SM_STATE_START;
729     handle->state_machine = ltc_process_message_in_sessions_EDMA;
730     retval                = handle->state_machine(base, handle);
731 
732     return retval;
733 }
734 
ltc_3des_process_EDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * input,uint8_t * output,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE],ltc_mode_symmetric_alg_t modeAs,ltc_mode_encrypt_t modeEnc)735 static status_t ltc_3des_process_EDMA(LTC_Type *base,
736                                       ltc_edma_handle_t *handle,
737                                       const uint8_t *input,
738                                       uint8_t *output,
739                                       uint32_t size,
740                                       const uint8_t iv[LTC_DES_IV_SIZE],
741                                       const uint8_t key1[LTC_DES_KEY_SIZE],
742                                       const uint8_t key2[LTC_DES_KEY_SIZE],
743                                       const uint8_t key3[LTC_DES_KEY_SIZE],
744                                       ltc_mode_symmetric_alg_t modeAs,
745                                       ltc_mode_encrypt_t modeEnc)
746 {
747     status_t retval;
748     uint8_t key[LTC_DES_KEY_SIZE * 3];
749     uint8_t keySize = LTC_DES_KEY_SIZE * 2;
750 
751     retval = ltc_3des_check_input_args(modeAs, size, key1, key2);
752     if (kStatus_Success != retval)
753     {
754         if (NULL != handle->callback)
755         {
756             handle->callback(base, handle, kStatus_InvalidArgument, handle->userData);
757         }
758         return retval;
759     }
760 
761     ltc_memcpy(&key[0], &key1[0], LTC_DES_KEY_SIZE);
762     ltc_memcpy(&key[LTC_DES_KEY_SIZE], &key2[0], LTC_DES_KEY_SIZE);
763     if (NULL != key3)
764     {
765         ltc_memcpy(&key[LTC_DES_KEY_SIZE * 2], &key3[0], LTC_DES_KEY_SIZE);
766         keySize = (uint8_t)sizeof(key);
767     }
768 
769     /* Initialize algorithm state. */
770     retval = ltc_symmetric_update(base, &key[0], keySize, kLTC_Algorithm3DES, modeAs, modeEnc);
771     if (kStatus_Success != retval)
772     {
773         return retval;
774     }
775 
776     if ((modeAs != kLTC_ModeECB))
777     {
778         retval = ltc_set_context(base, iv, LTC_DES_IV_SIZE, 0);
779         if (kStatus_Success != retval)
780         {
781             return retval;
782         }
783     }
784 
785     /* Process data and return status. */
786     handle->inData        = input;
787     handle->outData       = output;
788     handle->size          = size;
789     handle->state         = LTC_SM_STATE_START;
790     handle->state_machine = ltc_process_message_in_sessions_EDMA;
791     retval                = handle->state_machine(base, handle);
792 
793     return retval;
794 }
795 /*******************************************************************************
796  * DES / 3DES Code public
797  ******************************************************************************/
798 /*!
799  * brief Encrypts DES using ECB block mode.
800  *
801  * Encrypts DES using ECB block mode.
802  *
803  * param base LTC peripheral base address
804  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
805  * param plaintext Input plaintext to encrypt
806  * param[out] ciphertext Output ciphertext
807  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
808  * param key Input key to use for encryption
809  * return Status from encrypt/decrypt operation
810  */
LTC_DES_EncryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t key[LTC_DES_KEY_SIZE])811 status_t LTC_DES_EncryptEcbEDMA(LTC_Type *base,
812                                 ltc_edma_handle_t *handle,
813                                 const uint8_t *plaintext,
814                                 uint8_t *ciphertext,
815                                 uint32_t size,
816                                 const uint8_t key[LTC_DES_KEY_SIZE])
817 {
818     return ltc_des_process_EDMA(base, handle, plaintext, ciphertext, size, NULL, key, kLTC_ModeECB, kLTC_ModeEncrypt);
819 }
820 
821 /*!
822  * brief Decrypts DES using ECB block mode.
823  *
824  * Decrypts DES using ECB block mode.
825  *
826  * param base LTC peripheral base address
827  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
828  * param ciphertext Input ciphertext to decrypt
829  * param[out] plaintext Output plaintext
830  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
831  * param key Input key to use for decryption
832  * return Status from encrypt/decrypt operation
833  */
LTC_DES_DecryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t key[LTC_DES_KEY_SIZE])834 status_t LTC_DES_DecryptEcbEDMA(LTC_Type *base,
835                                 ltc_edma_handle_t *handle,
836                                 const uint8_t *ciphertext,
837                                 uint8_t *plaintext,
838                                 uint32_t size,
839                                 const uint8_t key[LTC_DES_KEY_SIZE])
840 {
841     return ltc_des_process_EDMA(base, handle, ciphertext, plaintext, size, NULL, key, kLTC_ModeECB, kLTC_ModeDecrypt);
842 }
843 
844 /*!
845  * brief Encrypts DES using CBC block mode.
846  *
847  * Encrypts DES using CBC block mode.
848  *
849  * param base LTC peripheral base address
850  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
851  * param plaintext Input plaintext to encrypt
852  * param[out] ciphertext Ouput ciphertext
853  * param size Size of input and output data in bytes
854  * param iv Input initial vector to combine with the first plaintext block.
855  *           The iv does not need to be secret, but it must be unpredictable.
856  * param key Input key to use for encryption
857  * return Status from encrypt/decrypt operation
858  */
LTC_DES_EncryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])859 status_t LTC_DES_EncryptCbcEDMA(LTC_Type *base,
860                                 ltc_edma_handle_t *handle,
861                                 const uint8_t *plaintext,
862                                 uint8_t *ciphertext,
863                                 uint32_t size,
864                                 const uint8_t iv[LTC_DES_IV_SIZE],
865                                 const uint8_t key[LTC_DES_KEY_SIZE])
866 {
867     return ltc_des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key, kLTC_ModeCBC, kLTC_ModeEncrypt);
868 }
869 
870 /*!
871  * brief Decrypts DES using CBC block mode.
872  *
873  * Decrypts DES using CBC block mode.
874  *
875  * param base LTC peripheral base address
876  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
877  * param ciphertext Input ciphertext to decrypt
878  * param[out] plaintext Output plaintext
879  * param size Size of input data in bytes
880  * param iv Input initial vector to combine with the first plaintext block.
881  *           The iv does not need to be secret, but it must be unpredictable.
882  * param key Input key to use for decryption
883  * return Status from encrypt/decrypt operation
884  */
LTC_DES_DecryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])885 status_t LTC_DES_DecryptCbcEDMA(LTC_Type *base,
886                                 ltc_edma_handle_t *handle,
887                                 const uint8_t *ciphertext,
888                                 uint8_t *plaintext,
889                                 uint32_t size,
890                                 const uint8_t iv[LTC_DES_IV_SIZE],
891                                 const uint8_t key[LTC_DES_KEY_SIZE])
892 {
893     return ltc_des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key, kLTC_ModeCBC, kLTC_ModeDecrypt);
894 }
895 
896 /*!
897  * brief Encrypts DES using CFB block mode.
898  *
899  * Encrypts DES using CFB block mode.
900  *
901  * param base LTC peripheral base address
902  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
903  * param plaintext Input plaintext to encrypt
904  * param size Size of input data in bytes
905  * param iv Input initial block.
906  * param key Input key to use for encryption
907  * param[out] ciphertext Output ciphertext
908  * return Status from encrypt/decrypt operation
909  */
LTC_DES_EncryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])910 status_t LTC_DES_EncryptCfbEDMA(LTC_Type *base,
911                                 ltc_edma_handle_t *handle,
912                                 const uint8_t *plaintext,
913                                 uint8_t *ciphertext,
914                                 uint32_t size,
915                                 const uint8_t iv[LTC_DES_IV_SIZE],
916                                 const uint8_t key[LTC_DES_KEY_SIZE])
917 {
918     return ltc_des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key, kLTC_ModeCFB, kLTC_ModeEncrypt);
919 }
920 
921 /*!
922  * brief Decrypts DES using CFB block mode.
923  *
924  * Decrypts DES using CFB block mode.
925  *
926  * param base LTC peripheral base address
927  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
928  * param ciphertext Input ciphertext to decrypt
929  * param[out] plaintext Output plaintext
930  * param size Size of input and output data in bytes
931  * param iv Input initial block.
932  * param key Input key to use for decryption
933  * return Status from encrypt/decrypt operation
934  */
LTC_DES_DecryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])935 status_t LTC_DES_DecryptCfbEDMA(LTC_Type *base,
936                                 ltc_edma_handle_t *handle,
937                                 const uint8_t *ciphertext,
938                                 uint8_t *plaintext,
939                                 uint32_t size,
940                                 const uint8_t iv[LTC_DES_IV_SIZE],
941                                 const uint8_t key[LTC_DES_KEY_SIZE])
942 {
943     return ltc_des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key, kLTC_ModeCFB, kLTC_ModeDecrypt);
944 }
945 
946 /*!
947  * brief Encrypts DES using OFB block mode.
948  *
949  * Encrypts DES using OFB block mode.
950  *
951  * param base LTC peripheral base address
952  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
953  * param plaintext Input plaintext to encrypt
954  * param[out] ciphertext Output ciphertext
955  * param size Size of input and output data in bytes
956  * param iv Input unique input vector. The OFB mode requires that the IV be unique
957  *           for each execution of the mode under the given key.
958  * param key Input key to use for encryption
959  * return Status from encrypt/decrypt operation
960  */
LTC_DES_EncryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])961 status_t LTC_DES_EncryptOfbEDMA(LTC_Type *base,
962                                 ltc_edma_handle_t *handle,
963                                 const uint8_t *plaintext,
964                                 uint8_t *ciphertext,
965                                 uint32_t size,
966                                 const uint8_t iv[LTC_DES_IV_SIZE],
967                                 const uint8_t key[LTC_DES_KEY_SIZE])
968 {
969     return ltc_des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key, kLTC_ModeOFB, kLTC_ModeEncrypt);
970 }
971 
972 /*!
973  * brief Decrypts DES using OFB block mode.
974  *
975  * Decrypts DES using OFB block mode.
976  *
977  * param base LTC peripheral base address
978  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
979  * param ciphertext Input ciphertext to decrypt
980  * param[out] plaintext Output plaintext
981  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
982  * param iv Input unique input vector. The OFB mode requires that the IV be unique
983  *           for each execution of the mode under the given key.
984  * param key Input key to use for decryption
985  * return Status from encrypt/decrypt operation
986  */
LTC_DES_DecryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key[LTC_DES_KEY_SIZE])987 status_t LTC_DES_DecryptOfbEDMA(LTC_Type *base,
988                                 ltc_edma_handle_t *handle,
989                                 const uint8_t *ciphertext,
990                                 uint8_t *plaintext,
991                                 uint32_t size,
992                                 const uint8_t iv[LTC_DES_IV_SIZE],
993                                 const uint8_t key[LTC_DES_KEY_SIZE])
994 {
995     return ltc_des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key, kLTC_ModeOFB, kLTC_ModeDecrypt);
996 }
997 
998 /*!
999  * brief Encrypts triple DES using ECB block mode with two keys.
1000  *
1001  * Encrypts triple DES using ECB block mode with two keys.
1002  *
1003  * param base LTC peripheral base address
1004  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1005  * param plaintext Input plaintext to encrypt
1006  * param[out] ciphertext Output ciphertext
1007  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
1008  * param key1 First input key for key bundle
1009  * param key2 Second input key for key bundle
1010  * return Status from encrypt/decrypt operation
1011  */
LTC_DES2_EncryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1012 status_t LTC_DES2_EncryptEcbEDMA(LTC_Type *base,
1013                                  ltc_edma_handle_t *handle,
1014                                  const uint8_t *plaintext,
1015                                  uint8_t *ciphertext,
1016                                  uint32_t size,
1017                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1018                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1019 {
1020     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, NULL, key1, key2, NULL, kLTC_ModeECB,
1021                                  kLTC_ModeEncrypt);
1022 }
1023 
1024 /*!
1025  * brief Encrypts triple DES using ECB block mode with three keys.
1026  *
1027  * Encrypts triple DES using ECB block mode with three keys.
1028  *
1029  * param base LTC peripheral base address
1030  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1031  * param plaintext Input plaintext to encrypt
1032  * param[out] ciphertext Output ciphertext
1033  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
1034  * param key1 First input key for key bundle
1035  * param key2 Second input key for key bundle
1036  * param key3 Third input key for key bundle
1037  * return Status from encrypt/decrypt operation
1038  */
LTC_DES3_EncryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1039 status_t LTC_DES3_EncryptEcbEDMA(LTC_Type *base,
1040                                  ltc_edma_handle_t *handle,
1041                                  const uint8_t *plaintext,
1042                                  uint8_t *ciphertext,
1043                                  uint32_t size,
1044                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1045                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1046                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1047 {
1048     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, NULL, key1, key2, key3, kLTC_ModeECB,
1049                                  kLTC_ModeEncrypt);
1050 }
1051 
1052 /*!
1053  * brief Decrypts triple DES using ECB block mode with two keys.
1054  *
1055  * Decrypts triple DES using ECB block mode with two keys.
1056  *
1057  * param base LTC peripheral base address
1058  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1059  * param ciphertext Input ciphertext to decrypt
1060  * param[out] plaintext Output plaintext
1061  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
1062  * param key1 First input key for key bundle
1063  * param key2 Second input key for key bundle
1064  * return Status from encrypt/decrypt operation
1065  */
LTC_DES2_DecryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1066 status_t LTC_DES2_DecryptEcbEDMA(LTC_Type *base,
1067                                  ltc_edma_handle_t *handle,
1068                                  const uint8_t *ciphertext,
1069                                  uint8_t *plaintext,
1070                                  uint32_t size,
1071                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1072                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1073 {
1074     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, NULL, key1, key2, NULL, kLTC_ModeECB,
1075                                  kLTC_ModeDecrypt);
1076 }
1077 
1078 /*!
1079  * brief Decrypts triple DES using ECB block mode with three keys.
1080  *
1081  * Decrypts triple DES using ECB block mode with three keys.
1082  *
1083  * param base LTC peripheral base address
1084  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1085  * param ciphertext Input ciphertext to decrypt
1086  * param[out] plaintext Output plaintext
1087  * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
1088  * param key1 First input key for key bundle
1089  * param key2 Second input key for key bundle
1090  * param key3 Third input key for key bundle
1091  * return Status from encrypt/decrypt operation
1092  */
LTC_DES3_DecryptEcbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1093 status_t LTC_DES3_DecryptEcbEDMA(LTC_Type *base,
1094                                  ltc_edma_handle_t *handle,
1095                                  const uint8_t *ciphertext,
1096                                  uint8_t *plaintext,
1097                                  uint32_t size,
1098                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1099                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1100                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1101 {
1102     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, NULL, key1, key2, key3, kLTC_ModeECB,
1103                                  kLTC_ModeDecrypt);
1104 }
1105 
1106 /*!
1107  * brief Encrypts triple DES using CBC block mode with two keys.
1108  *
1109  * Encrypts triple DES using CBC block mode with two keys.
1110  *
1111  * param base LTC peripheral base address
1112  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1113  * param plaintext Input plaintext to encrypt
1114  * param[out] ciphertext Output ciphertext
1115  * param size Size of input and output data in bytes
1116  * param iv Input initial vector to combine with the first plaintext block.
1117  *           The iv does not need to be secret, but it must be unpredictable.
1118  * param key1 First input key for key bundle
1119  * param key2 Second input key for key bundle
1120  * return Status from encrypt/decrypt operation
1121  */
LTC_DES2_EncryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1122 status_t LTC_DES2_EncryptCbcEDMA(LTC_Type *base,
1123                                  ltc_edma_handle_t *handle,
1124                                  const uint8_t *plaintext,
1125                                  uint8_t *ciphertext,
1126                                  uint32_t size,
1127                                  const uint8_t iv[LTC_DES_IV_SIZE],
1128                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1129                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1130 {
1131     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeCBC,
1132                                  kLTC_ModeEncrypt);
1133 }
1134 
1135 /*!
1136  * brief Encrypts triple DES using CBC block mode with three keys.
1137  *
1138  * Encrypts triple DES using CBC block mode with three keys.
1139  *
1140  * param base LTC peripheral base address
1141  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1142  * param plaintext Input plaintext to encrypt
1143  * param[out] ciphertext Output ciphertext
1144  * param size Size of input data in bytes
1145  * param iv Input initial vector to combine with the first plaintext block.
1146  *           The iv does not need to be secret, but it must be unpredictable.
1147  * param key1 First input key for key bundle
1148  * param key2 Second input key for key bundle
1149  * param key3 Third input key for key bundle
1150  * return Status from encrypt/decrypt operation
1151  */
LTC_DES3_EncryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1152 status_t LTC_DES3_EncryptCbcEDMA(LTC_Type *base,
1153                                  ltc_edma_handle_t *handle,
1154                                  const uint8_t *plaintext,
1155                                  uint8_t *ciphertext,
1156                                  uint32_t size,
1157                                  const uint8_t iv[LTC_DES_IV_SIZE],
1158                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1159                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1160                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1161 {
1162     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeCBC,
1163                                  kLTC_ModeEncrypt);
1164 }
1165 
1166 /*!
1167  * brief Decrypts triple DES using CBC block mode with two keys.
1168  *
1169  * Decrypts triple DES using CBC block mode with two keys.
1170  *
1171  * param base LTC peripheral base address
1172  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1173  * param ciphertext Input ciphertext to decrypt
1174  * param[out] plaintext Output plaintext
1175  * param size Size of input and output data in bytes
1176  * param iv Input initial vector to combine with the first plaintext block.
1177  *           The iv does not need to be secret, but it must be unpredictable.
1178  * param key1 First input key for key bundle
1179  * param key2 Second input key for key bundle
1180  * return Status from encrypt/decrypt operation
1181  */
LTC_DES2_DecryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1182 status_t LTC_DES2_DecryptCbcEDMA(LTC_Type *base,
1183                                  ltc_edma_handle_t *handle,
1184                                  const uint8_t *ciphertext,
1185                                  uint8_t *plaintext,
1186                                  uint32_t size,
1187                                  const uint8_t iv[LTC_DES_IV_SIZE],
1188                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1189                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1190 {
1191     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeCBC,
1192                                  kLTC_ModeDecrypt);
1193 }
1194 
1195 /*!
1196  * brief Decrypts triple DES using CBC block mode with three keys.
1197  *
1198  * Decrypts triple DES using CBC block mode with three keys.
1199  *
1200  * param base LTC peripheral base address
1201  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1202  * param ciphertext Input ciphertext to decrypt
1203  * param[out] plaintext Output plaintext
1204  * param size Size of input and output data in bytes
1205  * param iv Input initial vector to combine with the first plaintext block.
1206  *           The iv does not need to be secret, but it must be unpredictable.
1207  * param key1 First input key for key bundle
1208  * param key2 Second input key for key bundle
1209  * param key3 Third input key for key bundle
1210  * return Status from encrypt/decrypt operation
1211  */
LTC_DES3_DecryptCbcEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1212 status_t LTC_DES3_DecryptCbcEDMA(LTC_Type *base,
1213                                  ltc_edma_handle_t *handle,
1214                                  const uint8_t *ciphertext,
1215                                  uint8_t *plaintext,
1216                                  uint32_t size,
1217                                  const uint8_t iv[LTC_DES_IV_SIZE],
1218                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1219                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1220                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1221 {
1222     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeCBC,
1223                                  kLTC_ModeDecrypt);
1224 }
1225 
1226 /*!
1227  * brief Encrypts triple DES using CFB block mode with two keys.
1228  *
1229  * Encrypts triple DES using CFB block mode with two keys.
1230  *
1231  * param base LTC peripheral base address
1232  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1233  * param plaintext Input plaintext to encrypt
1234  * param[out] ciphertext Output ciphertext
1235  * param size Size of input and output data in bytes
1236  * param iv Input initial block.
1237  * param key1 First input key for key bundle
1238  * param key2 Second input key for key bundle
1239  * return Status from encrypt/decrypt operation
1240  */
LTC_DES2_EncryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1241 status_t LTC_DES2_EncryptCfbEDMA(LTC_Type *base,
1242                                  ltc_edma_handle_t *handle,
1243                                  const uint8_t *plaintext,
1244                                  uint8_t *ciphertext,
1245                                  uint32_t size,
1246                                  const uint8_t iv[LTC_DES_IV_SIZE],
1247                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1248                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1249 {
1250     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeCFB,
1251                                  kLTC_ModeEncrypt);
1252 }
1253 
1254 /*!
1255  * brief Encrypts triple DES using CFB block mode with three keys.
1256  *
1257  * Encrypts triple DES using CFB block mode with three keys.
1258  *
1259  * param base LTC peripheral base address
1260  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1261  * param plaintext Input plaintext to encrypt
1262  * param[out] ciphertext Output ciphertext
1263  * param size Size of input and ouput data in bytes
1264  * param iv Input initial block.
1265  * param key1 First input key for key bundle
1266  * param key2 Second input key for key bundle
1267  * param key3 Third input key for key bundle
1268  * return Status from encrypt/decrypt operation
1269  */
LTC_DES3_EncryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1270 status_t LTC_DES3_EncryptCfbEDMA(LTC_Type *base,
1271                                  ltc_edma_handle_t *handle,
1272                                  const uint8_t *plaintext,
1273                                  uint8_t *ciphertext,
1274                                  uint32_t size,
1275                                  const uint8_t iv[LTC_DES_IV_SIZE],
1276                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1277                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1278                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1279 {
1280     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeCFB,
1281                                  kLTC_ModeEncrypt);
1282 }
1283 
1284 /*!
1285  * brief Decrypts triple DES using CFB block mode with two keys.
1286  *
1287  * Decrypts triple DES using CFB block mode with two keys.
1288  *
1289  * param base LTC peripheral base address
1290  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1291  * param ciphertext Input ciphertext to decrypt
1292  * param[out] plaintext Output plaintext
1293  * param size Size of input and output data in bytes
1294  * param iv Input initial block.
1295  * param key1 First input key for key bundle
1296  * param key2 Second input key for key bundle
1297  * return Status from encrypt/decrypt operation
1298  */
LTC_DES2_DecryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1299 status_t LTC_DES2_DecryptCfbEDMA(LTC_Type *base,
1300                                  ltc_edma_handle_t *handle,
1301                                  const uint8_t *ciphertext,
1302                                  uint8_t *plaintext,
1303                                  uint32_t size,
1304                                  const uint8_t iv[LTC_DES_IV_SIZE],
1305                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1306                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1307 {
1308     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeCFB,
1309                                  kLTC_ModeDecrypt);
1310 }
1311 
1312 /*!
1313  * brief Decrypts triple DES using CFB block mode with three keys.
1314  *
1315  * Decrypts triple DES using CFB block mode with three keys.
1316  *
1317  * param base LTC peripheral base address
1318  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1319  * param ciphertext Input ciphertext to decrypt
1320  * param[out] plaintext Output plaintext
1321  * param size Size of input data in bytes
1322  * param iv Input initial block.
1323  * param key1 First input key for key bundle
1324  * param key2 Second input key for key bundle
1325  * param key3 Third input key for key bundle
1326  * return Status from encrypt/decrypt operation
1327  */
LTC_DES3_DecryptCfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1328 status_t LTC_DES3_DecryptCfbEDMA(LTC_Type *base,
1329                                  ltc_edma_handle_t *handle,
1330                                  const uint8_t *ciphertext,
1331                                  uint8_t *plaintext,
1332                                  uint32_t size,
1333                                  const uint8_t iv[LTC_DES_IV_SIZE],
1334                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1335                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1336                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1337 {
1338     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeCFB,
1339                                  kLTC_ModeDecrypt);
1340 }
1341 
1342 /*!
1343  * brief Encrypts triple DES using OFB block mode with two keys.
1344  *
1345  * Encrypts triple DES using OFB block mode with two keys.
1346  *
1347  * param base LTC peripheral base address
1348  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1349  * param plaintext Input plaintext to encrypt
1350  * param[out] ciphertext Output ciphertext
1351  * param size Size of input and output data in bytes
1352  * param iv Input unique input vector. The OFB mode requires that the IV be unique
1353  *           for each execution of the mode under the given key.
1354  * param key1 First input key for key bundle
1355  * param key2 Second input key for key bundle
1356  * return Status from encrypt/decrypt operation
1357  */
LTC_DES2_EncryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1358 status_t LTC_DES2_EncryptOfbEDMA(LTC_Type *base,
1359                                  ltc_edma_handle_t *handle,
1360                                  const uint8_t *plaintext,
1361                                  uint8_t *ciphertext,
1362                                  uint32_t size,
1363                                  const uint8_t iv[LTC_DES_IV_SIZE],
1364                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1365                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1366 {
1367     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeOFB,
1368                                  kLTC_ModeEncrypt);
1369 }
1370 
1371 /*!
1372  * brief Encrypts triple DES using OFB block mode with three keys.
1373  *
1374  * Encrypts triple DES using OFB block mode with three keys.
1375  *
1376  * param base LTC peripheral base address
1377  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1378  * param plaintext Input plaintext to encrypt
1379  * param[out] ciphertext Output ciphertext
1380  * param size Size of input and output data in bytes
1381  * param iv Input unique input vector. The OFB mode requires that the IV be unique
1382  *           for each execution of the mode under the given key.
1383  * param key1 First input key for key bundle
1384  * param key2 Second input key for key bundle
1385  * param key3 Third input key for key bundle
1386  * return Status from encrypt/decrypt operation
1387  */
LTC_DES3_EncryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * plaintext,uint8_t * ciphertext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1388 status_t LTC_DES3_EncryptOfbEDMA(LTC_Type *base,
1389                                  ltc_edma_handle_t *handle,
1390                                  const uint8_t *plaintext,
1391                                  uint8_t *ciphertext,
1392                                  uint32_t size,
1393                                  const uint8_t iv[LTC_DES_IV_SIZE],
1394                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1395                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1396                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1397 {
1398     return ltc_3des_process_EDMA(base, handle, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeOFB,
1399                                  kLTC_ModeEncrypt);
1400 }
1401 
1402 /*!
1403  * brief Decrypts triple DES using OFB block mode with two keys.
1404  *
1405  * Decrypts triple DES using OFB block mode with two keys.
1406  *
1407  * param base LTC peripheral base address
1408  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1409  * param ciphertext Input ciphertext to decrypt
1410  * param[out] plaintext Output plaintext
1411  * param size Size of input and output data in bytes
1412  * param iv Input unique input vector. The OFB mode requires that the IV be unique
1413  *           for each execution of the mode under the given key.
1414  * param key1 First input key for key bundle
1415  * param key2 Second input key for key bundle
1416  * return Status from encrypt/decrypt operation
1417  */
LTC_DES2_DecryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE])1418 status_t LTC_DES2_DecryptOfbEDMA(LTC_Type *base,
1419                                  ltc_edma_handle_t *handle,
1420                                  const uint8_t *ciphertext,
1421                                  uint8_t *plaintext,
1422                                  uint32_t size,
1423                                  const uint8_t iv[LTC_DES_IV_SIZE],
1424                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1425                                  const uint8_t key2[LTC_DES_KEY_SIZE])
1426 {
1427     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeOFB,
1428                                  kLTC_ModeDecrypt);
1429 }
1430 
1431 /*!
1432  * brief Decrypts triple DES using OFB block mode with three keys.
1433  *
1434  * Decrypts triple DES using OFB block mode with three keys.
1435  *
1436  * param base LTC peripheral base address
1437  * param handle pointer to ltc_edma_handle_t structure which stores the transaction state.
1438  * param ciphertext Input ciphertext to decrypt
1439  * param[out] plaintext Output plaintext
1440  * param size Size of input and output data in bytes
1441  * param iv Input unique input vector. The OFB mode requires that the IV be unique
1442  *           for each execution of the mode under the given key.
1443  * param key1 First input key for key bundle
1444  * param key2 Second input key for key bundle
1445  * param key3 Third input key for key bundle
1446  * return Status from encrypt/decrypt operation
1447  */
LTC_DES3_DecryptOfbEDMA(LTC_Type * base,ltc_edma_handle_t * handle,const uint8_t * ciphertext,uint8_t * plaintext,uint32_t size,const uint8_t iv[LTC_DES_IV_SIZE],const uint8_t key1[LTC_DES_KEY_SIZE],const uint8_t key2[LTC_DES_KEY_SIZE],const uint8_t key3[LTC_DES_KEY_SIZE])1448 status_t LTC_DES3_DecryptOfbEDMA(LTC_Type *base,
1449                                  ltc_edma_handle_t *handle,
1450                                  const uint8_t *ciphertext,
1451                                  uint8_t *plaintext,
1452                                  uint32_t size,
1453                                  const uint8_t iv[LTC_DES_IV_SIZE],
1454                                  const uint8_t key1[LTC_DES_KEY_SIZE],
1455                                  const uint8_t key2[LTC_DES_KEY_SIZE],
1456                                  const uint8_t key3[LTC_DES_KEY_SIZE])
1457 {
1458     return ltc_3des_process_EDMA(base, handle, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeOFB,
1459                                  kLTC_ModeDecrypt);
1460 }
1461 #endif /* FSL_FEATURE_LTC_HAS_DES */
1462 
1463 /*********************** LTC EDMA tools ***************************************/
1464 
LTC_GetInstance(LTC_Type * base)1465 static uint32_t LTC_GetInstance(LTC_Type *base)
1466 {
1467     uint32_t instance = 0;
1468     uint32_t i;
1469 
1470     for (i = 0; i < (uint32_t)FSL_FEATURE_SOC_LTC_COUNT; i++)
1471     {
1472         if (s_ltcBase[instance] == base)
1473         {
1474             instance = i;
1475             break;
1476         }
1477     }
1478     return instance;
1479 }
1480 
1481 /*!
1482  * @brief Enable or disable LTC Input FIFO DMA request.
1483  *
1484  * This function enables or disables DMA request and done signals for Input FIFO.
1485  *
1486  * @param base LTC peripheral base address.
1487  * @param enable True to enable, false to disable.
1488  */
LTC_EnableInputFifoDMA(LTC_Type * base,bool enable)1489 static inline void LTC_EnableInputFifoDMA(LTC_Type *base, bool enable)
1490 {
1491     if (enable)
1492     {
1493         base->CTL |= LTC_CTL_IFE_MASK;
1494     }
1495     else
1496     {
1497         base->CTL &= ~LTC_CTL_IFE_MASK;
1498     }
1499 }
1500 
1501 /*!
1502  * @brief Enable or disable LTC Output FIFO DMA request.
1503  *
1504  * This function enables or disables DMA request and done signals for Output FIFO.
1505  *
1506  * @param base LTC peripheral base address.
1507  * @param enable True to enable, false to disable.
1508  */
LTC_EnableOutputFifoDMA(LTC_Type * base,bool enable)1509 static inline void LTC_EnableOutputFifoDMA(LTC_Type *base, bool enable)
1510 {
1511     if (enable)
1512     {
1513         base->CTL |= LTC_CTL_OFE_MASK;
1514     }
1515     else
1516     {
1517         base->CTL &= ~LTC_CTL_OFE_MASK;
1518     }
1519 }
1520 
LTC_InputFifoEDMACallback(edma_handle_t * handle,void * param,bool transferDone,uint32_t tcds)1521 static void LTC_InputFifoEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
1522 {
1523     ltc_edma_private_handle_t *ltcPrivateHandle = (ltc_edma_private_handle_t *)param;
1524 
1525     /* Avoid the warning for unused variables. */
1526     handle = handle;
1527     tcds   = tcds;
1528 
1529     if (transferDone)
1530     {
1531         /* Stop DMA channel. */
1532         EDMA_StopTransfer(ltcPrivateHandle->handle->inputFifoEdmaHandle);
1533 
1534         /* Disable Input Fifo DMA */
1535         LTC_EnableInputFifoDMA(ltcPrivateHandle->base, false);
1536     }
1537 }
1538 
LTC_OutputFifoEDMACallback(edma_handle_t * handle,void * param,bool transferDone,uint32_t tcds)1539 static void LTC_OutputFifoEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
1540 {
1541     ltc_edma_private_handle_t *ltcPrivateHandle = (ltc_edma_private_handle_t *)param;
1542 
1543     /* Avoid the warning for unused variables. */
1544     handle = handle;
1545     tcds   = tcds;
1546 
1547     if (transferDone)
1548     {
1549         /* Stop DMA channel. */
1550         EDMA_StopTransfer(ltcPrivateHandle->handle->outputFifoEdmaHandle);
1551 
1552         /* Disable Output Fifo DMA */
1553         LTC_EnableOutputFifoDMA(ltcPrivateHandle->base, false);
1554 
1555         if (NULL != ltcPrivateHandle->handle->state_machine)
1556         {
1557             (void)ltcPrivateHandle->handle->state_machine(ltcPrivateHandle->base, ltcPrivateHandle->handle);
1558         }
1559     }
1560 }
1561 
1562 /* @brief Copy data to Input FIFO and reading from Ouput FIFO using eDMA. */
ltc_symmetric_process_EDMA(LTC_Type * base,uint32_t inSize,const uint8_t ** inData,uint8_t ** outData)1563 static void ltc_symmetric_process_EDMA(LTC_Type *base, uint32_t inSize, const uint8_t **inData, uint8_t **outData)
1564 {
1565     const uint8_t *in          = *inData;
1566     uint8_t *out               = *outData;
1567     uint32_t instance          = LTC_GetInstance(base);
1568     uint32_t entry_number      = inSize / sizeof(uint32_t);
1569     const uint8_t *inputBuffer = *inData;
1570     uint8_t *outputBuffer      = *outData;
1571     edma_transfer_config_t config;
1572 
1573     if (0U != entry_number)
1574     {
1575         /* =========== Init Input FIFO DMA ======================*/
1576         (void)memset(&config, 0, sizeof(config));
1577 
1578         /* Prepare transfer. */
1579         EDMA_PrepareTransfer(&config, (void *)(uint32_t *)(uintptr_t)inputBuffer, 1U,
1580                              (void *)(uint32_t *)(uintptr_t)(&base->IFIFO), 4U, 4U, entry_number * 4U,
1581                              kEDMA_MemoryToPeripheral);
1582         /* Submit transfer. */
1583         (void)EDMA_SubmitTransfer(s_edmaPrivateHandle[instance].handle->inputFifoEdmaHandle,
1584                                   (const edma_transfer_config_t *)(uint32_t)&config);
1585 
1586         /* Set request size.*/
1587         base->CTL &= ~LTC_CTL_IFR_MASK; /* 1 entry */
1588         /* Enable Input Fifo DMA */
1589         LTC_EnableInputFifoDMA(base, true);
1590 
1591         /* Start the DMA channel */
1592         EDMA_StartTransfer(s_edmaPrivateHandle[instance].handle->inputFifoEdmaHandle);
1593 
1594         /* =========== Init Output FIFO DMA ======================*/
1595         (void)memset(&config, 0, sizeof(config));
1596 
1597         /* Prepare transfer. */
1598         EDMA_PrepareTransfer(&config, (void *)(uint32_t *)(uintptr_t)(&base->OFIFO), 4U, (void *)outputBuffer, 1U, 4U,
1599                              entry_number * 4U, kEDMA_PeripheralToMemory);
1600         /* Submit transfer. */
1601         (void)EDMA_SubmitTransfer(s_edmaPrivateHandle[instance].handle->outputFifoEdmaHandle,
1602                                   (const edma_transfer_config_t *)(uint32_t)&config);
1603 
1604         /* Set request size.*/
1605         base->CTL &= ~LTC_CTL_OFR_MASK; /* 1 entry */
1606 
1607         /* Enable Output Fifo DMA */
1608         LTC_EnableOutputFifoDMA(base, true);
1609 
1610         /* Start the DMA channel */
1611         EDMA_StartTransfer(s_edmaPrivateHandle[instance].handle->outputFifoEdmaHandle);
1612 
1613         { /* Dummy read of LTC register. Do not delete.*/
1614             volatile uint32_t status_reg;
1615 
1616             status_reg = (base)->STA;
1617 
1618             (void)status_reg;
1619         }
1620 
1621         out = &out[entry_number * sizeof(uint32_t)];
1622         in  = &in[entry_number * sizeof(uint32_t)];
1623 
1624         *inData  = in;
1625         *outData = out;
1626     }
1627 }
1628 
1629 /*!
1630  * brief Init the LTC eDMA handle which is used in transactional functions
1631  * param base      LTC module base address
1632  * param handle    Pointer to ltc_edma_handle_t structure
1633  * param callback  Callback function, NULL means no callback.
1634  * param userData  Callback function parameter.
1635  * param inputFifoEdmaHandle User requested eDMA handle for Input FIFO eDMA.
1636  * param outputFifoEdmaHandle User requested eDMA handle for Output FIFO eDMA.
1637  */
LTC_CreateHandleEDMA(LTC_Type * base,ltc_edma_handle_t * handle,ltc_edma_callback_t callback,void * userData,edma_handle_t * inputFifoEdmaHandle,edma_handle_t * outputFifoEdmaHandle)1638 void LTC_CreateHandleEDMA(LTC_Type *base,
1639                           ltc_edma_handle_t *handle,
1640                           ltc_edma_callback_t callback,
1641                           void *userData,
1642                           edma_handle_t *inputFifoEdmaHandle,
1643                           edma_handle_t *outputFifoEdmaHandle)
1644 {
1645     assert(NULL != handle);
1646     assert(NULL != inputFifoEdmaHandle);
1647     assert(NULL != outputFifoEdmaHandle);
1648 
1649     uint32_t instance = LTC_GetInstance(base);
1650 
1651     s_edmaPrivateHandle[instance].base   = base;
1652     s_edmaPrivateHandle[instance].handle = handle;
1653 
1654     (void)memset(handle, 0, sizeof(*handle));
1655 
1656     handle->inputFifoEdmaHandle  = inputFifoEdmaHandle;
1657     handle->outputFifoEdmaHandle = outputFifoEdmaHandle;
1658 
1659     handle->callback = callback;
1660     handle->userData = userData;
1661 
1662     /* Register DMA callback functions */
1663     EDMA_SetCallback(handle->inputFifoEdmaHandle, LTC_InputFifoEDMACallback, &s_edmaPrivateHandle[instance]);
1664     EDMA_SetCallback(handle->outputFifoEdmaHandle, LTC_OutputFifoEDMACallback, &s_edmaPrivateHandle[instance]);
1665 
1666     /* Set request size. DMA request size is 1 entry.*/
1667     base->CTL &= ~LTC_CTL_IFR_MASK;
1668     base->CTL &= ~LTC_CTL_OFR_MASK;
1669 }
1670