mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:24750b9ad5ef 1 /*
Christopher Haster 1:24750b9ad5ef 2 * Entropy accumulator implementation
Christopher Haster 1:24750b9ad5ef 3 *
Christopher Haster 1:24750b9ad5ef 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Christopher Haster 1:24750b9ad5ef 5 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:24750b9ad5ef 6 *
Christopher Haster 1:24750b9ad5ef 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Christopher Haster 1:24750b9ad5ef 8 * not use this file except in compliance with the License.
Christopher Haster 1:24750b9ad5ef 9 * You may obtain a copy of the License at
Christopher Haster 1:24750b9ad5ef 10 *
Christopher Haster 1:24750b9ad5ef 11 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:24750b9ad5ef 12 *
Christopher Haster 1:24750b9ad5ef 13 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:24750b9ad5ef 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Christopher Haster 1:24750b9ad5ef 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:24750b9ad5ef 16 * See the License for the specific language governing permissions and
Christopher Haster 1:24750b9ad5ef 17 * limitations under the License.
Christopher Haster 1:24750b9ad5ef 18 *
Christopher Haster 1:24750b9ad5ef 19 * This file is part of mbed TLS (https://tls.mbed.org)
Christopher Haster 1:24750b9ad5ef 20 */
Christopher Haster 1:24750b9ad5ef 21
Christopher Haster 1:24750b9ad5ef 22 #if !defined(MBEDTLS_CONFIG_FILE)
Christopher Haster 1:24750b9ad5ef 23 #include "mbedtls/config.h"
Christopher Haster 1:24750b9ad5ef 24 #else
Christopher Haster 1:24750b9ad5ef 25 #include MBEDTLS_CONFIG_FILE
Christopher Haster 1:24750b9ad5ef 26 #endif
Christopher Haster 1:24750b9ad5ef 27
Christopher Haster 1:24750b9ad5ef 28 #if defined(MBEDTLS_ENTROPY_C)
Christopher Haster 1:24750b9ad5ef 29
Christopher Haster 1:24750b9ad5ef 30 #include "mbedtls/entropy.h"
Christopher Haster 1:24750b9ad5ef 31 #include "mbedtls/entropy_poll.h"
Christopher Haster 1:24750b9ad5ef 32
Christopher Haster 1:24750b9ad5ef 33 #include <string.h>
Christopher Haster 1:24750b9ad5ef 34
Christopher Haster 1:24750b9ad5ef 35 #if defined(MBEDTLS_FS_IO)
Christopher Haster 1:24750b9ad5ef 36 #include <stdio.h>
Christopher Haster 1:24750b9ad5ef 37 #endif
Christopher Haster 1:24750b9ad5ef 38
Christopher Haster 1:24750b9ad5ef 39 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 40 #if defined(MBEDTLS_PLATFORM_C)
Christopher Haster 1:24750b9ad5ef 41 #include "mbedtls/platform.h"
Christopher Haster 1:24750b9ad5ef 42 #else
Christopher Haster 1:24750b9ad5ef 43 #include <stdio.h>
Christopher Haster 1:24750b9ad5ef 44 #define mbedtls_printf printf
Christopher Haster 1:24750b9ad5ef 45 #endif /* MBEDTLS_PLATFORM_C */
Christopher Haster 1:24750b9ad5ef 46 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 47
Christopher Haster 1:24750b9ad5ef 48 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 49 #include "mbedtls/havege.h"
Christopher Haster 1:24750b9ad5ef 50 #endif
Christopher Haster 1:24750b9ad5ef 51
Christopher Haster 1:24750b9ad5ef 52 /* Implementation that should never be optimized out by the compiler */
Christopher Haster 1:24750b9ad5ef 53 static void mbedtls_zeroize( void *v, size_t n ) {
Christopher Haster 1:24750b9ad5ef 54 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Christopher Haster 1:24750b9ad5ef 55 }
Christopher Haster 1:24750b9ad5ef 56
Christopher Haster 1:24750b9ad5ef 57 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
Christopher Haster 1:24750b9ad5ef 58
Christopher Haster 1:24750b9ad5ef 59 void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
Christopher Haster 1:24750b9ad5ef 60 {
Christopher Haster 1:24750b9ad5ef 61 memset( ctx, 0, sizeof(mbedtls_entropy_context) );
Christopher Haster 1:24750b9ad5ef 62
Christopher Haster 1:24750b9ad5ef 63 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 64 mbedtls_mutex_init( &ctx->mutex );
Christopher Haster 1:24750b9ad5ef 65 #endif
Christopher Haster 1:24750b9ad5ef 66
Christopher Haster 1:24750b9ad5ef 67 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 68 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Christopher Haster 1:24750b9ad5ef 69 #else
Christopher Haster 1:24750b9ad5ef 70 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Christopher Haster 1:24750b9ad5ef 71 #endif
Christopher Haster 1:24750b9ad5ef 72 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 73 mbedtls_havege_init( &ctx->havege_data );
Christopher Haster 1:24750b9ad5ef 74 #endif
Christopher Haster 1:24750b9ad5ef 75
Christopher Haster 1:24750b9ad5ef 76 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
Christopher Haster 1:24750b9ad5ef 77 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Christopher Haster 1:24750b9ad5ef 78 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
Christopher Haster 1:24750b9ad5ef 79 MBEDTLS_ENTROPY_MIN_PLATFORM,
Christopher Haster 1:24750b9ad5ef 80 MBEDTLS_ENTROPY_SOURCE_STRONG );
Christopher Haster 1:24750b9ad5ef 81 #endif
Christopher Haster 1:24750b9ad5ef 82 #if defined(MBEDTLS_TIMING_C)
Christopher Haster 1:24750b9ad5ef 83 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
Christopher Haster 1:24750b9ad5ef 84 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
Christopher Haster 1:24750b9ad5ef 85 MBEDTLS_ENTROPY_SOURCE_WEAK );
Christopher Haster 1:24750b9ad5ef 86 #endif
Christopher Haster 1:24750b9ad5ef 87 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 88 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
Christopher Haster 1:24750b9ad5ef 89 MBEDTLS_ENTROPY_MIN_HAVEGE,
Christopher Haster 1:24750b9ad5ef 90 MBEDTLS_ENTROPY_SOURCE_STRONG );
Christopher Haster 1:24750b9ad5ef 91 #endif
Christopher Haster 1:24750b9ad5ef 92 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Christopher Haster 1:24750b9ad5ef 93 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
Christopher Haster 1:24750b9ad5ef 94 MBEDTLS_ENTROPY_MIN_HARDWARE,
Christopher Haster 1:24750b9ad5ef 95 MBEDTLS_ENTROPY_SOURCE_STRONG );
Christopher Haster 1:24750b9ad5ef 96 #endif
Christopher Haster 1:24750b9ad5ef 97 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
Christopher Haster 1:24750b9ad5ef 98 }
Christopher Haster 1:24750b9ad5ef 99
Christopher Haster 1:24750b9ad5ef 100 void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
Christopher Haster 1:24750b9ad5ef 101 {
Christopher Haster 1:24750b9ad5ef 102 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 103 mbedtls_havege_free( &ctx->havege_data );
Christopher Haster 1:24750b9ad5ef 104 #endif
Christopher Haster 1:24750b9ad5ef 105 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 106 mbedtls_mutex_free( &ctx->mutex );
Christopher Haster 1:24750b9ad5ef 107 #endif
Christopher Haster 1:24750b9ad5ef 108 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) );
Christopher Haster 1:24750b9ad5ef 109 }
Christopher Haster 1:24750b9ad5ef 110
Christopher Haster 1:24750b9ad5ef 111 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
Christopher Haster 1:24750b9ad5ef 112 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Christopher Haster 1:24750b9ad5ef 113 size_t threshold, int strong )
Christopher Haster 1:24750b9ad5ef 114 {
Christopher Haster 1:24750b9ad5ef 115 int index, ret = 0;
Christopher Haster 1:24750b9ad5ef 116
Christopher Haster 1:24750b9ad5ef 117 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 118 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 119 return( ret );
Christopher Haster 1:24750b9ad5ef 120 #endif
Christopher Haster 1:24750b9ad5ef 121
Christopher Haster 1:24750b9ad5ef 122 index = ctx->source_count;
Christopher Haster 1:24750b9ad5ef 123 if( index >= MBEDTLS_ENTROPY_MAX_SOURCES )
Christopher Haster 1:24750b9ad5ef 124 {
Christopher Haster 1:24750b9ad5ef 125 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
Christopher Haster 1:24750b9ad5ef 126 goto exit;
Christopher Haster 1:24750b9ad5ef 127 }
Christopher Haster 1:24750b9ad5ef 128
Christopher Haster 1:24750b9ad5ef 129 ctx->source[index].f_source = f_source;
Christopher Haster 1:24750b9ad5ef 130 ctx->source[index].p_source = p_source;
Christopher Haster 1:24750b9ad5ef 131 ctx->source[index].threshold = threshold;
Christopher Haster 1:24750b9ad5ef 132 ctx->source[index].strong = strong;
Christopher Haster 1:24750b9ad5ef 133
Christopher Haster 1:24750b9ad5ef 134 ctx->source_count++;
Christopher Haster 1:24750b9ad5ef 135
Christopher Haster 1:24750b9ad5ef 136 exit:
Christopher Haster 1:24750b9ad5ef 137 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 138 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Christopher Haster 1:24750b9ad5ef 139 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Christopher Haster 1:24750b9ad5ef 140 #endif
Christopher Haster 1:24750b9ad5ef 141
Christopher Haster 1:24750b9ad5ef 142 return( ret );
Christopher Haster 1:24750b9ad5ef 143 }
Christopher Haster 1:24750b9ad5ef 144
Christopher Haster 1:24750b9ad5ef 145 /*
Christopher Haster 1:24750b9ad5ef 146 * Entropy accumulator update
Christopher Haster 1:24750b9ad5ef 147 */
Christopher Haster 1:24750b9ad5ef 148 static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
Christopher Haster 1:24750b9ad5ef 149 const unsigned char *data, size_t len )
Christopher Haster 1:24750b9ad5ef 150 {
Christopher Haster 1:24750b9ad5ef 151 unsigned char header[2];
Christopher Haster 1:24750b9ad5ef 152 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
Christopher Haster 1:24750b9ad5ef 153 size_t use_len = len;
Christopher Haster 1:24750b9ad5ef 154 const unsigned char *p = data;
Christopher Haster 1:24750b9ad5ef 155
Christopher Haster 1:24750b9ad5ef 156 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Christopher Haster 1:24750b9ad5ef 157 {
Christopher Haster 1:24750b9ad5ef 158 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 159 mbedtls_sha512( data, len, tmp, 0 );
Christopher Haster 1:24750b9ad5ef 160 #else
Christopher Haster 1:24750b9ad5ef 161 mbedtls_sha256( data, len, tmp, 0 );
Christopher Haster 1:24750b9ad5ef 162 #endif
Christopher Haster 1:24750b9ad5ef 163 p = tmp;
Christopher Haster 1:24750b9ad5ef 164 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Christopher Haster 1:24750b9ad5ef 165 }
Christopher Haster 1:24750b9ad5ef 166
Christopher Haster 1:24750b9ad5ef 167 header[0] = source_id;
Christopher Haster 1:24750b9ad5ef 168 header[1] = use_len & 0xFF;
Christopher Haster 1:24750b9ad5ef 169
Christopher Haster 1:24750b9ad5ef 170 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 171 mbedtls_sha512_update( &ctx->accumulator, header, 2 );
Christopher Haster 1:24750b9ad5ef 172 mbedtls_sha512_update( &ctx->accumulator, p, use_len );
Christopher Haster 1:24750b9ad5ef 173 #else
Christopher Haster 1:24750b9ad5ef 174 mbedtls_sha256_update( &ctx->accumulator, header, 2 );
Christopher Haster 1:24750b9ad5ef 175 mbedtls_sha256_update( &ctx->accumulator, p, use_len );
Christopher Haster 1:24750b9ad5ef 176 #endif
Christopher Haster 1:24750b9ad5ef 177
Christopher Haster 1:24750b9ad5ef 178 return( 0 );
Christopher Haster 1:24750b9ad5ef 179 }
Christopher Haster 1:24750b9ad5ef 180
Christopher Haster 1:24750b9ad5ef 181 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Christopher Haster 1:24750b9ad5ef 182 const unsigned char *data, size_t len )
Christopher Haster 1:24750b9ad5ef 183 {
Christopher Haster 1:24750b9ad5ef 184 int ret;
Christopher Haster 1:24750b9ad5ef 185
Christopher Haster 1:24750b9ad5ef 186 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 187 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 188 return( ret );
Christopher Haster 1:24750b9ad5ef 189 #endif
Christopher Haster 1:24750b9ad5ef 190
Christopher Haster 1:24750b9ad5ef 191 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
Christopher Haster 1:24750b9ad5ef 192
Christopher Haster 1:24750b9ad5ef 193 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 194 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Christopher Haster 1:24750b9ad5ef 195 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Christopher Haster 1:24750b9ad5ef 196 #endif
Christopher Haster 1:24750b9ad5ef 197
Christopher Haster 1:24750b9ad5ef 198 return( ret );
Christopher Haster 1:24750b9ad5ef 199 }
Christopher Haster 1:24750b9ad5ef 200
Christopher Haster 1:24750b9ad5ef 201 /*
Christopher Haster 1:24750b9ad5ef 202 * Run through the different sources to add entropy to our accumulator
Christopher Haster 1:24750b9ad5ef 203 */
Christopher Haster 1:24750b9ad5ef 204 static int entropy_gather_internal( mbedtls_entropy_context *ctx )
Christopher Haster 1:24750b9ad5ef 205 {
Christopher Haster 1:24750b9ad5ef 206 int ret, i, have_one_strong = 0;
Christopher Haster 1:24750b9ad5ef 207 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
Christopher Haster 1:24750b9ad5ef 208 size_t olen;
Christopher Haster 1:24750b9ad5ef 209
Christopher Haster 1:24750b9ad5ef 210 if( ctx->source_count == 0 )
Christopher Haster 1:24750b9ad5ef 211 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
Christopher Haster 1:24750b9ad5ef 212
Christopher Haster 1:24750b9ad5ef 213 /*
Christopher Haster 1:24750b9ad5ef 214 * Run through our entropy sources
Christopher Haster 1:24750b9ad5ef 215 */
Christopher Haster 1:24750b9ad5ef 216 for( i = 0; i < ctx->source_count; i++ )
Christopher Haster 1:24750b9ad5ef 217 {
Christopher Haster 1:24750b9ad5ef 218 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
Christopher Haster 1:24750b9ad5ef 219 have_one_strong = 1;
Christopher Haster 1:24750b9ad5ef 220
Christopher Haster 1:24750b9ad5ef 221 olen = 0;
Christopher Haster 1:24750b9ad5ef 222 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Christopher Haster 1:24750b9ad5ef 223 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 224 {
Christopher Haster 1:24750b9ad5ef 225 return( ret );
Christopher Haster 1:24750b9ad5ef 226 }
Christopher Haster 1:24750b9ad5ef 227
Christopher Haster 1:24750b9ad5ef 228 /*
Christopher Haster 1:24750b9ad5ef 229 * Add if we actually gathered something
Christopher Haster 1:24750b9ad5ef 230 */
Christopher Haster 1:24750b9ad5ef 231 if( olen > 0 )
Christopher Haster 1:24750b9ad5ef 232 {
Christopher Haster 1:24750b9ad5ef 233 entropy_update( ctx, (unsigned char) i, buf, olen );
Christopher Haster 1:24750b9ad5ef 234 ctx->source[i].size += olen;
Christopher Haster 1:24750b9ad5ef 235 }
Christopher Haster 1:24750b9ad5ef 236 }
Christopher Haster 1:24750b9ad5ef 237
Christopher Haster 1:24750b9ad5ef 238 if( have_one_strong == 0 )
Christopher Haster 1:24750b9ad5ef 239 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE );
Christopher Haster 1:24750b9ad5ef 240
Christopher Haster 1:24750b9ad5ef 241 return( 0 );
Christopher Haster 1:24750b9ad5ef 242 }
Christopher Haster 1:24750b9ad5ef 243
Christopher Haster 1:24750b9ad5ef 244 /*
Christopher Haster 1:24750b9ad5ef 245 * Thread-safe wrapper for entropy_gather_internal()
Christopher Haster 1:24750b9ad5ef 246 */
Christopher Haster 1:24750b9ad5ef 247 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
Christopher Haster 1:24750b9ad5ef 248 {
Christopher Haster 1:24750b9ad5ef 249 int ret;
Christopher Haster 1:24750b9ad5ef 250
Christopher Haster 1:24750b9ad5ef 251 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 252 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 253 return( ret );
Christopher Haster 1:24750b9ad5ef 254 #endif
Christopher Haster 1:24750b9ad5ef 255
Christopher Haster 1:24750b9ad5ef 256 ret = entropy_gather_internal( ctx );
Christopher Haster 1:24750b9ad5ef 257
Christopher Haster 1:24750b9ad5ef 258 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 259 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Christopher Haster 1:24750b9ad5ef 260 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Christopher Haster 1:24750b9ad5ef 261 #endif
Christopher Haster 1:24750b9ad5ef 262
Christopher Haster 1:24750b9ad5ef 263 return( ret );
Christopher Haster 1:24750b9ad5ef 264 }
Christopher Haster 1:24750b9ad5ef 265
Christopher Haster 1:24750b9ad5ef 266 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
Christopher Haster 1:24750b9ad5ef 267 {
Christopher Haster 1:24750b9ad5ef 268 int ret, count = 0, i, done;
Christopher Haster 1:24750b9ad5ef 269 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
Christopher Haster 1:24750b9ad5ef 270 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Christopher Haster 1:24750b9ad5ef 271
Christopher Haster 1:24750b9ad5ef 272 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
Christopher Haster 1:24750b9ad5ef 273 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 274
Christopher Haster 1:24750b9ad5ef 275 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 276 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 277 return( ret );
Christopher Haster 1:24750b9ad5ef 278 #endif
Christopher Haster 1:24750b9ad5ef 279
Christopher Haster 1:24750b9ad5ef 280 /*
Christopher Haster 1:24750b9ad5ef 281 * Always gather extra entropy before a call
Christopher Haster 1:24750b9ad5ef 282 */
Christopher Haster 1:24750b9ad5ef 283 do
Christopher Haster 1:24750b9ad5ef 284 {
Christopher Haster 1:24750b9ad5ef 285 if( count++ > ENTROPY_MAX_LOOP )
Christopher Haster 1:24750b9ad5ef 286 {
Christopher Haster 1:24750b9ad5ef 287 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Christopher Haster 1:24750b9ad5ef 288 goto exit;
Christopher Haster 1:24750b9ad5ef 289 }
Christopher Haster 1:24750b9ad5ef 290
Christopher Haster 1:24750b9ad5ef 291 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 292 goto exit;
Christopher Haster 1:24750b9ad5ef 293
Christopher Haster 1:24750b9ad5ef 294 done = 1;
Christopher Haster 1:24750b9ad5ef 295 for( i = 0; i < ctx->source_count; i++ )
Christopher Haster 1:24750b9ad5ef 296 if( ctx->source[i].size < ctx->source[i].threshold )
Christopher Haster 1:24750b9ad5ef 297 done = 0;
Christopher Haster 1:24750b9ad5ef 298 }
Christopher Haster 1:24750b9ad5ef 299 while( ! done );
Christopher Haster 1:24750b9ad5ef 300
Christopher Haster 1:24750b9ad5ef 301 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Christopher Haster 1:24750b9ad5ef 302
Christopher Haster 1:24750b9ad5ef 303 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 304 mbedtls_sha512_finish( &ctx->accumulator, buf );
Christopher Haster 1:24750b9ad5ef 305
Christopher Haster 1:24750b9ad5ef 306 /*
Christopher Haster 1:24750b9ad5ef 307 * Reset accumulator and counters and recycle existing entropy
Christopher Haster 1:24750b9ad5ef 308 */
Christopher Haster 1:24750b9ad5ef 309 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
Christopher Haster 1:24750b9ad5ef 310 mbedtls_sha512_starts( &ctx->accumulator, 0 );
Christopher Haster 1:24750b9ad5ef 311 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Christopher Haster 1:24750b9ad5ef 312
Christopher Haster 1:24750b9ad5ef 313 /*
Christopher Haster 1:24750b9ad5ef 314 * Perform second SHA-512 on entropy
Christopher Haster 1:24750b9ad5ef 315 */
Christopher Haster 1:24750b9ad5ef 316 mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
Christopher Haster 1:24750b9ad5ef 317 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Christopher Haster 1:24750b9ad5ef 318 mbedtls_sha256_finish( &ctx->accumulator, buf );
Christopher Haster 1:24750b9ad5ef 319
Christopher Haster 1:24750b9ad5ef 320 /*
Christopher Haster 1:24750b9ad5ef 321 * Reset accumulator and counters and recycle existing entropy
Christopher Haster 1:24750b9ad5ef 322 */
Christopher Haster 1:24750b9ad5ef 323 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
Christopher Haster 1:24750b9ad5ef 324 mbedtls_sha256_starts( &ctx->accumulator, 0 );
Christopher Haster 1:24750b9ad5ef 325 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
Christopher Haster 1:24750b9ad5ef 326
Christopher Haster 1:24750b9ad5ef 327 /*
Christopher Haster 1:24750b9ad5ef 328 * Perform second SHA-256 on entropy
Christopher Haster 1:24750b9ad5ef 329 */
Christopher Haster 1:24750b9ad5ef 330 mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
Christopher Haster 1:24750b9ad5ef 331 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
Christopher Haster 1:24750b9ad5ef 332
Christopher Haster 1:24750b9ad5ef 333 for( i = 0; i < ctx->source_count; i++ )
Christopher Haster 1:24750b9ad5ef 334 ctx->source[i].size = 0;
Christopher Haster 1:24750b9ad5ef 335
Christopher Haster 1:24750b9ad5ef 336 memcpy( output, buf, len );
Christopher Haster 1:24750b9ad5ef 337
Christopher Haster 1:24750b9ad5ef 338 ret = 0;
Christopher Haster 1:24750b9ad5ef 339
Christopher Haster 1:24750b9ad5ef 340 exit:
Christopher Haster 1:24750b9ad5ef 341 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 342 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Christopher Haster 1:24750b9ad5ef 343 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Christopher Haster 1:24750b9ad5ef 344 #endif
Christopher Haster 1:24750b9ad5ef 345
Christopher Haster 1:24750b9ad5ef 346 return( ret );
Christopher Haster 1:24750b9ad5ef 347 }
Christopher Haster 1:24750b9ad5ef 348
Christopher Haster 1:24750b9ad5ef 349 #if defined(MBEDTLS_FS_IO)
Christopher Haster 1:24750b9ad5ef 350 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
Christopher Haster 1:24750b9ad5ef 351 {
Christopher Haster 1:24750b9ad5ef 352 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Christopher Haster 1:24750b9ad5ef 353 FILE *f;
Christopher Haster 1:24750b9ad5ef 354 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Christopher Haster 1:24750b9ad5ef 355
Christopher Haster 1:24750b9ad5ef 356 if( ( f = fopen( path, "wb" ) ) == NULL )
Christopher Haster 1:24750b9ad5ef 357 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Christopher Haster 1:24750b9ad5ef 358
Christopher Haster 1:24750b9ad5ef 359 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 360 goto exit;
Christopher Haster 1:24750b9ad5ef 361
Christopher Haster 1:24750b9ad5ef 362 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
Christopher Haster 1:24750b9ad5ef 363 {
Christopher Haster 1:24750b9ad5ef 364 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
Christopher Haster 1:24750b9ad5ef 365 goto exit;
Christopher Haster 1:24750b9ad5ef 366 }
Christopher Haster 1:24750b9ad5ef 367
Christopher Haster 1:24750b9ad5ef 368 ret = 0;
Christopher Haster 1:24750b9ad5ef 369
Christopher Haster 1:24750b9ad5ef 370 exit:
Christopher Haster 1:24750b9ad5ef 371 fclose( f );
Christopher Haster 1:24750b9ad5ef 372 return( ret );
Christopher Haster 1:24750b9ad5ef 373 }
Christopher Haster 1:24750b9ad5ef 374
Christopher Haster 1:24750b9ad5ef 375 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
Christopher Haster 1:24750b9ad5ef 376 {
Christopher Haster 1:24750b9ad5ef 377 FILE *f;
Christopher Haster 1:24750b9ad5ef 378 size_t n;
Christopher Haster 1:24750b9ad5ef 379 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
Christopher Haster 1:24750b9ad5ef 380
Christopher Haster 1:24750b9ad5ef 381 if( ( f = fopen( path, "rb" ) ) == NULL )
Christopher Haster 1:24750b9ad5ef 382 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Christopher Haster 1:24750b9ad5ef 383
Christopher Haster 1:24750b9ad5ef 384 fseek( f, 0, SEEK_END );
Christopher Haster 1:24750b9ad5ef 385 n = (size_t) ftell( f );
Christopher Haster 1:24750b9ad5ef 386 fseek( f, 0, SEEK_SET );
Christopher Haster 1:24750b9ad5ef 387
Christopher Haster 1:24750b9ad5ef 388 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
Christopher Haster 1:24750b9ad5ef 389 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Christopher Haster 1:24750b9ad5ef 390
Christopher Haster 1:24750b9ad5ef 391 if( fread( buf, 1, n, f ) != n )
Christopher Haster 1:24750b9ad5ef 392 {
Christopher Haster 1:24750b9ad5ef 393 fclose( f );
Christopher Haster 1:24750b9ad5ef 394 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
Christopher Haster 1:24750b9ad5ef 395 }
Christopher Haster 1:24750b9ad5ef 396
Christopher Haster 1:24750b9ad5ef 397 fclose( f );
Christopher Haster 1:24750b9ad5ef 398
Christopher Haster 1:24750b9ad5ef 399 mbedtls_entropy_update_manual( ctx, buf, n );
Christopher Haster 1:24750b9ad5ef 400
Christopher Haster 1:24750b9ad5ef 401 return( mbedtls_entropy_write_seed_file( ctx, path ) );
Christopher Haster 1:24750b9ad5ef 402 }
Christopher Haster 1:24750b9ad5ef 403 #endif /* MBEDTLS_FS_IO */
Christopher Haster 1:24750b9ad5ef 404
Christopher Haster 1:24750b9ad5ef 405 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 406 /*
Christopher Haster 1:24750b9ad5ef 407 * Dummy source function
Christopher Haster 1:24750b9ad5ef 408 */
Christopher Haster 1:24750b9ad5ef 409 static int entropy_dummy_source( void *data, unsigned char *output,
Christopher Haster 1:24750b9ad5ef 410 size_t len, size_t *olen )
Christopher Haster 1:24750b9ad5ef 411 {
Christopher Haster 1:24750b9ad5ef 412 ((void) data);
Christopher Haster 1:24750b9ad5ef 413
Christopher Haster 1:24750b9ad5ef 414 memset( output, 0x2a, len );
Christopher Haster 1:24750b9ad5ef 415 *olen = len;
Christopher Haster 1:24750b9ad5ef 416
Christopher Haster 1:24750b9ad5ef 417 return( 0 );
Christopher Haster 1:24750b9ad5ef 418 }
Christopher Haster 1:24750b9ad5ef 419
Christopher Haster 1:24750b9ad5ef 420 /*
Christopher Haster 1:24750b9ad5ef 421 * The actual entropy quality is hard to test, but we can at least
Christopher Haster 1:24750b9ad5ef 422 * test that the functions don't cause errors and write the correct
Christopher Haster 1:24750b9ad5ef 423 * amount of data to buffers.
Christopher Haster 1:24750b9ad5ef 424 */
Christopher Haster 1:24750b9ad5ef 425 int mbedtls_entropy_self_test( int verbose )
Christopher Haster 1:24750b9ad5ef 426 {
Christopher Haster 1:24750b9ad5ef 427 int ret = 0;
Christopher Haster 1:24750b9ad5ef 428 mbedtls_entropy_context ctx;
Christopher Haster 1:24750b9ad5ef 429 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Christopher Haster 1:24750b9ad5ef 430 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Christopher Haster 1:24750b9ad5ef 431 size_t i, j;
Christopher Haster 1:24750b9ad5ef 432
Christopher Haster 1:24750b9ad5ef 433 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 434 mbedtls_printf( " ENTROPY test: " );
Christopher Haster 1:24750b9ad5ef 435
Christopher Haster 1:24750b9ad5ef 436 mbedtls_entropy_init( &ctx );
Christopher Haster 1:24750b9ad5ef 437
Christopher Haster 1:24750b9ad5ef 438 /* First do a gather to make sure we have default sources */
Christopher Haster 1:24750b9ad5ef 439 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 440 goto cleanup;
Christopher Haster 1:24750b9ad5ef 441
Christopher Haster 1:24750b9ad5ef 442 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
Christopher Haster 1:24750b9ad5ef 443 MBEDTLS_ENTROPY_SOURCE_WEAK );
Christopher Haster 1:24750b9ad5ef 444 if( ret != 0 )
Christopher Haster 1:24750b9ad5ef 445 goto cleanup;
Christopher Haster 1:24750b9ad5ef 446
Christopher Haster 1:24750b9ad5ef 447 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 448 goto cleanup;
Christopher Haster 1:24750b9ad5ef 449
Christopher Haster 1:24750b9ad5ef 450 /*
Christopher Haster 1:24750b9ad5ef 451 * To test that mbedtls_entropy_func writes correct number of bytes:
Christopher Haster 1:24750b9ad5ef 452 * - use the whole buffer and rely on ASan to detect overruns
Christopher Haster 1:24750b9ad5ef 453 * - collect entropy 8 times and OR the result in an accumulator:
Christopher Haster 1:24750b9ad5ef 454 * any byte should then be 0 with probably 2^(-64), so requiring
Christopher Haster 1:24750b9ad5ef 455 * each of the 32 or 64 bytes to be non-zero has a false failure rate
Christopher Haster 1:24750b9ad5ef 456 * of at most 2^(-58) which is acceptable.
Christopher Haster 1:24750b9ad5ef 457 */
Christopher Haster 1:24750b9ad5ef 458 for( i = 0; i < 8; i++ )
Christopher Haster 1:24750b9ad5ef 459 {
Christopher Haster 1:24750b9ad5ef 460 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
Christopher Haster 1:24750b9ad5ef 461 goto cleanup;
Christopher Haster 1:24750b9ad5ef 462
Christopher Haster 1:24750b9ad5ef 463 for( j = 0; j < sizeof( buf ); j++ )
Christopher Haster 1:24750b9ad5ef 464 acc[j] |= buf[j];
Christopher Haster 1:24750b9ad5ef 465 }
Christopher Haster 1:24750b9ad5ef 466
Christopher Haster 1:24750b9ad5ef 467 for( j = 0; j < sizeof( buf ); j++ )
Christopher Haster 1:24750b9ad5ef 468 {
Christopher Haster 1:24750b9ad5ef 469 if( acc[j] == 0 )
Christopher Haster 1:24750b9ad5ef 470 {
Christopher Haster 1:24750b9ad5ef 471 ret = 1;
Christopher Haster 1:24750b9ad5ef 472 goto cleanup;
Christopher Haster 1:24750b9ad5ef 473 }
Christopher Haster 1:24750b9ad5ef 474 }
Christopher Haster 1:24750b9ad5ef 475
Christopher Haster 1:24750b9ad5ef 476 cleanup:
Christopher Haster 1:24750b9ad5ef 477 mbedtls_entropy_free( &ctx );
Christopher Haster 1:24750b9ad5ef 478
Christopher Haster 1:24750b9ad5ef 479 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 480 {
Christopher Haster 1:24750b9ad5ef 481 if( ret != 0 )
Christopher Haster 1:24750b9ad5ef 482 mbedtls_printf( "failed\n" );
Christopher Haster 1:24750b9ad5ef 483 else
Christopher Haster 1:24750b9ad5ef 484 mbedtls_printf( "passed\n" );
Christopher Haster 1:24750b9ad5ef 485
Christopher Haster 1:24750b9ad5ef 486 mbedtls_printf( "\n" );
Christopher Haster 1:24750b9ad5ef 487 }
Christopher Haster 1:24750b9ad5ef 488
Christopher Haster 1:24750b9ad5ef 489 return( ret != 0 );
Christopher Haster 1:24750b9ad5ef 490 }
Christopher Haster 1:24750b9ad5ef 491 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 492
Christopher Haster 1:24750b9ad5ef 493 #endif /* MBEDTLS_ENTROPY_C */