Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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