Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers entropy_poll.c Source File

entropy_poll.c

00001 /*
00002  *  Platform-specific and custom entropy polling functions
00003  *
00004  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 #if defined(__linux__)
00023 /* Ensure that syscall() is available even when compiling with -std=c99 */
00024 #define _GNU_SOURCE
00025 #endif
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "mbedtls/config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #include <string.h>
00034 
00035 #if defined(MBEDTLS_ENTROPY_C)
00036 
00037 #include "mbedtls/entropy.h"
00038 #include "mbedtls/entropy_poll.h"
00039 
00040 #if defined(MBEDTLS_TIMING_C)
00041 #include "mbedtls/timing.h"
00042 #endif
00043 #if defined(MBEDTLS_HAVEGE_C)
00044 #include "mbedtls/havege.h"
00045 #endif
00046 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00047 #include "mbedtls/platform.h"
00048 #endif
00049 
00050 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
00051 
00052 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
00053     !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
00054     !defined(__HAIKU__)
00055 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
00056 #endif
00057 
00058 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
00059 
00060 #if !defined(_WIN32_WINNT)
00061 #define _WIN32_WINNT 0x0400
00062 #endif
00063 #include <windows.h>
00064 #include <wincrypt.h>
00065 
00066 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
00067                            size_t *olen )
00068 {
00069     HCRYPTPROV provider;
00070     ((void) data);
00071     *olen = 0;
00072 
00073     if( CryptAcquireContext( &provider, NULL, NULL,
00074                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
00075     {
00076         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00077     }
00078 
00079     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
00080     {
00081         CryptReleaseContext( provider, 0 );
00082         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00083     }
00084 
00085     CryptReleaseContext( provider, 0 );
00086     *olen = len;
00087 
00088     return( 0 );
00089 }
00090 #else /* _WIN32 && !EFIX64 && !EFI32 */
00091 
00092 /*
00093  * Test for Linux getrandom() support.
00094  * Since there is no wrapper in the libc yet, use the generic syscall wrapper
00095  * available in GNU libc and compatible libc's (eg uClibc).
00096  */
00097 #if defined(__linux__) && defined(__GLIBC__)
00098 #include <unistd.h>
00099 #include <sys/syscall.h>
00100 #if defined(SYS_getrandom)
00101 #define HAVE_GETRANDOM
00102 #include <errno.h>
00103 
00104 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
00105 {
00106     /* MemSan cannot understand that the syscall writes to the buffer */
00107 #if defined(__has_feature)
00108 #if __has_feature(memory_sanitizer)
00109     memset( buf, 0, buflen );
00110 #endif
00111 #endif
00112     return( syscall( SYS_getrandom, buf, buflen, flags ) );
00113 }
00114 #endif /* SYS_getrandom */
00115 #endif /* __linux__ */
00116 
00117 #include <stdio.h>
00118 
00119 int mbedtls_platform_entropy_poll( void *data,
00120                            unsigned char *output, size_t len, size_t *olen )
00121 {
00122     FILE *file;
00123     size_t read_len;
00124     int ret;
00125     ((void) data);
00126 
00127 #if defined(HAVE_GETRANDOM)
00128     ret = getrandom_wrapper( output, len, 0 );
00129     if( ret >= 0 )
00130     {
00131         *olen = ret;
00132         return( 0 );
00133     }
00134     else if( errno != ENOSYS )
00135         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00136     /* Fall through if the system call isn't known. */
00137 #else
00138     ((void) ret);
00139 #endif /* HAVE_GETRANDOM */
00140 
00141     *olen = 0;
00142 
00143     file = fopen( "/dev/urandom", "rb" );
00144     if( file == NULL )
00145         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00146 
00147     read_len = fread( output, 1, len, file );
00148     if( read_len != len )
00149     {
00150         fclose( file );
00151         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00152     }
00153 
00154     fclose( file );
00155     *olen = len;
00156 
00157     return( 0 );
00158 }
00159 #endif /* _WIN32 && !EFIX64 && !EFI32 */
00160 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
00161 
00162 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
00163 int mbedtls_null_entropy_poll( void *data,
00164                     unsigned char *output, size_t len, size_t *olen )
00165 {
00166     ((void) data);
00167     ((void) output);
00168     *olen = 0;
00169 
00170     if( len < sizeof(unsigned char) )
00171         return( 0 );
00172 
00173     *olen = sizeof(unsigned char);
00174 
00175     return( 0 );
00176 }
00177 #endif
00178 
00179 #if defined(MBEDTLS_TIMING_C)
00180 int mbedtls_hardclock_poll( void *data,
00181                     unsigned char *output, size_t len, size_t *olen )
00182 {
00183     unsigned long timer = mbedtls_timing_hardclock();
00184     ((void) data);
00185     *olen = 0;
00186 
00187     if( len < sizeof(unsigned long) )
00188         return( 0 );
00189 
00190     memcpy( output, &timer, sizeof(unsigned long) );
00191     *olen = sizeof(unsigned long);
00192 
00193     return( 0 );
00194 }
00195 #endif /* MBEDTLS_TIMING_C */
00196 
00197 #if defined(MBEDTLS_HAVEGE_C)
00198 int mbedtls_havege_poll( void *data,
00199                  unsigned char *output, size_t len, size_t *olen )
00200 {
00201     mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
00202     *olen = 0;
00203 
00204     if( mbedtls_havege_random( hs, output, len ) != 0 )
00205         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00206 
00207     *olen = len;
00208 
00209     return( 0 );
00210 }
00211 #endif /* MBEDTLS_HAVEGE_C */
00212 
00213 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00214 int mbedtls_nv_seed_poll( void *data,
00215                           unsigned char *output, size_t len, size_t *olen )
00216 {
00217     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
00218     size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
00219     ((void) data);
00220 
00221     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
00222 
00223     if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
00224       return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
00225 
00226     if( len < use_len )
00227       use_len = len;
00228 
00229     memcpy( output, buf, use_len );
00230     *olen = use_len;
00231 
00232     return( 0 );
00233 }
00234 #endif /* MBEDTLS_ENTROPY_NV_SEED */
00235 
00236 #endif /* MBEDTLS_ENTROPY_C */