Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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