Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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