1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 #ifndef _DEMO_HELPER_FUNCTIONS_
28 #define _DEMO_HELPER_FUNCTIONS_
29 
30 #include "core_pkcs11.h"
31 #include "threading_alt.h"
32 #include "mbedtls/pk.h"
33 
34 /* This function contains standard setup code for PKCS #11. See the
35  * "management_and_rng.c" file for the demo code explaining this section
36  * of cryptoki.
37  */
38 void vStart( CK_SESSION_HANDLE * pxSession,
39              CK_SLOT_ID ** ppxSlotId );
40 /*-----------------------------------------------------------*/
41 
42 /* This function contains standard tear down code for PKCS #11. See the
43  * "management_and_rng.c" file for the demo code explaining this section
44  * of cryptoki.
45  */
46 void vEnd( CK_SESSION_HANDLE xSession,
47            CK_SLOT_ID * pxSlotId );
48 /*-----------------------------------------------------------*/
49 
50 /* This function is simply a helper function to print the raw hex values
51  * of an EC public key. It's explanation is not within the scope of the demos
52  * and is sparsely commented. */
53 void vWriteHexBytesToConsole( char * pcDescription,
54                               CK_BYTE * pucData,
55                               CK_ULONG ulDataLength );
56 /*-----------------------------------------------------------*/
57 
58 /* This function is simply a helper function to export the raw hex values
59  * of an EC public key into a buffer. It's explanation is not within the
60  * scope of the demos and is sparsely commented. */
61 CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession,
62                         CK_OBJECT_HANDLE xPublicKeyHandle,
63                         CK_BYTE ** ppucDerPublicKey,
64                         CK_ULONG * pulDerPublicKeyLength );
65 /*-----------------------------------------------------------*/
66 
67 /**
68  * @brief Implements libc calloc semantics using the FreeRTOS heap
69  */
70 void * pvCalloc( size_t xNumElements,
71                  size_t xSize );
72 /*-----------------------------------------------------------*/
73 
74 /**
75  * @brief Implementation of mbedtls_mutex_init for thread-safety.
76  *
77  */
78 void aws_mbedtls_mutex_init( mbedtls_threading_mutex_t * mutex );
79 /*-----------------------------------------------------------*/
80 
81 /**
82  * @brief Implementation of mbedtls_mutex_free for thread-safety.
83  *
84  */
85 void aws_mbedtls_mutex_free( mbedtls_threading_mutex_t * mutex );
86 /*-----------------------------------------------------------*/
87 
88 /**
89  * @brief Implementation of mbedtls_mutex_lock for thread-safety.
90  *
91  * @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout,
92  * MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid.
93  */
94 int aws_mbedtls_mutex_lock( mbedtls_threading_mutex_t * mutex );
95 /*-----------------------------------------------------------*/
96 
97 /**
98  * @brief Implementation of mbedtls_mutex_unlock for thread-safety.
99  *
100  * @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout,
101  * MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid.
102  */
103 int aws_mbedtls_mutex_unlock( mbedtls_threading_mutex_t * mutex );
104 
105 #endif /* _DEMO_HELPER_FUNCTIONS_ */
106