Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*
switches 0:5c4d7b2438d3 2 * Entropy accumulator implementation
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
switches 0:5c4d7b2438d3 5 * SPDX-License-Identifier: Apache-2.0
switches 0:5c4d7b2438d3 6 *
switches 0:5c4d7b2438d3 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
switches 0:5c4d7b2438d3 8 * not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 9 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 10 *
switches 0:5c4d7b2438d3 11 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 12 *
switches 0:5c4d7b2438d3 13 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
switches 0:5c4d7b2438d3 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 16 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 17 * limitations under the License.
switches 0:5c4d7b2438d3 18 *
switches 0:5c4d7b2438d3 19 * This file is part of mbed TLS (https://tls.mbed.org)
switches 0:5c4d7b2438d3 20 */
switches 0:5c4d7b2438d3 21
switches 0:5c4d7b2438d3 22 #if !defined(MBEDTLS_CONFIG_FILE)
switches 0:5c4d7b2438d3 23 #include "mbedtls/config.h"
switches 0:5c4d7b2438d3 24 #else
switches 0:5c4d7b2438d3 25 #include MBEDTLS_CONFIG_FILE
switches 0:5c4d7b2438d3 26 #endif
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 #if defined(MBEDTLS_ENTROPY_C)
switches 0:5c4d7b2438d3 29
switches 0:5c4d7b2438d3 30 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
switches 0:5c4d7b2438d3 31 #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
switches 0:5c4d7b2438d3 32 #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
switches 0:5c4d7b2438d3 33 #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
switches 0:5c4d7b2438d3 34 #endif
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 #include "mbedtls/entropy.h"
switches 0:5c4d7b2438d3 37 #include "mbedtls/entropy_poll.h"
switches 0:5c4d7b2438d3 38
switches 0:5c4d7b2438d3 39 #include <string.h>
switches 0:5c4d7b2438d3 40
switches 0:5c4d7b2438d3 41 #if defined(MBEDTLS_FS_IO)
switches 0:5c4d7b2438d3 42 #include <stdio.h>
switches 0:5c4d7b2438d3 43 #endif
switches 0:5c4d7b2438d3 44
switches 0:5c4d7b2438d3 45 #if defined(MBEDTLS_ENTROPY_NV_SEED)
switches 0:5c4d7b2438d3 46 #include "mbedtls/platform.h"
switches 0:5c4d7b2438d3 47 #endif
switches 0:5c4d7b2438d3 48
switches 0:5c4d7b2438d3 49 #if defined(MBEDTLS_SELF_TEST)
switches 0:5c4d7b2438d3 50 #if defined(MBEDTLS_PLATFORM_C)
switches 0:5c4d7b2438d3 51 #include "mbedtls/platform.h"
switches 0:5c4d7b2438d3 52 #else
switches 0:5c4d7b2438d3 53 #include <stdio.h>
switches 0:5c4d7b2438d3 54 #define mbedtls_printf printf
switches 0:5c4d7b2438d3 55 #endif /* MBEDTLS_PLATFORM_C */
switches 0:5c4d7b2438d3 56 #endif /* MBEDTLS_SELF_TEST */
switches 0:5c4d7b2438d3 57
switches 0:5c4d7b2438d3 58 #if defined(MBEDTLS_HAVEGE_C)
switches 0:5c4d7b2438d3 59 #include "mbedtls/havege.h"
switches 0:5c4d7b2438d3 60 #endif
switches 0:5c4d7b2438d3 61
switches 0:5c4d7b2438d3 62 /* Implementation that should never be optimized out by the compiler */
switches 0:5c4d7b2438d3 63 static void mbedtls_zeroize( void *v, size_t n ) {
switches 0:5c4d7b2438d3 64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
switches 0:5c4d7b2438d3 65 }
switches 0:5c4d7b2438d3 66
switches 0:5c4d7b2438d3 67 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
switches 0:5c4d7b2438d3 68
switches 0:5c4d7b2438d3 69 void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
switches 0:5c4d7b2438d3 70 {
switches 0:5c4d7b2438d3 71 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
switches 0:5c4d7b2438d3 72
switches 0:5c4d7b2438d3 73 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 74 mbedtls_mutex_init( &ctx->mutex );
switches 0:5c4d7b2438d3 75 #endif
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
switches 0:5c4d7b2438d3 78 mbedtls_sha512_starts( &ctx->accumulator, 0 );
switches 0:5c4d7b2438d3 79 #else
switches 0:5c4d7b2438d3 80 mbedtls_sha256_starts( &ctx->accumulator, 0 );
switches 0:5c4d7b2438d3 81 #endif
switches 0:5c4d7b2438d3 82 #if defined(MBEDTLS_HAVEGE_C)
switches 0:5c4d7b2438d3 83 mbedtls_havege_init( &ctx->havege_data );
switches 0:5c4d7b2438d3 84 #endif
switches 0:5c4d7b2438d3 85
switches 0:5c4d7b2438d3 86 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
switches 0:5c4d7b2438d3 87 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
switches 0:5c4d7b2438d3 88 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
switches 0:5c4d7b2438d3 89 #endif
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
switches 0:5c4d7b2438d3 92 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
switches 0:5c4d7b2438d3 93 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
switches 0:5c4d7b2438d3 94 MBEDTLS_ENTROPY_MIN_PLATFORM,
switches 0:5c4d7b2438d3 95 MBEDTLS_ENTROPY_SOURCE_STRONG );
switches 0:5c4d7b2438d3 96 #endif
switches 0:5c4d7b2438d3 97 #if defined(MBEDTLS_TIMING_C)
switches 0:5c4d7b2438d3 98 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
switches 0:5c4d7b2438d3 99 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
switches 0:5c4d7b2438d3 100 MBEDTLS_ENTROPY_SOURCE_WEAK );
switches 0:5c4d7b2438d3 101 #endif
switches 0:5c4d7b2438d3 102 #if defined(MBEDTLS_HAVEGE_C)
switches 0:5c4d7b2438d3 103 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
switches 0:5c4d7b2438d3 104 MBEDTLS_ENTROPY_MIN_HAVEGE,
switches 0:5c4d7b2438d3 105 MBEDTLS_ENTROPY_SOURCE_STRONG );
switches 0:5c4d7b2438d3 106 #endif
switches 0:5c4d7b2438d3 107 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
switches 0:5c4d7b2438d3 108 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
switches 0:5c4d7b2438d3 109 MBEDTLS_ENTROPY_MIN_HARDWARE,
switches 0:5c4d7b2438d3 110 MBEDTLS_ENTROPY_SOURCE_STRONG );
switches 0:5c4d7b2438d3 111 #endif
switches 0:5c4d7b2438d3 112 #if defined(MBEDTLS_ENTROPY_NV_SEED)
switches 0:5c4d7b2438d3 113 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
switches 0:5c4d7b2438d3 114 MBEDTLS_ENTROPY_BLOCK_SIZE,
switches 0:5c4d7b2438d3 115 MBEDTLS_ENTROPY_SOURCE_STRONG );
switches 0:5c4d7b2438d3 116 #endif
switches 0:5c4d7b2438d3 117 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
switches 0:5c4d7b2438d3 118 }
switches 0:5c4d7b2438d3 119
switches 0:5c4d7b2438d3 120 void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
switches 0:5c4d7b2438d3 121 {
switches 0:5c4d7b2438d3 122 #if defined(MBEDTLS_HAVEGE_C)
switches 0:5c4d7b2438d3 123 mbedtls_havege_free( &ctx->havege_data );
switches 0:5c4d7b2438d3 124 #endif
switches 0:5c4d7b2438d3 125 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 126 mbedtls_mutex_free( &ctx->mutex );
switches 0:5c4d7b2438d3 127 #endif
switches 0:5c4d7b2438d3 128 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
switches 0:5c4d7b2438d3 129 }
switches 0:5c4d7b2438d3 130
switches 0:5c4d7b2438d3 131 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
switches 0:5c4d7b2438d3 132 mbedtls_entropy_f_source_ptr f_source, void *p_source,
switches 0:5c4d7b2438d3 133 size_t threshold, int strong )
switches 0:5c4d7b2438d3 134 {
switches 0:5c4d7b2438d3 135 int index, ret = 0;
switches 0:5c4d7b2438d3 136
switches 0:5c4d7b2438d3 137 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 138 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 139 return( ret );
switches 0:5c4d7b2438d3 140 #endif
switches 0:5c4d7b2438d3 141
switches 0:5c4d7b2438d3 142 index = ctx->source_count;
switches 0:5c4d7b2438d3 143 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
switches 0:5c4d7b2438d3 144 {
switches 0:5c4d7b2438d3 145 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
switches 0:5c4d7b2438d3 146 goto exit;
switches 0:5c4d7b2438d3 147 }
switches 0:5c4d7b2438d3 148
switches 0:5c4d7b2438d3 149 ctx->source[index].f_source = f_source;
switches 0:5c4d7b2438d3 150 ctx->source[index].p_source = p_source;
switches 0:5c4d7b2438d3 151 ctx->source[index].threshold = threshold;
switches 0:5c4d7b2438d3 152 ctx->source[index].strong = strong;
switches 0:5c4d7b2438d3 153
switches 0:5c4d7b2438d3 154 ctx->source_count++;
switches 0:5c4d7b2438d3 155
switches 0:5c4d7b2438d3 156 exit:
switches 0:5c4d7b2438d3 157 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 158 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 159 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 160 #endif
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 return( ret );
switches 0:5c4d7b2438d3 163 }
switches 0:5c4d7b2438d3 164
switches 0:5c4d7b2438d3 165 /*
switches 0:5c4d7b2438d3 166 * Entropy accumulator update
switches 0:5c4d7b2438d3 167 */
switches 0:5c4d7b2438d3 168 static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
switches 0:5c4d7b2438d3 169 const unsigned char *data, size_t len )
switches 0:5c4d7b2438d3 170 {
switches 0:5c4d7b2438d3 171 unsigned char header[2];
switches 0:5c4d7b2438d3 172 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
switches 0:5c4d7b2438d3 173 size_t use_len = len;
switches 0:5c4d7b2438d3 174 const unsigned char *p = data;
switches 0:5c4d7b2438d3 175
switches 0:5c4d7b2438d3 176 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
switches 0:5c4d7b2438d3 177 {
switches 0:5c4d7b2438d3 178 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
switches 0:5c4d7b2438d3 179 mbedtls_sha512( data, len, tmp, 0 );
switches 0:5c4d7b2438d3 180 #else
switches 0:5c4d7b2438d3 181 mbedtls_sha256( data, len, tmp, 0 );
switches 0:5c4d7b2438d3 182 #endif
switches 0:5c4d7b2438d3 183 p = tmp;
switches 0:5c4d7b2438d3 184 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
switches 0:5c4d7b2438d3 185 }
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 header[0] = source_id;
switches 0:5c4d7b2438d3 188 header[1] = use_len & 0xFF;
switches 0:5c4d7b2438d3 189
switches 0:5c4d7b2438d3 190 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
switches 0:5c4d7b2438d3 191 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
switches 0:5c4d7b2438d3 192 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
switches 0:5c4d7b2438d3 193 #else
switches 0:5c4d7b2438d3 194 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
switches 0:5c4d7b2438d3 195 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
switches 0:5c4d7b2438d3 196 #endif
switches 0:5c4d7b2438d3 197
switches 0:5c4d7b2438d3 198 return( 0 );
switches 0:5c4d7b2438d3 199 }
switches 0:5c4d7b2438d3 200
switches 0:5c4d7b2438d3 201 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
switches 0:5c4d7b2438d3 202 const unsigned char *data, size_t len )
switches 0:5c4d7b2438d3 203 {
switches 0:5c4d7b2438d3 204 int ret;
switches 0:5c4d7b2438d3 205
switches 0:5c4d7b2438d3 206 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 207 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 208 return( ret );
switches 0:5c4d7b2438d3 209 #endif
switches 0:5c4d7b2438d3 210
switches 0:5c4d7b2438d3 211 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
switches 0:5c4d7b2438d3 212
switches 0:5c4d7b2438d3 213 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 214 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 215 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 216 #endif
switches 0:5c4d7b2438d3 217
switches 0:5c4d7b2438d3 218 return( ret );
switches 0:5c4d7b2438d3 219 }
switches 0:5c4d7b2438d3 220
switches 0:5c4d7b2438d3 221 /*
switches 0:5c4d7b2438d3 222 * Run through the different sources to add entropy to our accumulator
switches 0:5c4d7b2438d3 223 */
switches 0:5c4d7b2438d3 224 static int entropy_gather_internal( mbedtls_entropy_context *ctx )
switches 0:5c4d7b2438d3 225 {
switches 0:5c4d7b2438d3 226 int ret, i, have_one_strong = 0;
switches 0:5c4d7b2438d3 227 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
switches 0:5c4d7b2438d3 228 size_t olen;
switches 0:5c4d7b2438d3 229
switches 0:5c4d7b2438d3 230 if( ctx->source_count == 0 )
switches 0:5c4d7b2438d3 231 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 /*
switches 0:5c4d7b2438d3 234 * Run through our entropy sources
switches 0:5c4d7b2438d3 235 */
switches 0:5c4d7b2438d3 236 for( i = 0; i < ctx->source_count; i++ )
switches 0:5c4d7b2438d3 237 {
switches 0:5c4d7b2438d3 238 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
switches 0:5c4d7b2438d3 239 have_one_strong = 1;
switches 0:5c4d7b2438d3 240
switches 0:5c4d7b2438d3 241 olen = 0;
switches 0:5c4d7b2438d3 242 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
switches 0:5c4d7b2438d3 243 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
switches 0:5c4d7b2438d3 244 {
switches 0:5c4d7b2438d3 245 return( ret );
switches 0:5c4d7b2438d3 246 }
switches 0:5c4d7b2438d3 247
switches 0:5c4d7b2438d3 248 /*
switches 0:5c4d7b2438d3 249 * Add if we actually gathered something
switches 0:5c4d7b2438d3 250 */
switches 0:5c4d7b2438d3 251 if( olen > 0 )
switches 0:5c4d7b2438d3 252 {
switches 0:5c4d7b2438d3 253 entropy_update( ctx, (unsigned char) i, buf, olen );
switches 0:5c4d7b2438d3 254 ctx->source[i].size += olen;
switches 0:5c4d7b2438d3 255 }
switches 0:5c4d7b2438d3 256 }
switches 0:5c4d7b2438d3 257
switches 0:5c4d7b2438d3 258 if( have_one_strong == 0 )
switches 0:5c4d7b2438d3 259 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
switches 0:5c4d7b2438d3 260
switches 0:5c4d7b2438d3 261 return( 0 );
switches 0:5c4d7b2438d3 262 }
switches 0:5c4d7b2438d3 263
switches 0:5c4d7b2438d3 264 /*
switches 0:5c4d7b2438d3 265 * Thread-safe wrapper for entropy_gather_internal()
switches 0:5c4d7b2438d3 266 */
switches 0:5c4d7b2438d3 267 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
switches 0:5c4d7b2438d3 268 {
switches 0:5c4d7b2438d3 269 int ret;
switches 0:5c4d7b2438d3 270
switches 0:5c4d7b2438d3 271 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 272 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 273 return( ret );
switches 0:5c4d7b2438d3 274 #endif
switches 0:5c4d7b2438d3 275
switches 0:5c4d7b2438d3 276 ret = entropy_gather_internal( ctx );
switches 0:5c4d7b2438d3 277
switches 0:5c4d7b2438d3 278 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 279 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 280 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 281 #endif
switches 0:5c4d7b2438d3 282
switches 0:5c4d7b2438d3 283 return( ret );
switches 0:5c4d7b2438d3 284 }
switches 0:5c4d7b2438d3 285
switches 0:5c4d7b2438d3 286 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
switches 0:5c4d7b2438d3 287 {
switches 0:5c4d7b2438d3 288 int ret, count = 0, i, done;
switches 0:5c4d7b2438d3 289 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
switches 0:5c4d7b2438d3 290 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
switches 0:5c4d7b2438d3 291
switches 0:5c4d7b2438d3 292 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
switches 0:5c4d7b2438d3 293 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
switches 0:5c4d7b2438d3 294
switches 0:5c4d7b2438d3 295 #if defined(MBEDTLS_ENTROPY_NV_SEED)
switches 0:5c4d7b2438d3 296 /* Update the NV entropy seed before generating any entropy for outside
switches 0:5c4d7b2438d3 297 * use.
switches 0:5c4d7b2438d3 298 */
switches 0:5c4d7b2438d3 299 if( ctx->initial_entropy_run == 0 )
switches 0:5c4d7b2438d3 300 {
switches 0:5c4d7b2438d3 301 ctx->initial_entropy_run = 1;
switches 0:5c4d7b2438d3 302 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
switches 0:5c4d7b2438d3 303 return( ret );
switches 0:5c4d7b2438d3 304 }
switches 0:5c4d7b2438d3 305 #endif
switches 0:5c4d7b2438d3 306
switches 0:5c4d7b2438d3 307 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 308 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 309 return( ret );
switches 0:5c4d7b2438d3 310 #endif
switches 0:5c4d7b2438d3 311
switches 0:5c4d7b2438d3 312 /*
switches 0:5c4d7b2438d3 313 * Always gather extra entropy before a call
switches 0:5c4d7b2438d3 314 */
switches 0:5c4d7b2438d3 315 do
switches 0:5c4d7b2438d3 316 {
switches 0:5c4d7b2438d3 317 if( count++ > ENTROPY_MAX_LOOP )
switches 0:5c4d7b2438d3 318 {
switches 0:5c4d7b2438d3 319 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
switches 0:5c4d7b2438d3 320 goto exit;
switches 0:5c4d7b2438d3 321 }
switches 0:5c4d7b2438d3 322
switches 0:5c4d7b2438d3 323 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
switches 0:5c4d7b2438d3 324 goto exit;
switches 0:5c4d7b2438d3 325
switches 0:5c4d7b2438d3 326 done = 1;
switches 0:5c4d7b2438d3 327 for( i = 0; i < ctx->source_count; i++ )
switches 0:5c4d7b2438d3 328 if( ctx->source[i].size < ctx->source[i].threshold )
switches 0:5c4d7b2438d3 329 done = 0;
switches 0:5c4d7b2438d3 330 }
switches 0:5c4d7b2438d3 331 while( ! done );
switches 0:5c4d7b2438d3 332
switches 0:5c4d7b2438d3 333 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
switches 0:5c4d7b2438d3 334
switches 0:5c4d7b2438d3 335 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
switches 0:5c4d7b2438d3 336 mbedtls_sha512_finish( &ctx->accumulator, buf );
switches 0:5c4d7b2438d3 337
switches 0:5c4d7b2438d3 338 /*
switches 0:5c4d7b2438d3 339 * Reset accumulator and counters and recycle existing entropy
switches 0:5c4d7b2438d3 340 */
switches 0:5c4d7b2438d3 341 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
switches 0:5c4d7b2438d3 342 mbedtls_sha512_starts( &ctx->accumulator, 0 );
switches 0:5c4d7b2438d3 343 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
switches 0:5c4d7b2438d3 344
switches 0:5c4d7b2438d3 345 /*
switches 0:5c4d7b2438d3 346 * Perform second SHA-512 on entropy
switches 0:5c4d7b2438d3 347 */
switches 0:5c4d7b2438d3 348 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
switches 0:5c4d7b2438d3 349 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
switches 0:5c4d7b2438d3 350 mbedtls_sha256_finish( &ctx->accumulator, buf );
switches 0:5c4d7b2438d3 351
switches 0:5c4d7b2438d3 352 /*
switches 0:5c4d7b2438d3 353 * Reset accumulator and counters and recycle existing entropy
switches 0:5c4d7b2438d3 354 */
switches 0:5c4d7b2438d3 355 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
switches 0:5c4d7b2438d3 356 mbedtls_sha256_starts( &ctx->accumulator, 0 );
switches 0:5c4d7b2438d3 357 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
switches 0:5c4d7b2438d3 358
switches 0:5c4d7b2438d3 359 /*
switches 0:5c4d7b2438d3 360 * Perform second SHA-256 on entropy
switches 0:5c4d7b2438d3 361 */
switches 0:5c4d7b2438d3 362 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
switches 0:5c4d7b2438d3 363 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
switches 0:5c4d7b2438d3 364
switches 0:5c4d7b2438d3 365 for( i = 0; i < ctx->source_count; i++ )
switches 0:5c4d7b2438d3 366 ctx->source[i].size = 0;
switches 0:5c4d7b2438d3 367
switches 0:5c4d7b2438d3 368 memcpy( output, buf, len );
switches 0:5c4d7b2438d3 369
switches 0:5c4d7b2438d3 370 ret = 0;
switches 0:5c4d7b2438d3 371
switches 0:5c4d7b2438d3 372 exit:
switches 0:5c4d7b2438d3 373 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 374 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 375 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 376 #endif
switches 0:5c4d7b2438d3 377
switches 0:5c4d7b2438d3 378 return( ret );
switches 0:5c4d7b2438d3 379 }
switches 0:5c4d7b2438d3 380
switches 0:5c4d7b2438d3 381 #if defined(MBEDTLS_ENTROPY_NV_SEED)
switches 0:5c4d7b2438d3 382 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
switches 0:5c4d7b2438d3 383 {
switches 0:5c4d7b2438d3 384 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
switches 0:5c4d7b2438d3 385 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
switches 0:5c4d7b2438d3 386
switches 0:5c4d7b2438d3 387 /* Read new seed and write it to NV */
switches 0:5c4d7b2438d3 388 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
switches 0:5c4d7b2438d3 389 return( ret );
switches 0:5c4d7b2438d3 390
switches 0:5c4d7b2438d3 391 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
switches 0:5c4d7b2438d3 392 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
switches 0:5c4d7b2438d3 393
switches 0:5c4d7b2438d3 394 /* Manually update the remaining stream with a separator value to diverge */
switches 0:5c4d7b2438d3 395 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
switches 0:5c4d7b2438d3 396 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
switches 0:5c4d7b2438d3 397
switches 0:5c4d7b2438d3 398 return( 0 );
switches 0:5c4d7b2438d3 399 }
switches 0:5c4d7b2438d3 400 #endif /* MBEDTLS_ENTROPY_NV_SEED */
switches 0:5c4d7b2438d3 401
switches 0:5c4d7b2438d3 402 #if defined(MBEDTLS_FS_IO)
switches 0:5c4d7b2438d3 403 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
switches 0:5c4d7b2438d3 404 {
switches 0:5c4d7b2438d3 405 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
switches 0:5c4d7b2438d3 406 FILE *f;
switches 0:5c4d7b2438d3 407 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
switches 0:5c4d7b2438d3 408
switches 0:5c4d7b2438d3 409 if( ( f = fopen( path, "wb" ) ) == NULL )
switches 0:5c4d7b2438d3 410 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
switches 0:5c4d7b2438d3 411
switches 0:5c4d7b2438d3 412 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
switches 0:5c4d7b2438d3 413 goto exit;
switches 0:5c4d7b2438d3 414
switches 0:5c4d7b2438d3 415 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
switches 0:5c4d7b2438d3 416 {
switches 0:5c4d7b2438d3 417 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
switches 0:5c4d7b2438d3 418 goto exit;
switches 0:5c4d7b2438d3 419 }
switches 0:5c4d7b2438d3 420
switches 0:5c4d7b2438d3 421 ret = 0;
switches 0:5c4d7b2438d3 422
switches 0:5c4d7b2438d3 423 exit:
switches 0:5c4d7b2438d3 424 fclose( f );
switches 0:5c4d7b2438d3 425 return( ret );
switches 0:5c4d7b2438d3 426 }
switches 0:5c4d7b2438d3 427
switches 0:5c4d7b2438d3 428 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
switches 0:5c4d7b2438d3 429 {
switches 0:5c4d7b2438d3 430 FILE *f;
switches 0:5c4d7b2438d3 431 size_t n;
switches 0:5c4d7b2438d3 432 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
switches 0:5c4d7b2438d3 433
switches 0:5c4d7b2438d3 434 if( ( f = fopen( path, "rb" ) ) == NULL )
switches 0:5c4d7b2438d3 435 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
switches 0:5c4d7b2438d3 436
switches 0:5c4d7b2438d3 437 fseek( f, 0, SEEK_END );
switches 0:5c4d7b2438d3 438 n = (size_t) ftell( f );
switches 0:5c4d7b2438d3 439 fseek( f, 0, SEEK_SET );
switches 0:5c4d7b2438d3 440
switches 0:5c4d7b2438d3 441 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
switches 0:5c4d7b2438d3 442 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
switches 0:5c4d7b2438d3 443
switches 0:5c4d7b2438d3 444 if( fread( buf, 1, n, f ) != n )
switches 0:5c4d7b2438d3 445 {
switches 0:5c4d7b2438d3 446 fclose( f );
switches 0:5c4d7b2438d3 447 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
switches 0:5c4d7b2438d3 448 }
switches 0:5c4d7b2438d3 449
switches 0:5c4d7b2438d3 450 fclose( f );
switches 0:5c4d7b2438d3 451
switches 0:5c4d7b2438d3 452 mbedtls_entropy_update_manual( ctx, buf, n );
switches 0:5c4d7b2438d3 453
switches 0:5c4d7b2438d3 454 return( mbedtls_entropy_write_seed_file( ctx, path ) );
switches 0:5c4d7b2438d3 455 }
switches 0:5c4d7b2438d3 456 #endif /* MBEDTLS_FS_IO */
switches 0:5c4d7b2438d3 457
switches 0:5c4d7b2438d3 458 #if defined(MBEDTLS_SELF_TEST)
switches 0:5c4d7b2438d3 459 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
switches 0:5c4d7b2438d3 460 /*
switches 0:5c4d7b2438d3 461 * Dummy source function
switches 0:5c4d7b2438d3 462 */
switches 0:5c4d7b2438d3 463 static int entropy_dummy_source( void *data, unsigned char *output,
switches 0:5c4d7b2438d3 464 size_t len, size_t *olen )
switches 0:5c4d7b2438d3 465 {
switches 0:5c4d7b2438d3 466 ((void) data);
switches 0:5c4d7b2438d3 467
switches 0:5c4d7b2438d3 468 memset( output, 0x2a, len );
switches 0:5c4d7b2438d3 469 *olen = len;
switches 0:5c4d7b2438d3 470
switches 0:5c4d7b2438d3 471 return( 0 );
switches 0:5c4d7b2438d3 472 }
switches 0:5c4d7b2438d3 473 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
switches 0:5c4d7b2438d3 474
switches 0:5c4d7b2438d3 475 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
switches 0:5c4d7b2438d3 476
switches 0:5c4d7b2438d3 477 static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
switches 0:5c4d7b2438d3 478 {
switches 0:5c4d7b2438d3 479 int ret = 0;
switches 0:5c4d7b2438d3 480 size_t entropy_len = 0;
switches 0:5c4d7b2438d3 481 size_t olen = 0;
switches 0:5c4d7b2438d3 482 size_t attempts = buf_len;
switches 0:5c4d7b2438d3 483
switches 0:5c4d7b2438d3 484 while( attempts > 0 && entropy_len < buf_len )
switches 0:5c4d7b2438d3 485 {
switches 0:5c4d7b2438d3 486 if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
switches 0:5c4d7b2438d3 487 buf_len - entropy_len, &olen ) ) != 0 )
switches 0:5c4d7b2438d3 488 return( ret );
switches 0:5c4d7b2438d3 489
switches 0:5c4d7b2438d3 490 entropy_len += olen;
switches 0:5c4d7b2438d3 491 attempts--;
switches 0:5c4d7b2438d3 492 }
switches 0:5c4d7b2438d3 493
switches 0:5c4d7b2438d3 494 if( entropy_len < buf_len )
switches 0:5c4d7b2438d3 495 {
switches 0:5c4d7b2438d3 496 ret = 1;
switches 0:5c4d7b2438d3 497 }
switches 0:5c4d7b2438d3 498
switches 0:5c4d7b2438d3 499 return( ret );
switches 0:5c4d7b2438d3 500 }
switches 0:5c4d7b2438d3 501
switches 0:5c4d7b2438d3 502
switches 0:5c4d7b2438d3 503 static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
switches 0:5c4d7b2438d3 504 size_t buf_len )
switches 0:5c4d7b2438d3 505 {
switches 0:5c4d7b2438d3 506 unsigned char set= 0xFF;
switches 0:5c4d7b2438d3 507 unsigned char unset = 0x00;
switches 0:5c4d7b2438d3 508 size_t i;
switches 0:5c4d7b2438d3 509
switches 0:5c4d7b2438d3 510 for( i = 0; i < buf_len; i++ )
switches 0:5c4d7b2438d3 511 {
switches 0:5c4d7b2438d3 512 set &= buf[i];
switches 0:5c4d7b2438d3 513 unset |= buf[i];
switches 0:5c4d7b2438d3 514 }
switches 0:5c4d7b2438d3 515
switches 0:5c4d7b2438d3 516 return( set == 0xFF || unset == 0x00 );
switches 0:5c4d7b2438d3 517 }
switches 0:5c4d7b2438d3 518
switches 0:5c4d7b2438d3 519 /*
switches 0:5c4d7b2438d3 520 * A test to ensure hat the entropy sources are functioning correctly
switches 0:5c4d7b2438d3 521 * and there is no obvious failure. The test performs the following checks:
switches 0:5c4d7b2438d3 522 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
switches 0:5c4d7b2438d3 523 * bits set).
switches 0:5c4d7b2438d3 524 * - The entropy source is not providing values in a pattern. Because the
switches 0:5c4d7b2438d3 525 * hardware could be providing data in an arbitrary length, this check polls
switches 0:5c4d7b2438d3 526 * the hardware entropy source twice and compares the result to ensure they
switches 0:5c4d7b2438d3 527 * are not equal.
switches 0:5c4d7b2438d3 528 * - The error code returned by the entropy source is not an error.
switches 0:5c4d7b2438d3 529 */
switches 0:5c4d7b2438d3 530 int mbedtls_entropy_source_self_test( int verbose )
switches 0:5c4d7b2438d3 531 {
switches 0:5c4d7b2438d3 532 int ret = 0;
switches 0:5c4d7b2438d3 533 unsigned char buf0[2 * sizeof( unsigned long long int )];
switches 0:5c4d7b2438d3 534 unsigned char buf1[2 * sizeof( unsigned long long int )];
switches 0:5c4d7b2438d3 535
switches 0:5c4d7b2438d3 536 if( verbose != 0 )
switches 0:5c4d7b2438d3 537 mbedtls_printf( " ENTROPY_BIAS test: " );
switches 0:5c4d7b2438d3 538
switches 0:5c4d7b2438d3 539 memset( buf0, 0x00, sizeof( buf0 ) );
switches 0:5c4d7b2438d3 540 memset( buf1, 0x00, sizeof( buf1 ) );
switches 0:5c4d7b2438d3 541
switches 0:5c4d7b2438d3 542 if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
switches 0:5c4d7b2438d3 543 goto cleanup;
switches 0:5c4d7b2438d3 544 if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
switches 0:5c4d7b2438d3 545 goto cleanup;
switches 0:5c4d7b2438d3 546
switches 0:5c4d7b2438d3 547 /* Make sure that the returned values are not all 0 or 1 */
switches 0:5c4d7b2438d3 548 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
switches 0:5c4d7b2438d3 549 goto cleanup;
switches 0:5c4d7b2438d3 550 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
switches 0:5c4d7b2438d3 551 goto cleanup;
switches 0:5c4d7b2438d3 552
switches 0:5c4d7b2438d3 553 /* Make sure that the entropy source is not returning values in a
switches 0:5c4d7b2438d3 554 * pattern */
switches 0:5c4d7b2438d3 555 ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
switches 0:5c4d7b2438d3 556
switches 0:5c4d7b2438d3 557 cleanup:
switches 0:5c4d7b2438d3 558 if( verbose != 0 )
switches 0:5c4d7b2438d3 559 {
switches 0:5c4d7b2438d3 560 if( ret != 0 )
switches 0:5c4d7b2438d3 561 mbedtls_printf( "failed\n" );
switches 0:5c4d7b2438d3 562 else
switches 0:5c4d7b2438d3 563 mbedtls_printf( "passed\n" );
switches 0:5c4d7b2438d3 564
switches 0:5c4d7b2438d3 565 mbedtls_printf( "\n" );
switches 0:5c4d7b2438d3 566 }
switches 0:5c4d7b2438d3 567
switches 0:5c4d7b2438d3 568 return( ret != 0 );
switches 0:5c4d7b2438d3 569 }
switches 0:5c4d7b2438d3 570
switches 0:5c4d7b2438d3 571 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
switches 0:5c4d7b2438d3 572
switches 0:5c4d7b2438d3 573 /*
switches 0:5c4d7b2438d3 574 * The actual entropy quality is hard to test, but we can at least
switches 0:5c4d7b2438d3 575 * test that the functions don't cause errors and write the correct
switches 0:5c4d7b2438d3 576 * amount of data to buffers.
switches 0:5c4d7b2438d3 577 */
switches 0:5c4d7b2438d3 578 int mbedtls_entropy_self_test( int verbose )
switches 0:5c4d7b2438d3 579 {
switches 0:5c4d7b2438d3 580 int ret = 1;
switches 0:5c4d7b2438d3 581 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
switches 0:5c4d7b2438d3 582 mbedtls_entropy_context ctx;
switches 0:5c4d7b2438d3 583 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
switches 0:5c4d7b2438d3 584 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
switches 0:5c4d7b2438d3 585 size_t i, j;
switches 0:5c4d7b2438d3 586 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
switches 0:5c4d7b2438d3 587
switches 0:5c4d7b2438d3 588 if( verbose != 0 )
switches 0:5c4d7b2438d3 589 mbedtls_printf( " ENTROPY test: " );
switches 0:5c4d7b2438d3 590
switches 0:5c4d7b2438d3 591 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
switches 0:5c4d7b2438d3 592 mbedtls_entropy_init( &ctx );
switches 0:5c4d7b2438d3 593
switches 0:5c4d7b2438d3 594 /* First do a gather to make sure we have default sources */
switches 0:5c4d7b2438d3 595 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
switches 0:5c4d7b2438d3 596 goto cleanup;
switches 0:5c4d7b2438d3 597
switches 0:5c4d7b2438d3 598 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
switches 0:5c4d7b2438d3 599 MBEDTLS_ENTROPY_SOURCE_WEAK );
switches 0:5c4d7b2438d3 600 if( ret != 0 )
switches 0:5c4d7b2438d3 601 goto cleanup;
switches 0:5c4d7b2438d3 602
switches 0:5c4d7b2438d3 603 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
switches 0:5c4d7b2438d3 604 goto cleanup;
switches 0:5c4d7b2438d3 605
switches 0:5c4d7b2438d3 606 /*
switches 0:5c4d7b2438d3 607 * To test that mbedtls_entropy_func writes correct number of bytes:
switches 0:5c4d7b2438d3 608 * - use the whole buffer and rely on ASan to detect overruns
switches 0:5c4d7b2438d3 609 * - collect entropy 8 times and OR the result in an accumulator:
switches 0:5c4d7b2438d3 610 * any byte should then be 0 with probably 2^(-64), so requiring
switches 0:5c4d7b2438d3 611 * each of the 32 or 64 bytes to be non-zero has a false failure rate
switches 0:5c4d7b2438d3 612 * of at most 2^(-58) which is acceptable.
switches 0:5c4d7b2438d3 613 */
switches 0:5c4d7b2438d3 614 for( i = 0; i < 8; i++ )
switches 0:5c4d7b2438d3 615 {
switches 0:5c4d7b2438d3 616 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
switches 0:5c4d7b2438d3 617 goto cleanup;
switches 0:5c4d7b2438d3 618
switches 0:5c4d7b2438d3 619 for( j = 0; j < sizeof( buf ); j++ )
switches 0:5c4d7b2438d3 620 acc[j] |= buf[j];
switches 0:5c4d7b2438d3 621 }
switches 0:5c4d7b2438d3 622
switches 0:5c4d7b2438d3 623 for( j = 0; j < sizeof( buf ); j++ )
switches 0:5c4d7b2438d3 624 {
switches 0:5c4d7b2438d3 625 if( acc[j] == 0 )
switches 0:5c4d7b2438d3 626 {
switches 0:5c4d7b2438d3 627 ret = 1;
switches 0:5c4d7b2438d3 628 goto cleanup;
switches 0:5c4d7b2438d3 629 }
switches 0:5c4d7b2438d3 630 }
switches 0:5c4d7b2438d3 631
switches 0:5c4d7b2438d3 632 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
switches 0:5c4d7b2438d3 633 if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
switches 0:5c4d7b2438d3 634 goto cleanup;
switches 0:5c4d7b2438d3 635 #endif
switches 0:5c4d7b2438d3 636
switches 0:5c4d7b2438d3 637 cleanup:
switches 0:5c4d7b2438d3 638 mbedtls_entropy_free( &ctx );
switches 0:5c4d7b2438d3 639 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
switches 0:5c4d7b2438d3 640
switches 0:5c4d7b2438d3 641 if( verbose != 0 )
switches 0:5c4d7b2438d3 642 {
switches 0:5c4d7b2438d3 643 if( ret != 0 )
switches 0:5c4d7b2438d3 644 mbedtls_printf( "failed\n" );
switches 0:5c4d7b2438d3 645 else
switches 0:5c4d7b2438d3 646 mbedtls_printf( "passed\n" );
switches 0:5c4d7b2438d3 647
switches 0:5c4d7b2438d3 648 mbedtls_printf( "\n" );
switches 0:5c4d7b2438d3 649 }
switches 0:5c4d7b2438d3 650
switches 0:5c4d7b2438d3 651 return( ret != 0 );
switches 0:5c4d7b2438d3 652 }
switches 0:5c4d7b2438d3 653 #endif /* MBEDTLS_SELF_TEST */
switches 0:5c4d7b2438d3 654
switches 0:5c4d7b2438d3 655 #endif /* MBEDTLS_ENTROPY_C */