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