]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/mbedtls/mbedtls_platform_freertos.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-IoT-Libraries / abstractions / mbedtls / mbedtls_platform_freertos.c
diff --git a/FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/mbedtls/mbedtls_platform_freertos.c b/FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/mbedtls/mbedtls_platform_freertos.c
new file mode 100644 (file)
index 0000000..2fadb89
--- /dev/null
@@ -0,0 +1,173 @@
+/*\r
+ * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ */\r
+\r
+/**\r
+ * @file mbedtls_platform.c\r
+ * @brief Implements mbed TLS platform functions for FreeRTOS.\r
+ */\r
+\r
+/* FreeRTOS includes. */\r
+#include "FreeRTOS.h"\r
+#include "FreeRTOS_Sockets.h"\r
+\r
+/* mbed TLS includes. */\r
+#include "mbedtls_config.h"\r
+#include "threading_alt.h"\r
+#include "mbedtls/entropy.h"\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void * mbedtls_platform_calloc( size_t nmemb,\r
+                                size_t size )\r
+{\r
+    size_t totalSize = nmemb * size;\r
+    void * pBuffer = NULL;\r
+\r
+    /* Check that neither nmemb nor size were 0. */\r
+    if( totalSize > 0 )\r
+    {\r
+        /* Overflow check. */\r
+        if( totalSize / size == nmemb )\r
+        {\r
+            pBuffer = pvPortMalloc( totalSize );\r
+\r
+            if( pBuffer != NULL )\r
+            {\r
+                ( void ) memset( pBuffer, 0x00, totalSize );\r
+            }\r
+        }\r
+    }\r
+\r
+    return pBuffer;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void mbedtls_platform_free( void * ptr )\r
+{\r
+    vPortFree( ptr );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int mbedtls_platform_send( void * ctx,\r
+                           const unsigned char * buf,\r
+                           size_t len )\r
+{\r
+    Socket_t socket = ctx;\r
+\r
+    return ( int ) FreeRTOS_send( socket, buf, len, 0 );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int mbedtls_platform_recv( void * ctx,\r
+                           unsigned char * buf,\r
+                           size_t len )\r
+{\r
+    Socket_t socket = ctx;\r
+\r
+    return ( int ) FreeRTOS_recv( socket, buf, len, 0 );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void mbedtls_platform_mutex_init( mbedtls_threading_mutex_t * pMutex )\r
+{\r
+    /* Create a statically-allocated FreeRTOS mutex. This should never fail as\r
+     * storage is provided. */\r
+    pMutex->mutexHandle = xSemaphoreCreateMutexStatic( &( pMutex->mutexStorage ) );\r
+    configASSERT( pMutex->mutexHandle != NULL );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void mbedtls_platform_mutex_free( mbedtls_threading_mutex_t * pMutex )\r
+{\r
+    /* Nothing needs to be done to free a statically-allocated FreeRTOS mutex. */\r
+    ( void ) pMutex;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int mbedtls_platform_mutex_lock( mbedtls_threading_mutex_t * pMutex )\r
+{\r
+    BaseType_t mutexStatus = 0;\r
+\r
+    /* mutexStatus is not used if asserts are disabled. */\r
+    ( void ) mutexStatus;\r
+\r
+    /* This function should never fail if the mutex is initialized. */\r
+    mutexStatus = xSemaphoreTake( pMutex->mutexHandle, portMAX_DELAY );\r
+    configASSERT( mutexStatus == pdTRUE );\r
+\r
+    return 0;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int mbedtls_platform_mutex_unlock( mbedtls_threading_mutex_t * pMutex )\r
+{\r
+    BaseType_t mutexStatus = 0;\r
+\r
+    /* mutexStatus is not used if asserts are disabled. */\r
+    ( void ) mutexStatus;\r
+\r
+    /* This function should never fail if the mutex is initialized. */\r
+    mutexStatus = xSemaphoreGive( pMutex->mutexHandle );\r
+    configASSERT( mutexStatus == pdTRUE );\r
+\r
+    return 0;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int mbedtls_platform_entropy_poll( void * data,\r
+                                   unsigned char * output,\r
+                                   size_t len,\r
+                                   size_t * olen )\r
+{\r
+    int status = 0;\r
+    NTSTATUS rngStatus = 0;\r
+\r
+    /* Context is not used by this function. */\r
+    ( void ) data;\r
+\r
+    /* TLS requires a secure random number generator; use the RNG provided\r
+     * by Windows. This function MUST be re-implemented for other platforms. */\r
+    rngStatus = BCryptGenRandom( NULL, output, len, BCRYPT_USE_SYSTEM_PREFERRED_RNG );\r
+\r
+    if( rngStatus == 0 )\r
+    {\r
+        /* All random bytes generated. */\r
+        *olen = len;\r
+    }\r
+    else\r
+    {\r
+        /* RNG failure. */\r
+        *olen = 0;\r
+        status = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;\r
+    }\r
+\r
+    return status;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r