I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Thu Nov 17 16:13:29 2016 +0000
Revision:
18:198e9b0acf11
Parent:
12:0071cb144c7a
Updates to mbed os resulted in mutex.h going away and rtos.h needed to be used; This fixes the Mutex typedef failure.  Also cast data buffers from 'char *' to (const std::uint8_t*) to conform with Fred's changes in WncController

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 12:0071cb144c7a 1 /*
JMF 12:0071cb144c7a 2 * Platform-specific and custom entropy polling functions
JMF 12:0071cb144c7a 3 *
JMF 12:0071cb144c7a 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
JMF 12:0071cb144c7a 5 * SPDX-License-Identifier: Apache-2.0
JMF 12:0071cb144c7a 6 *
JMF 12:0071cb144c7a 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
JMF 12:0071cb144c7a 8 * not use this file except in compliance with the License.
JMF 12:0071cb144c7a 9 * You may obtain a copy of the License at
JMF 12:0071cb144c7a 10 *
JMF 12:0071cb144c7a 11 * http://www.apache.org/licenses/LICENSE-2.0
JMF 12:0071cb144c7a 12 *
JMF 12:0071cb144c7a 13 * Unless required by applicable law or agreed to in writing, software
JMF 12:0071cb144c7a 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
JMF 12:0071cb144c7a 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 12:0071cb144c7a 16 * See the License for the specific language governing permissions and
JMF 12:0071cb144c7a 17 * limitations under the License.
JMF 12:0071cb144c7a 18 *
JMF 12:0071cb144c7a 19 * This file is part of mbed TLS (https://tls.mbed.org)
JMF 12:0071cb144c7a 20 */
JMF 12:0071cb144c7a 21
JMF 12:0071cb144c7a 22 #if !defined(MBEDTLS_CONFIG_FILE)
JMF 12:0071cb144c7a 23 #include "mbedtls/config.h"
JMF 12:0071cb144c7a 24 #else
JMF 12:0071cb144c7a 25 #include MBEDTLS_CONFIG_FILE
JMF 12:0071cb144c7a 26 #endif
JMF 12:0071cb144c7a 27
JMF 12:0071cb144c7a 28 #if defined(MBEDTLS_ENTROPY_C)
JMF 12:0071cb144c7a 29
JMF 12:0071cb144c7a 30 #include "mbedtls/entropy.h"
JMF 12:0071cb144c7a 31 #include "mbedtls/entropy_poll.h"
JMF 12:0071cb144c7a 32
JMF 12:0071cb144c7a 33 #if defined(MBEDTLS_TIMING_C)
JMF 12:0071cb144c7a 34 #include <string.h>
JMF 12:0071cb144c7a 35 #include "mbedtls/timing.h"
JMF 12:0071cb144c7a 36 #endif
JMF 12:0071cb144c7a 37 #if defined(MBEDTLS_HAVEGE_C)
JMF 12:0071cb144c7a 38 #include "mbedtls/havege.h"
JMF 12:0071cb144c7a 39 #endif
JMF 12:0071cb144c7a 40 #if defined(MBEDTLS_ENTROPY_NV_SEED)
JMF 12:0071cb144c7a 41 #include "mbedtls/platform.h"
JMF 12:0071cb144c7a 42 #endif
JMF 12:0071cb144c7a 43
JMF 12:0071cb144c7a 44 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
JMF 12:0071cb144c7a 45
JMF 12:0071cb144c7a 46 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
JMF 12:0071cb144c7a 47 !defined(__APPLE__) && !defined(_WIN32)
JMF 12:0071cb144c7a 48 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
JMF 12:0071cb144c7a 49 #endif
JMF 12:0071cb144c7a 50
JMF 12:0071cb144c7a 51 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
JMF 12:0071cb144c7a 52
JMF 12:0071cb144c7a 53 #if !defined(_WIN32_WINNT)
JMF 12:0071cb144c7a 54 #define _WIN32_WINNT 0x0400
JMF 12:0071cb144c7a 55 #endif
JMF 12:0071cb144c7a 56 #include <windows.h>
JMF 12:0071cb144c7a 57 #include <wincrypt.h>
JMF 12:0071cb144c7a 58
JMF 12:0071cb144c7a 59 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
JMF 12:0071cb144c7a 60 size_t *olen )
JMF 12:0071cb144c7a 61 {
JMF 12:0071cb144c7a 62 HCRYPTPROV provider;
JMF 12:0071cb144c7a 63 ((void) data);
JMF 12:0071cb144c7a 64 *olen = 0;
JMF 12:0071cb144c7a 65
JMF 12:0071cb144c7a 66 if( CryptAcquireContext( &provider, NULL, NULL,
JMF 12:0071cb144c7a 67 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
JMF 12:0071cb144c7a 68 {
JMF 12:0071cb144c7a 69 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 70 }
JMF 12:0071cb144c7a 71
JMF 12:0071cb144c7a 72 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
JMF 12:0071cb144c7a 73 {
JMF 12:0071cb144c7a 74 CryptReleaseContext( provider, 0 );
JMF 12:0071cb144c7a 75 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 76 }
JMF 12:0071cb144c7a 77
JMF 12:0071cb144c7a 78 CryptReleaseContext( provider, 0 );
JMF 12:0071cb144c7a 79 *olen = len;
JMF 12:0071cb144c7a 80
JMF 12:0071cb144c7a 81 return( 0 );
JMF 12:0071cb144c7a 82 }
JMF 12:0071cb144c7a 83 #else /* _WIN32 && !EFIX64 && !EFI32 */
JMF 12:0071cb144c7a 84
JMF 12:0071cb144c7a 85 /*
JMF 12:0071cb144c7a 86 * Test for Linux getrandom() support.
JMF 12:0071cb144c7a 87 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
JMF 12:0071cb144c7a 88 * available in GNU libc and compatible libc's (eg uClibc).
JMF 12:0071cb144c7a 89 */
JMF 12:0071cb144c7a 90 #if defined(__linux__) && defined(__GLIBC__)
JMF 12:0071cb144c7a 91 #include <unistd.h>
JMF 12:0071cb144c7a 92 #include <sys/syscall.h>
JMF 12:0071cb144c7a 93 #if defined(SYS_getrandom)
JMF 12:0071cb144c7a 94 #define HAVE_GETRANDOM
JMF 12:0071cb144c7a 95
JMF 12:0071cb144c7a 96 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
JMF 12:0071cb144c7a 97 {
JMF 12:0071cb144c7a 98 /* MemSan cannot understand that the syscall writes to the buffer */
JMF 12:0071cb144c7a 99 #if defined(__has_feature)
JMF 12:0071cb144c7a 100 #if __has_feature(memory_sanitizer)
JMF 12:0071cb144c7a 101 memset( buf, 0, buflen );
JMF 12:0071cb144c7a 102 #endif
JMF 12:0071cb144c7a 103 #endif
JMF 12:0071cb144c7a 104
JMF 12:0071cb144c7a 105 return( syscall( SYS_getrandom, buf, buflen, flags ) );
JMF 12:0071cb144c7a 106 }
JMF 12:0071cb144c7a 107
JMF 12:0071cb144c7a 108 #include <sys/utsname.h>
JMF 12:0071cb144c7a 109 /* Check if version is at least 3.17.0 */
JMF 12:0071cb144c7a 110 static int check_version_3_17_plus( void )
JMF 12:0071cb144c7a 111 {
JMF 12:0071cb144c7a 112 int minor;
JMF 12:0071cb144c7a 113 struct utsname un;
JMF 12:0071cb144c7a 114 const char *ver;
JMF 12:0071cb144c7a 115
JMF 12:0071cb144c7a 116 /* Get version information */
JMF 12:0071cb144c7a 117 uname(&un);
JMF 12:0071cb144c7a 118 ver = un.release;
JMF 12:0071cb144c7a 119
JMF 12:0071cb144c7a 120 /* Check major version; assume a single digit */
JMF 12:0071cb144c7a 121 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
JMF 12:0071cb144c7a 122 return( -1 );
JMF 12:0071cb144c7a 123
JMF 12:0071cb144c7a 124 if( ver[0] - '0' > 3 )
JMF 12:0071cb144c7a 125 return( 0 );
JMF 12:0071cb144c7a 126
JMF 12:0071cb144c7a 127 /* Ok, so now we know major == 3, check minor.
JMF 12:0071cb144c7a 128 * Assume 1 or 2 digits. */
JMF 12:0071cb144c7a 129 if( ver[2] < '0' || ver[2] > '9' )
JMF 12:0071cb144c7a 130 return( -1 );
JMF 12:0071cb144c7a 131
JMF 12:0071cb144c7a 132 minor = ver[2] - '0';
JMF 12:0071cb144c7a 133
JMF 12:0071cb144c7a 134 if( ver[3] >= '0' && ver[3] <= '9' )
JMF 12:0071cb144c7a 135 minor = 10 * minor + ver[3] - '0';
JMF 12:0071cb144c7a 136 else if( ver [3] != '.' )
JMF 12:0071cb144c7a 137 return( -1 );
JMF 12:0071cb144c7a 138
JMF 12:0071cb144c7a 139 if( minor < 17 )
JMF 12:0071cb144c7a 140 return( -1 );
JMF 12:0071cb144c7a 141
JMF 12:0071cb144c7a 142 return( 0 );
JMF 12:0071cb144c7a 143 }
JMF 12:0071cb144c7a 144 static int has_getrandom = -1;
JMF 12:0071cb144c7a 145 #endif /* SYS_getrandom */
JMF 12:0071cb144c7a 146 #endif /* __linux__ */
JMF 12:0071cb144c7a 147
JMF 12:0071cb144c7a 148 #include <stdio.h>
JMF 12:0071cb144c7a 149
JMF 12:0071cb144c7a 150 int mbedtls_platform_entropy_poll( void *data,
JMF 12:0071cb144c7a 151 unsigned char *output, size_t len, size_t *olen )
JMF 12:0071cb144c7a 152 {
JMF 12:0071cb144c7a 153 FILE *file;
JMF 12:0071cb144c7a 154 size_t read_len;
JMF 12:0071cb144c7a 155 ((void) data);
JMF 12:0071cb144c7a 156
JMF 12:0071cb144c7a 157 #if defined(HAVE_GETRANDOM)
JMF 12:0071cb144c7a 158 if( has_getrandom == -1 )
JMF 12:0071cb144c7a 159 has_getrandom = ( check_version_3_17_plus() == 0 );
JMF 12:0071cb144c7a 160
JMF 12:0071cb144c7a 161 if( has_getrandom )
JMF 12:0071cb144c7a 162 {
JMF 12:0071cb144c7a 163 int ret;
JMF 12:0071cb144c7a 164
JMF 12:0071cb144c7a 165 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
JMF 12:0071cb144c7a 166 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 167
JMF 12:0071cb144c7a 168 *olen = ret;
JMF 12:0071cb144c7a 169 return( 0 );
JMF 12:0071cb144c7a 170 }
JMF 12:0071cb144c7a 171 #endif /* HAVE_GETRANDOM */
JMF 12:0071cb144c7a 172
JMF 12:0071cb144c7a 173 *olen = 0;
JMF 12:0071cb144c7a 174
JMF 12:0071cb144c7a 175 file = fopen( "/dev/urandom", "rb" );
JMF 12:0071cb144c7a 176 if( file == NULL )
JMF 12:0071cb144c7a 177 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 178
JMF 12:0071cb144c7a 179 read_len = fread( output, 1, len, file );
JMF 12:0071cb144c7a 180 if( read_len != len )
JMF 12:0071cb144c7a 181 {
JMF 12:0071cb144c7a 182 fclose( file );
JMF 12:0071cb144c7a 183 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 184 }
JMF 12:0071cb144c7a 185
JMF 12:0071cb144c7a 186 fclose( file );
JMF 12:0071cb144c7a 187 *olen = len;
JMF 12:0071cb144c7a 188
JMF 12:0071cb144c7a 189 return( 0 );
JMF 12:0071cb144c7a 190 }
JMF 12:0071cb144c7a 191 #endif /* _WIN32 && !EFIX64 && !EFI32 */
JMF 12:0071cb144c7a 192 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
JMF 12:0071cb144c7a 193
JMF 12:0071cb144c7a 194 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
JMF 12:0071cb144c7a 195 int mbedtls_null_entropy_poll( void *data,
JMF 12:0071cb144c7a 196 unsigned char *output, size_t len, size_t *olen )
JMF 12:0071cb144c7a 197 {
JMF 12:0071cb144c7a 198 ((void) data);
JMF 12:0071cb144c7a 199 ((void) output);
JMF 12:0071cb144c7a 200 *olen = 0;
JMF 12:0071cb144c7a 201
JMF 12:0071cb144c7a 202 if( len < sizeof(unsigned char) )
JMF 12:0071cb144c7a 203 return( 0 );
JMF 12:0071cb144c7a 204
JMF 12:0071cb144c7a 205 *olen = sizeof(unsigned char);
JMF 12:0071cb144c7a 206
JMF 12:0071cb144c7a 207 return( 0 );
JMF 12:0071cb144c7a 208 }
JMF 12:0071cb144c7a 209 #endif
JMF 12:0071cb144c7a 210
JMF 12:0071cb144c7a 211 #if defined(MBEDTLS_TIMING_C)
JMF 12:0071cb144c7a 212 int mbedtls_hardclock_poll( void *data,
JMF 12:0071cb144c7a 213 unsigned char *output, size_t len, size_t *olen )
JMF 12:0071cb144c7a 214 {
JMF 12:0071cb144c7a 215 unsigned long timer = mbedtls_timing_hardclock();
JMF 12:0071cb144c7a 216 ((void) data);
JMF 12:0071cb144c7a 217 *olen = 0;
JMF 12:0071cb144c7a 218
JMF 12:0071cb144c7a 219 if( len < sizeof(unsigned long) )
JMF 12:0071cb144c7a 220 return( 0 );
JMF 12:0071cb144c7a 221
JMF 12:0071cb144c7a 222 memcpy( output, &timer, sizeof(unsigned long) );
JMF 12:0071cb144c7a 223 *olen = sizeof(unsigned long);
JMF 12:0071cb144c7a 224
JMF 12:0071cb144c7a 225 return( 0 );
JMF 12:0071cb144c7a 226 }
JMF 12:0071cb144c7a 227 #endif /* MBEDTLS_TIMING_C */
JMF 12:0071cb144c7a 228
JMF 12:0071cb144c7a 229 #if defined(MBEDTLS_HAVEGE_C)
JMF 12:0071cb144c7a 230 int mbedtls_havege_poll( void *data,
JMF 12:0071cb144c7a 231 unsigned char *output, size_t len, size_t *olen )
JMF 12:0071cb144c7a 232 {
JMF 12:0071cb144c7a 233 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
JMF 12:0071cb144c7a 234 *olen = 0;
JMF 12:0071cb144c7a 235
JMF 12:0071cb144c7a 236 if( mbedtls_havege_random( hs, output, len ) != 0 )
JMF 12:0071cb144c7a 237 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 238
JMF 12:0071cb144c7a 239 *olen = len;
JMF 12:0071cb144c7a 240
JMF 12:0071cb144c7a 241 return( 0 );
JMF 12:0071cb144c7a 242 }
JMF 12:0071cb144c7a 243 #endif /* MBEDTLS_HAVEGE_C */
JMF 12:0071cb144c7a 244
JMF 12:0071cb144c7a 245 #if defined(MBEDTLS_ENTROPY_NV_SEED)
JMF 12:0071cb144c7a 246 int mbedtls_nv_seed_poll( void *data,
JMF 12:0071cb144c7a 247 unsigned char *output, size_t len, size_t *olen )
JMF 12:0071cb144c7a 248 {
JMF 12:0071cb144c7a 249 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
JMF 12:0071cb144c7a 250 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
JMF 12:0071cb144c7a 251 ((void) data);
JMF 12:0071cb144c7a 252
JMF 12:0071cb144c7a 253 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
JMF 12:0071cb144c7a 254
JMF 12:0071cb144c7a 255 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
JMF 12:0071cb144c7a 256 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 257
JMF 12:0071cb144c7a 258 if( len < use_len )
JMF 12:0071cb144c7a 259 use_len = len;
JMF 12:0071cb144c7a 260
JMF 12:0071cb144c7a 261 memcpy( output, buf, use_len );
JMF 12:0071cb144c7a 262 *olen = use_len;
JMF 12:0071cb144c7a 263
JMF 12:0071cb144c7a 264 return( 0 );
JMF 12:0071cb144c7a 265 }
JMF 12:0071cb144c7a 266 #endif /* MBEDTLS_ENTROPY_NV_SEED */
JMF 12:0071cb144c7a 267
JMF 12:0071cb144c7a 268 #endif /* MBEDTLS_ENTROPY_C */