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