Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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