Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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