1 /***************************************************************************//**
2 * \file cy_crypto_core_aes.h
3 * \version 2.90
4 *
5 * \brief
6 *  This file provides constant and parameters for the API for the AES method
7 *  in the Crypto driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 *    http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27 
28 
29 #if !defined (CY_CRYPTO_CORE_AES_H)
30 #define CY_CRYPTO_CORE_AES_H
31 
32 #include "cy_device.h"
33 
34 #if defined (CY_IP_MXCRYPTO)
35 
36 #include "cy_crypto_common.h"
37 
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41 
42 #if (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_AES_C)
43 
44 #include "cy_crypto_core_aes_v1.h"
45 #include "cy_crypto_core_aes_v2.h"
46 
47 #include "cy_crypto_core_hw.h"
48 
49 typedef cy_en_crypto_status_t (*cy_crypto_aes_init_func_t)(CRYPTO_Type *base,
50                                                  uint8_t const *key,
51                                                  cy_en_crypto_aes_key_length_t keyLength,
52                                                  cy_stc_crypto_aes_state_t *aesState,
53                                                  cy_stc_crypto_aes_buffers_t *aesBuffers);
54 
55 typedef cy_en_crypto_status_t (*cy_crypto_aes_ecb_func_t)(CRYPTO_Type *base,
56                                                 cy_en_crypto_dir_mode_t dirMode,
57                                                 uint8_t *dst,
58                                                 uint8_t const *src,
59                                                 cy_stc_crypto_aes_state_t *aesState);
60 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
61 typedef cy_en_crypto_status_t (*cy_crypto_aes_cbc_func_t)(CRYPTO_Type *base,
62                                                 cy_en_crypto_dir_mode_t dirMode,
63                                                 uint32_t srcSize,
64                                                 uint8_t *ivPtr,
65                                                 uint8_t *dst,
66                                                 uint8_t const *src,
67                                                 cy_stc_crypto_aes_state_t *aesState);
68 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
69 
70 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
71 typedef cy_en_crypto_status_t (*cy_crypto_aes_cfb_func_t)(CRYPTO_Type *base,
72                                                 cy_en_crypto_dir_mode_t dirMode,
73                                                 uint32_t srcSize,
74                                                 uint8_t *ivPtr,
75                                                 uint8_t *dst,
76                                                 uint8_t const *src,
77                                                 cy_stc_crypto_aes_state_t *aesState);
78 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
79 
80 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
81 typedef cy_en_crypto_status_t (*cy_crypto_aes_ctr_func_t)(CRYPTO_Type *base,
82                                                 uint32_t srcSize,
83                                                 uint32_t *srcOffset,
84                                                 uint8_t *ivPtr,
85                                                 uint8_t *streamBlock,
86                                                 uint8_t *dst,
87                                                 uint8_t const *src,
88                                                 cy_stc_crypto_aes_state_t *aesState);
89 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
90 
91 /**
92 * \addtogroup group_crypto_lld_symmetric_functions
93 * \{
94 */
95 
96 /*******************************************************************************
97 * Function Name: Cy_Crypto_Core_Aes_Init
98 ****************************************************************************//**
99 *
100 * Initializes AES mode of operation and prepares an inverse key.
101 *
102 * \param base
103 * The pointer to the CRYPTO instance.
104 *
105 * \param key
106 * The pointer to the encryption/decryption key.
107 *
108 * \param keyLength
109 * \ref cy_en_crypto_aes_key_length_t
110 *
111 * \param aesState
112 * The pointer to the AES state structure allocated by the user. The user
113 * must not modify anything in this structure.
114 *
115 * \return
116 * \ref cy_en_crypto_status_t
117 *
118 * \funcusage
119 * \snippet crypto/snippet/main.c snippet_myCryptoCoreAesEcbUse
120 *
121 *******************************************************************************/
Cy_Crypto_Core_Aes_Init(CRYPTO_Type * base,uint8_t const * key,cy_en_crypto_aes_key_length_t keyLength,cy_stc_crypto_aes_state_t * aesState)122 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Init(CRYPTO_Type *base,
123                                                  uint8_t const *key,
124                                                  cy_en_crypto_aes_key_length_t keyLength,
125                                                  cy_stc_crypto_aes_state_t *aesState)
126 {
127     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
128 
129     #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE) || defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
130     cy_stc_crypto_aes_buffers_t *aesBuffers = (cy_stc_crypto_aes_buffers_t *)((void *)Cy_Crypto_Core_GetVuMemoryAddress(base));
131 
132     if (CY_CRYPTO_V1)
133     {
134         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
135         tmpResult = Cy_Crypto_Core_V1_Aes_Init(base, key, keyLength, aesState, aesBuffers);
136         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
137     }
138     else
139     {
140         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
141         tmpResult = Cy_Crypto_Core_V2_Aes_Init(base, key, keyLength, aesState, aesBuffers);
142         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
143     }
144     #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) || defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
145 
146     return tmpResult;
147 }
148 
149 /*******************************************************************************
150 * Function Name: Cy_Crypto_Core_Aes_InitContext
151 ****************************************************************************//**
152 *
153 * Initializes AES mode of operation and prepares an inverse key within a user
154 * specified buffer.
155 *
156 * \param base
157 * The pointer to the CRYPTO instance.
158 *
159 * \param key
160 * The pointer to the encryption/decryption key.
161 *
162 * \param keyLength
163 * \ref cy_en_crypto_aes_key_length_t
164 *
165 * \param aesState
166 * The pointer to the AES state structure allocated by the user. The user
167 * must not modify anything in this structure.
168 *
169 * \param aesBuffers
170 * The pointer to the AES buffer provided by the user. The user must not modify
171 * anything in this buffer during operation.
172 *
173 * \return
174 * \ref cy_en_crypto_status_t
175 *
176 *******************************************************************************/
Cy_Crypto_Core_Aes_InitContext(CRYPTO_Type * base,uint8_t const * key,cy_en_crypto_aes_key_length_t keyLength,cy_stc_crypto_aes_state_t * aesState,cy_stc_crypto_aes_buffers_t * aesBuffers)177 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_InitContext(CRYPTO_Type *base,
178                                                  uint8_t const *key,
179                                                  cy_en_crypto_aes_key_length_t keyLength,
180                                                  cy_stc_crypto_aes_state_t *aesState,
181                                                  cy_stc_crypto_aes_buffers_t *aesBuffers)
182 {
183     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
184 
185     if (CY_CRYPTO_V1)
186     {
187         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
188         tmpResult = Cy_Crypto_Core_V1_Aes_Init(base, key, keyLength, aesState, aesBuffers);
189         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
190     }
191     else
192     {
193         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
194         tmpResult = Cy_Crypto_Core_V2_Aes_Init(base, key, keyLength, aesState, aesBuffers);
195         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
196     }
197 
198     return tmpResult;
199 }
200 
201 /*******************************************************************************
202 * Function Name: Cy_Crypto_Core_Aes_Free
203 ****************************************************************************//**
204 *
205 * Clears AES operation context.
206 *
207 * \param base
208 * The pointer to the CRYPTO instance.
209 *
210 * \param aesState
211 * The pointer to the AES state structure allocated by the user. The user
212 * must not modify anything in this structure.
213 *
214 * \return
215 * \ref cy_en_crypto_status_t
216 *
217 * \funcusage
218 * \snippet crypto/snippet/main.c snippet_myCryptoCoreAesEcbUse
219 *
220 *******************************************************************************/
Cy_Crypto_Core_Aes_Free(CRYPTO_Type * base,cy_stc_crypto_aes_state_t * aesState)221 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Free(CRYPTO_Type *base,
222                                                  cy_stc_crypto_aes_state_t *aesState)
223 {
224     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
225 
226     if (CY_CRYPTO_V1)
227     {
228         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
229         tmpResult = Cy_Crypto_Core_V1_Aes_Free(base, aesState);
230         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
231     }
232     else
233     {
234         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
235         tmpResult = Cy_Crypto_Core_V2_Aes_Free(base, aesState);
236         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
237     }
238 
239     return tmpResult;
240 }
241 
242 /*******************************************************************************
243 * Function Name: Cy_Crypto_Core_Aes_Ecb
244 ****************************************************************************//**
245 *
246 * Performs the AES operation on a single block.
247 *
248 * \param base
249 * The pointer to the CRYPTO instance.
250 *
251 * \param dirMode
252 * Can be \ref CY_CRYPTO_ENCRYPT or \ref CY_CRYPTO_DECRYPT
253 * (\ref cy_en_crypto_dir_mode_t).
254 *
255 * \param dst
256 * The pointer to the destination cipher block.
257 *
258 * \param src
259 * The pointer to the source block.
260 *
261 * \param aesState
262 * The pointer to the AES state structure allocated by the user. The user
263 * must not modify anything in this structure.
264 *
265 * \return
266 * \ref cy_en_crypto_status_t
267 *
268 * \funcusage
269 * \snippet crypto/snippet/main.c snippet_myCryptoCoreAesEcbUse
270 *
271 *******************************************************************************/
Cy_Crypto_Core_Aes_Ecb(CRYPTO_Type * base,cy_en_crypto_dir_mode_t dirMode,uint8_t * dst,uint8_t const * src,cy_stc_crypto_aes_state_t * aesState)272 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Ecb(CRYPTO_Type *base,
273                                                 cy_en_crypto_dir_mode_t dirMode,
274                                                 uint8_t *dst,
275                                                 uint8_t const *src,
276                                                 cy_stc_crypto_aes_state_t *aesState)
277 {
278     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
279 
280     if (CY_CRYPTO_V1)
281     {
282         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
283         tmpResult = Cy_Crypto_Core_V1_Aes_Ecb(base, dirMode, dst, src, aesState);
284         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
285     }
286     else
287     {
288         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
289         tmpResult = Cy_Crypto_Core_V2_Aes_Ecb(base, dirMode, dst, src, aesState);
290         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
291     }
292 
293     return tmpResult;
294 }
295 
296 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC)
297 /*******************************************************************************
298 * Function Name: Cy_Crypto_Core_Aes_Cbc
299 ****************************************************************************//**
300 *
301 * Performs the AES-CBC operation defined in the dirMode parameter on a plain text
302 * defined in the src parameter.
303 *
304 * \param base
305 * The pointer to the CRYPTO instance.
306 *
307 * \param dirMode
308 * Can be \ref CY_CRYPTO_ENCRYPT or \ref CY_CRYPTO_DECRYPT
309 * (\ref cy_en_crypto_dir_mode_t)
310 *
311 * \param srcSize
312 * The size of the source plain text.
313 *
314 * \param ivPtr
315 * The pointer to the initial vector.
316 *
317 * \param dst
318 * The pointer to the destination cipher text.
319 *
320 * \param src
321 * The pointer to the source plain text.
322 *
323 * \param aesState
324 * The pointer to the AES state structure allocated by the user. The user
325 * must not modify anything in this structure.
326 *
327 * \return
328 * \ref cy_en_crypto_status_t
329 *
330 *******************************************************************************/
Cy_Crypto_Core_Aes_Cbc(CRYPTO_Type * base,cy_en_crypto_dir_mode_t dirMode,uint32_t srcSize,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_crypto_aes_state_t * aesState)331 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Cbc(CRYPTO_Type *base,
332                                                 cy_en_crypto_dir_mode_t dirMode,
333                                                 uint32_t srcSize,
334                                                 uint8_t *ivPtr,
335                                                 uint8_t *dst,
336                                                 uint8_t const *src,
337                                                 cy_stc_crypto_aes_state_t *aesState)
338 {
339     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
340 
341     if (CY_CRYPTO_V1)
342     {
343         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
344         tmpResult = Cy_Crypto_Core_V1_Aes_Cbc(base, dirMode, srcSize, ivPtr, dst, src, aesState);
345         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
346     }
347     else
348     {
349         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
350         tmpResult = Cy_Crypto_Core_V2_Aes_Cbc(base, dirMode, srcSize, ivPtr, dst, src, aesState);
351         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
352     }
353 
354     return tmpResult;
355 }
356 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CBC) */
357 
358 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB)
359 /*******************************************************************************
360 * Function Name: Cy_Crypto_Core_Aes_Cfb
361 ****************************************************************************//**
362 *
363 * Performs the AES-CFB operation defined in the dirMode parameter on a plain text
364 * defined in the SRC parameter.
365 *
366 * \param base
367 * The pointer to the CRYPTO instance.
368 *
369 * \param dirMode
370 * Can be \ref CY_CRYPTO_ENCRYPT or \ref CY_CRYPTO_DECRYPT
371 * (\ref cy_en_crypto_dir_mode_t)
372 *
373 * \param srcSize
374 * The size of the source plain text.
375 *
376 * \param ivPtr
377 * The pointer to the initial vector.
378 *
379 * \param dst
380 * The pointer to the destination cipher text.
381 *
382 * \param src
383 * The pointer to the source plain text.
384 *
385 * \param aesState
386 * The pointer to the AES state structure allocated by the user. The user
387 * must not modify anything in this structure.
388 *
389 * \return
390 * \ref cy_en_crypto_status_t
391 *
392 *******************************************************************************/
Cy_Crypto_Core_Aes_Cfb(CRYPTO_Type * base,cy_en_crypto_dir_mode_t dirMode,uint32_t srcSize,uint8_t * ivPtr,uint8_t * dst,uint8_t const * src,cy_stc_crypto_aes_state_t * aesState)393 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Cfb(CRYPTO_Type *base,
394                                                 cy_en_crypto_dir_mode_t dirMode,
395                                                 uint32_t srcSize,
396                                                 uint8_t *ivPtr,
397                                                 uint8_t *dst,
398                                                 uint8_t const *src,
399                                                 cy_stc_crypto_aes_state_t *aesState)
400 {
401     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
402 
403     if (CY_CRYPTO_V1)
404     {
405         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
406         tmpResult = Cy_Crypto_Core_V1_Aes_Cfb(base, dirMode, srcSize, ivPtr, dst, src, aesState);
407         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
408     }
409     else
410     {
411         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
412         tmpResult = Cy_Crypto_Core_V2_Aes_Cfb(base, dirMode, srcSize, ivPtr, dst, src, aesState);
413         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
414     }
415 
416     return tmpResult;
417 }
418 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CFB) */
419 
420 #if defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR)
421 /*******************************************************************************
422 * Function Name: Cy_Crypto_Core_Aes_Ctr
423 ****************************************************************************//**
424 *
425 * Performs the AES-CTR operation on a plain text defined in the src parameter.
426 *
427 * \param base
428 * The pointer to the CRYPTO instance.
429 *
430 * \param srcSize
431 * The size of the source plain text.
432 *
433 * \param srcOffset
434 * The size of an offset within the current block stream for resuming within the
435 * current cipher stream.
436 *
437 * \param ivPtr
438 * The 128-bit initial vector that contains a 64-bit nonce and 64-bit counter.
439 *
440 * \param streamBlock
441 * The saved stream-block for resuming. Is over-written by the function.
442 *
443 * \param dst
444 * The pointer to the destination cipher text.
445 *
446 * \param src
447 * The pointer to the source plain text. Must be 4-Byte aligned.
448 *
449 * \param aesState
450 * The pointer to the AES state structure allocated by the user. The user
451 * must not modify anything in this structure.
452 *
453 * \return
454 * \ref cy_en_crypto_status_t
455 *
456 *******************************************************************************/
Cy_Crypto_Core_Aes_Ctr(CRYPTO_Type * base,uint32_t srcSize,uint32_t * srcOffset,uint8_t * ivPtr,uint8_t * streamBlock,uint8_t * dst,uint8_t const * src,cy_stc_crypto_aes_state_t * aesState)457 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_Ctr(CRYPTO_Type *base,
458                                                 uint32_t srcSize,
459                                                 uint32_t *srcOffset,
460                                                 uint8_t *ivPtr,
461                                                 uint8_t *streamBlock,
462                                                 uint8_t *dst,
463                                                 uint8_t const *src,
464                                                 cy_stc_crypto_aes_state_t *aesState)
465 {
466     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
467 
468     if (CY_CRYPTO_V1)
469     {
470         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
471         tmpResult = Cy_Crypto_Core_V1_Aes_Ctr(base, srcSize, srcOffset, ivPtr, streamBlock, dst, src, aesState);
472         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
473     }
474     else
475     {
476         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
477         tmpResult = Cy_Crypto_Core_V2_Aes_Ctr(base, srcSize, srcOffset, ivPtr, streamBlock, dst, src, aesState);
478         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
479     }
480 
481     return tmpResult;
482 }
483 #endif /* defined(CY_CRYPTO_CFG_CIPHER_MODE_CTR) */
484 
485 #if (CPUSS_CRYPTO_GCM == 1) && defined(CY_CRYPTO_CFG_GCM_C)
486 
487 /*******************************************************************************
488 * Function Name: Cy_Crypto_Core_Aes_GCM_Init
489 ****************************************************************************//**
490 *
491 * The function to initialize AES GCM operation.
492 *
493 * \param base
494 * The pointer to the CRYPTO instance.
495 *
496 * \param aesGCMBuffers
497 * The pointer to the AES GCM buffer provided by the user. The user must not modify anything in this structure.
498 *
499 * \param aesGCMctx
500 * The pointer to the AES GCM state structure allocated by the user. The user
501 * must not modify anything in this structure.
502 *
503 * \return
504 * \ref cy_en_crypto_status_t
505 *
506 * \funcusage
507 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
508 *******************************************************************************/
509 
Cy_Crypto_Core_Aes_GCM_Init(CRYPTO_Type * base,cy_stc_crypto_aes_gcm_buffers_t * aesGCMBuffers,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)510 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Init(CRYPTO_Type *base,cy_stc_crypto_aes_gcm_buffers_t *aesGCMBuffers,
511                                                       cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
512 {
513     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
514 
515     if (CY_CRYPTO_V1)
516     {
517         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
518         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
519         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
520     }
521     else
522     {
523         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
524         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Init(base, aesGCMBuffers, aesGCMctx);
525         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
526     }
527 
528     return tmpResult;
529 }
530 
531 
532 
533 /*******************************************************************************
534 * Function Name: Cy_Crypto_Core_Aes_GCM_SetKey
535 ****************************************************************************//**
536 *
537 * The function to set AES GCM Key.
538 *
539 * \param base
540 * The pointer to the CRYPTO instance.
541 *
542 * \param aesKey
543 * The pointer to the AES key.
544 *
545 * \param keyLength
546 * \ref cy_en_crypto_aes_key_length_t
547 *
548 * \param aesGCMctx
549 * The pointer to the AES GCM state structure allocated by the user. The user
550 * must not modify anything in this structure.
551 *
552 * \return
553 * \ref cy_en_crypto_status_t
554 *
555 * \funcusage
556 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
557 *******************************************************************************/
558 
Cy_Crypto_Core_Aes_GCM_SetKey(CRYPTO_Type * base,uint8_t const * aesKey,cy_en_crypto_aes_key_length_t keyLength,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)559 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_SetKey(CRYPTO_Type *base, uint8_t const *aesKey, cy_en_crypto_aes_key_length_t keyLength,
560                                                       cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
561 {
562     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
563 
564     if (CY_CRYPTO_V1)
565     {
566         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
567         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
568         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
569     }
570     else
571     {
572         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
573         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_SetKey(base, aesKey, keyLength, aesGCMctx);
574         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
575     }
576 
577     return tmpResult;
578 }
579 
580 
581 /*******************************************************************************
582 * Function Name: Cy_Crypto_Core_Aes_GCM_Start
583 ****************************************************************************//**
584 *
585 * The function to start AES GCM operation.
586 *
587 * \param base
588 * The pointer to the CRYPTO instance.
589 *
590 * \param mode
591 * \ref cy_en_crypto_dir_mode_t
592 *
593 * \param iv
594 * The pointer to the Initialization vector.
595 *
596 * \param ivSize
597 * The length of the iv.
598 *
599 * \param aesGCMctx
600 * The pointer to the AES GCM state structure allocated by the user. The user
601 * must not modify anything in this structure.
602 *
603 * \return
604 * \ref cy_en_crypto_status_t
605 *
606 * \funcusage
607 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
608 *******************************************************************************/
609 
Cy_Crypto_Core_Aes_GCM_Start(CRYPTO_Type * base,cy_en_crypto_dir_mode_t mode,uint8_t const * iv,uint32_t ivSize,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)610 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Start(CRYPTO_Type *base, cy_en_crypto_dir_mode_t mode,
611                                                       uint8_t const *iv, uint32_t ivSize,
612                                                       cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
613 {
614     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
615 
616     if (CY_CRYPTO_V1)
617     {
618         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
619         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
620         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
621     }
622     else
623     {
624         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
625         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Start(base, mode, iv, ivSize, aesGCMctx);
626         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
627     }
628 
629     return tmpResult;
630 }
631 
632 
633 
634 /*******************************************************************************
635 * Function Name: Cy_Crypto_Core_Aes_GCM_AAD_Update
636 ****************************************************************************//**
637 *
638 * The function to update the Additional Authentication Data.
639 *
640 * \param base
641 * The pointer to the CRYPTO instance.
642 *
643 * \param aad
644 * The pointer to the Additional Authentication Data.
645 *
646 * \param aadSize
647 *  The length of the Additional Authentication Data
648 *
649 * \param aesGCMctx
650 * The pointer to the AES GCM state structure allocated by the user. The user
651 * must not modify anything in this structure.
652 *
653 * \return
654 * \ref cy_en_crypto_status_t
655 *
656 * \funcusage
657 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
658 *******************************************************************************/
659 
Cy_Crypto_Core_Aes_GCM_AAD_Update(CRYPTO_Type * base,uint8_t * aad,uint32_t aadSize,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)660 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_AAD_Update(CRYPTO_Type *base, uint8_t *aad,
661                                                                         uint32_t aadSize,
662                                                                         cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
663 
664 {
665     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
666 
667     if (CY_CRYPTO_V1)
668     {
669         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
670         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
671         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
672     }
673     else
674     {
675         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
676         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_AAD_Update(base, aad, aadSize, aesGCMctx);
677         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
678     }
679 
680     return tmpResult;
681 }
682 
683 
684 
685 /*******************************************************************************
686 * Function Name: Cy_Crypto_Core_Aes_GCM_Update
687 ****************************************************************************//**
688 *
689 * The function to update the data
690 *
691 * \param base
692 * The pointer to the CRYPTO instance.
693 *
694 * \param input
695 * The pointer to the input data to be encrypted/decrypted.
696 *
697 * \param inputSize
698 *  The length of the input data.
699 *
700 * \param output
701 * The pointer to the encrypted/decrypted output data.
702 *
703 * \param aesGCMctx
704 * The pointer to the AES GCm state structure allocated by the user. The user
705 * must not modify anything in this structure.
706 *
707 * \return
708 * \ref cy_en_crypto_status_t
709 *
710 * \funcusage
711 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
712 *******************************************************************************/
Cy_Crypto_Core_Aes_GCM_Update(CRYPTO_Type * base,const uint8_t * input,uint32_t inputSize,uint8_t * output,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)713 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Update(CRYPTO_Type *base,  const uint8_t *input,
714                                                        uint32_t inputSize,  uint8_t *output,
715                                                        cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
716 
717 {
718     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
719 
720     if (CY_CRYPTO_V1)
721     {
722         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
723         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
724         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
725     }
726     else
727     {
728         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
729         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Update(base, input, inputSize, output, aesGCMctx);
730         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
731     }
732 
733     return tmpResult;
734 }
735 
736 
737 
738 
739 /*******************************************************************************
740 * Function Name: Cy_Crypto_Core_Aes_GCM_Finish
741 ****************************************************************************//**
742 *
743 * The function to finish the AES GCM operation and to calculate the tag.
744 *
745 * \param base
746 * The pointer to the CRYPTO instance.
747 *
748 * \param p_tag
749 * The pointer to the buffer for storing tag.
750 *
751 * \param tagSize
752 *  The length of the p_tag.
753 *
754 * \param aesGCMctx
755 * The pointer to the AES aesGCMctx structure allocated by the user. The user
756 * must not modify anything in this structure.
757 *
758 * \return
759 * \ref cy_en_crypto_status_t
760 *
761 * \funcusage
762 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
763 *******************************************************************************/
764 
Cy_Crypto_Core_Aes_GCM_Finish(CRYPTO_Type * base,uint8_t * p_tag,uint32_t tagSize,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)765 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Finish(CRYPTO_Type *base,  uint8_t *p_tag,
766                                                               uint32_t tagSize, cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
767 
768 
769 {
770     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
771 
772     if (CY_CRYPTO_V1)
773     {
774         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
775         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
776         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
777     }
778     else
779     {
780         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
781         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Finish(base, p_tag, tagSize, aesGCMctx);
782         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
783     }
784 
785     return tmpResult;
786 }
787 
788 
789 
790 /*******************************************************************************
791 * Function Name: Cy_Crypto_Core_Aes_GCM_Free
792 ****************************************************************************//**
793 *
794 * The function to finish the encryption process and calculate tag.
795 *
796 * \param base
797 * The pointer to the CRYPTO instance.
798 *
799 * \param aesGCMctx
800 * The pointer to the AES aesGCMctx structure allocated by the user. The user
801 * must not modify anything in this structure.
802 *
803 * \return
804 * \ref cy_en_crypto_status_t
805 *
806 * \funcusage
807 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_init_update_finish_free
808 *******************************************************************************/
809 
Cy_Crypto_Core_Aes_GCM_Free(CRYPTO_Type * base,cy_stc_crypto_aes_gcm_state_t * aesGCMctx)810 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Free(CRYPTO_Type *base,  cy_stc_crypto_aes_gcm_state_t* aesGCMctx)
811 
812 
813 {
814     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
815 
816     if (CY_CRYPTO_V1)
817     {
818         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
819         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
820         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
821     }
822     else
823     {
824         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
825         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Free(base, aesGCMctx);
826         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
827     }
828 
829     return tmpResult;
830 }
831 
832 
833 /*******************************************************************************
834 * Function Name: Cy_Crypto_Core_Aes_GCM_Encrypt_Tag
835 ****************************************************************************//**
836 *
837 * Performs the AES GCM encryption operation on the input data, iv & aad data, generates the encrypted data and TAG.
838 *
839 * \param base
840 * The pointer to the CRYPTO instance.
841 *
842 * \param aesKey
843 * The pointer to the AES key.
844 *
845 * \param keyLength
846 * \ref cy_en_crypto_aes_key_length_t
847 *
848 * \param iv
849 * The pointer to the Initialization vector.
850 *
851 * \param ivSize
852 * The length of the iv.
853 *
854 * \param aad
855 * The pointer to the Additional Authentication Data.
856 *
857 * \param aadSize
858 *  The length of the additional Authentication Data
859 *
860 * \param input
861 * The pointer to the input data to be encrypted/decrypted.
862 *
863 * \param inputSize
864 *  The length of the input data.
865 *
866 * \param output
867 * The pointer to the encrypted/decrypted output data.
868 *
869 * \param tag
870 * The pointer to the tag.
871 *
872 * \param tagSize
873 *  The length of the p_tag.
874 *
875 * \return
876 * \ref cy_en_crypto_status_t
877 *
878 * \funcusage
879 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_Encrypt_Tag
880 *******************************************************************************/
Cy_Crypto_Core_Aes_GCM_Encrypt_Tag(CRYPTO_Type * base,uint8_t const * aesKey,cy_en_crypto_aes_key_length_t keyLength,uint8_t const * iv,uint32_t ivSize,uint8_t * aad,uint32_t aadSize,const uint8_t * input,uint32_t inputSize,uint8_t * output,uint8_t * tag,uint32_t tagSize)881 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Encrypt_Tag(CRYPTO_Type *base, uint8_t const *aesKey, cy_en_crypto_aes_key_length_t keyLength,
882                                                             uint8_t const *iv, uint32_t ivSize, uint8_t *aad,   uint32_t aadSize,
883                                                             const uint8_t *input,   uint32_t inputSize,  uint8_t *output, uint8_t *tag, uint32_t tagSize)
884 
885 {
886     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
887 
888     if (CY_CRYPTO_V1)
889     {
890         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
891         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
892         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
893     }
894     else
895     {
896         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
897         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Encrypt_Tag(base, aesKey, keyLength,
898                                                           iv, ivSize, aad, aadSize,
899                                                           input, inputSize, output, tag, tagSize);
900         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
901     }
902 
903     return tmpResult;
904 }
905 
906 
907 /*******************************************************************************
908 * Function Name: Cy_Crypto_Core_Aes_GCM_Decrypt_Tag
909 ****************************************************************************//**
910 *
911 * Performs the AES GCM decryption operation on the input data and verifies the TAG.
912 *
913 * \param base
914 * The pointer to the CRYPTO instance.
915 *
916 * \param aesKey
917 * The pointer to the AES key.
918 *
919 * \param keyLength
920 * \ref cy_en_crypto_aes_key_length_t
921 *
922 * \param iv
923 * The pointer to the Initialization vector.
924 *
925 * \param ivSize
926 * The length of the iv.
927 *
928 * \param aad
929 * The pointer to the Additional Authentication Data.
930 *
931 * \param aadSize
932 *  The length of the additional Authentication Data
933 *
934 * \param input
935 * The pointer to the input data to be encrypted/decrypted.
936 *
937 * \param inputSize
938 *  The length of the input data.
939 *
940 * \param tag
941 * The pointer to the tag.
942 *
943 * \param tagSize
944 *  The length of the p_tag.
945 *
946 * \param output
947 * The pointer to the encrypted/decrypted output data.
948 *
949 * \param isVerified
950 * The status of the AES GCM verification.
951 *
952 * \return
953 * \ref cy_en_crypto_status_t
954 *
955 * \funcusage
956 * \snippet crypto/snippet/main.c snippet_Cy_Crypto_Core_Aes_GCM_Decrypt_Tag
957 *******************************************************************************/
Cy_Crypto_Core_Aes_GCM_Decrypt_Tag(CRYPTO_Type * base,uint8_t const * aesKey,cy_en_crypto_aes_key_length_t keyLength,uint8_t const * iv,uint32_t ivSize,uint8_t * aad,uint32_t aadSize,const uint8_t * input,uint32_t inputSize,uint8_t * tag,uint32_t tagSize,uint8_t * output,cy_en_crypto_aesgcm_tag_verify_result_t * isVerified)958 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Aes_GCM_Decrypt_Tag(CRYPTO_Type *base, uint8_t const *aesKey, cy_en_crypto_aes_key_length_t keyLength,
959                                                             uint8_t const *iv, uint32_t ivSize, uint8_t *aad,   uint32_t aadSize,
960                                                             const uint8_t *input,   uint32_t inputSize, uint8_t *tag, uint32_t tagSize, uint8_t *output,
961                                                             cy_en_crypto_aesgcm_tag_verify_result_t * isVerified)
962 {
963     cy_en_crypto_status_t tmpResult = CY_CRYPTO_NOT_SUPPORTED;
964 
965     if (CY_CRYPTO_V1)
966     {
967         #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
968         tmpResult = CY_CRYPTO_NOT_SUPPORTED;
969         #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
970     }
971     else
972     {
973         #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
974         tmpResult = Cy_Crypto_Core_V2_Aes_GCM_Decrypt_Tag(base, aesKey, keyLength,
975                                                             iv, ivSize, aad, aadSize,
976                                                             input, inputSize, tag, tagSize, output, isVerified);
977         #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
978     }
979 
980     return tmpResult;
981 }
982 #endif /* (CPUSS_CRYPTO_GCM == 1) && defined(CY_CRYPTO_CFG_GCM_C)*/
983 
984 /** \} group_crypto_lld_symmetric_functions */
985 
986 #endif /* (CPUSS_CRYPTO_AES == 1) && defined(CY_CRYPTO_CFG_AES_C) */
987 
988 #if defined(__cplusplus)
989 }
990 #endif
991 
992 #endif /* CY_IP_MXCRYPTO */
993 
994 #endif /* #if !defined (CY_CRYPTO_CORE_AES_H) */
995 
996 
997 /* [] END OF FILE */
998