mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Child:
2:bbdeda018a3c
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /*
markrad 0:cdf462088d13 2 * Threading abstraction layer
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 5 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 6 *
markrad 0:cdf462088d13 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 8 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 9 * You may obtain a copy of the License at
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 16 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 17 * limitations under the License.
markrad 0:cdf462088d13 18 *
markrad 0:cdf462088d13 19 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 20 */
markrad 0:cdf462088d13 21
markrad 0:cdf462088d13 22 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 23 #include "mbedtls/config.h"
markrad 0:cdf462088d13 24 #else
markrad 0:cdf462088d13 25 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 26 #endif
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 #include "mbedtls/threading.h"
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #if defined(MBEDTLS_THREADING_PTHREAD)
markrad 0:cdf462088d13 33 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 34 {
markrad 0:cdf462088d13 35 if( mutex == NULL || mutex->is_valid )
markrad 0:cdf462088d13 36 return;
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
markrad 0:cdf462088d13 39 }
markrad 0:cdf462088d13 40
markrad 0:cdf462088d13 41 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 42 {
markrad 0:cdf462088d13 43 if( mutex == NULL || !mutex->is_valid )
markrad 0:cdf462088d13 44 return;
markrad 0:cdf462088d13 45
markrad 0:cdf462088d13 46 (void) pthread_mutex_destroy( &mutex->mutex );
markrad 0:cdf462088d13 47 mutex->is_valid = 0;
markrad 0:cdf462088d13 48 }
markrad 0:cdf462088d13 49
markrad 0:cdf462088d13 50 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 51 {
markrad 0:cdf462088d13 52 if( mutex == NULL || ! mutex->is_valid )
markrad 0:cdf462088d13 53 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
markrad 0:cdf462088d13 56 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
markrad 0:cdf462088d13 57
markrad 0:cdf462088d13 58 return( 0 );
markrad 0:cdf462088d13 59 }
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 62 {
markrad 0:cdf462088d13 63 if( mutex == NULL || ! mutex->is_valid )
markrad 0:cdf462088d13 64 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
markrad 0:cdf462088d13 67 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
markrad 0:cdf462088d13 68
markrad 0:cdf462088d13 69 return( 0 );
markrad 0:cdf462088d13 70 }
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
markrad 0:cdf462088d13 73 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
markrad 0:cdf462088d13 74 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
markrad 0:cdf462088d13 75 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
markrad 0:cdf462088d13 76
markrad 0:cdf462088d13 77 /*
markrad 0:cdf462088d13 78 * With phtreads we can statically initialize mutexes
markrad 0:cdf462088d13 79 */
markrad 0:cdf462088d13 80 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 #endif /* MBEDTLS_THREADING_PTHREAD */
markrad 0:cdf462088d13 83
markrad 0:cdf462088d13 84 #if defined(MBEDTLS_THREADING_ALT)
markrad 0:cdf462088d13 85 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 86 {
markrad 0:cdf462088d13 87 ((void) mutex );
markrad 0:cdf462088d13 88 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
markrad 0:cdf462088d13 89 }
markrad 0:cdf462088d13 90 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
markrad 0:cdf462088d13 91 {
markrad 0:cdf462088d13 92 ((void) mutex );
markrad 0:cdf462088d13 93 return;
markrad 0:cdf462088d13 94 }
markrad 0:cdf462088d13 95
markrad 0:cdf462088d13 96 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
markrad 0:cdf462088d13 97 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
markrad 0:cdf462088d13 98 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
markrad 0:cdf462088d13 99 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
markrad 0:cdf462088d13 100
markrad 0:cdf462088d13 101 /*
markrad 0:cdf462088d13 102 * Set functions pointers and initialize global mutexes
markrad 0:cdf462088d13 103 */
markrad 0:cdf462088d13 104 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
markrad 0:cdf462088d13 105 void (*mutex_free)( mbedtls_threading_mutex_t * ),
markrad 0:cdf462088d13 106 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
markrad 0:cdf462088d13 107 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
markrad 0:cdf462088d13 108 {
markrad 0:cdf462088d13 109 mbedtls_mutex_init = mutex_init;
markrad 0:cdf462088d13 110 mbedtls_mutex_free = mutex_free;
markrad 0:cdf462088d13 111 mbedtls_mutex_lock = mutex_lock;
markrad 0:cdf462088d13 112 mbedtls_mutex_unlock = mutex_unlock;
markrad 0:cdf462088d13 113
markrad 0:cdf462088d13 114 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
markrad 0:cdf462088d13 115 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
markrad 0:cdf462088d13 116 }
markrad 0:cdf462088d13 117
markrad 0:cdf462088d13 118 /*
markrad 0:cdf462088d13 119 * Free global mutexes
markrad 0:cdf462088d13 120 */
markrad 0:cdf462088d13 121 void mbedtls_threading_free_alt( void )
markrad 0:cdf462088d13 122 {
markrad 0:cdf462088d13 123 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
markrad 0:cdf462088d13 124 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
markrad 0:cdf462088d13 125 }
markrad 0:cdf462088d13 126 #endif /* MBEDTLS_THREADING_ALT */
markrad 0:cdf462088d13 127
markrad 0:cdf462088d13 128 /*
markrad 0:cdf462088d13 129 * Define global mutexes
markrad 0:cdf462088d13 130 */
markrad 0:cdf462088d13 131 #ifndef MUTEX_INIT
markrad 0:cdf462088d13 132 #define MUTEX_INIT
markrad 0:cdf462088d13 133 #endif
markrad 0:cdf462088d13 134 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
markrad 0:cdf462088d13 135 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
markrad 0:cdf462088d13 136
markrad 0:cdf462088d13 137 #endif /* MBEDTLS_THREADING_C */