Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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