Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

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 * Buffer-based memory allocator
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_MEMORY_BUFFER_ALLOC_C)
Christopher Haster 1:24750b9ad5ef 29 #include "mbedtls/memory_buffer_alloc.h"
Christopher Haster 1:24750b9ad5ef 30
Christopher Haster 1:24750b9ad5ef 31 /* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
Christopher Haster 1:24750b9ad5ef 32 is dependent upon MBEDTLS_PLATFORM_C */
Christopher Haster 1:24750b9ad5ef 33 #include "mbedtls/platform.h"
Christopher Haster 1:24750b9ad5ef 34
Christopher Haster 1:24750b9ad5ef 35 #include <string.h>
Christopher Haster 1:24750b9ad5ef 36
Christopher Haster 1:24750b9ad5ef 37 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 38 #include <execinfo.h>
Christopher Haster 1:24750b9ad5ef 39 #endif
Christopher Haster 1:24750b9ad5ef 40
Christopher Haster 1:24750b9ad5ef 41 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 42 #include "mbedtls/threading.h"
Christopher Haster 1:24750b9ad5ef 43 #endif
Christopher Haster 1:24750b9ad5ef 44
Christopher Haster 1:24750b9ad5ef 45 /* Implementation that should never be optimized out by the compiler */
Christopher Haster 1:24750b9ad5ef 46 static void mbedtls_zeroize( void *v, size_t n ) {
Christopher Haster 1:24750b9ad5ef 47 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Christopher Haster 1:24750b9ad5ef 48 }
Christopher Haster 1:24750b9ad5ef 49
Christopher Haster 1:24750b9ad5ef 50 #define MAGIC1 0xFF00AA55
Christopher Haster 1:24750b9ad5ef 51 #define MAGIC2 0xEE119966
Christopher Haster 1:24750b9ad5ef 52 #define MAX_BT 20
Christopher Haster 1:24750b9ad5ef 53
Christopher Haster 1:24750b9ad5ef 54 typedef struct _memory_header memory_header;
Christopher Haster 1:24750b9ad5ef 55 struct _memory_header
Christopher Haster 1:24750b9ad5ef 56 {
Christopher Haster 1:24750b9ad5ef 57 size_t magic1;
Christopher Haster 1:24750b9ad5ef 58 size_t size;
Christopher Haster 1:24750b9ad5ef 59 size_t alloc;
Christopher Haster 1:24750b9ad5ef 60 memory_header *prev;
Christopher Haster 1:24750b9ad5ef 61 memory_header *next;
Christopher Haster 1:24750b9ad5ef 62 memory_header *prev_free;
Christopher Haster 1:24750b9ad5ef 63 memory_header *next_free;
Christopher Haster 1:24750b9ad5ef 64 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 65 char **trace;
Christopher Haster 1:24750b9ad5ef 66 size_t trace_count;
Christopher Haster 1:24750b9ad5ef 67 #endif
Christopher Haster 1:24750b9ad5ef 68 size_t magic2;
Christopher Haster 1:24750b9ad5ef 69 };
Christopher Haster 1:24750b9ad5ef 70
Christopher Haster 1:24750b9ad5ef 71 typedef struct
Christopher Haster 1:24750b9ad5ef 72 {
Christopher Haster 1:24750b9ad5ef 73 unsigned char *buf;
Christopher Haster 1:24750b9ad5ef 74 size_t len;
Christopher Haster 1:24750b9ad5ef 75 memory_header *first;
Christopher Haster 1:24750b9ad5ef 76 memory_header *first_free;
Christopher Haster 1:24750b9ad5ef 77 int verify;
Christopher Haster 1:24750b9ad5ef 78 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 79 size_t alloc_count;
Christopher Haster 1:24750b9ad5ef 80 size_t free_count;
Christopher Haster 1:24750b9ad5ef 81 size_t total_used;
Christopher Haster 1:24750b9ad5ef 82 size_t maximum_used;
Christopher Haster 1:24750b9ad5ef 83 size_t header_count;
Christopher Haster 1:24750b9ad5ef 84 size_t maximum_header_count;
Christopher Haster 1:24750b9ad5ef 85 #endif
Christopher Haster 1:24750b9ad5ef 86 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 87 mbedtls_threading_mutex_t mutex;
Christopher Haster 1:24750b9ad5ef 88 #endif
Christopher Haster 1:24750b9ad5ef 89 }
Christopher Haster 1:24750b9ad5ef 90 buffer_alloc_ctx;
Christopher Haster 1:24750b9ad5ef 91
Christopher Haster 1:24750b9ad5ef 92 static buffer_alloc_ctx heap;
Christopher Haster 1:24750b9ad5ef 93
Christopher Haster 1:24750b9ad5ef 94 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 95 static void debug_header( memory_header *hdr )
Christopher Haster 1:24750b9ad5ef 96 {
Christopher Haster 1:24750b9ad5ef 97 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 98 size_t i;
Christopher Haster 1:24750b9ad5ef 99 #endif
Christopher Haster 1:24750b9ad5ef 100
Christopher Haster 1:24750b9ad5ef 101 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Christopher Haster 1:24750b9ad5ef 102 "ALLOC(%zu), SIZE(%10zu)\n",
Christopher Haster 1:24750b9ad5ef 103 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
Christopher Haster 1:24750b9ad5ef 104 hdr->alloc, hdr->size );
Christopher Haster 1:24750b9ad5ef 105 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Christopher Haster 1:24750b9ad5ef 106 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Christopher Haster 1:24750b9ad5ef 107
Christopher Haster 1:24750b9ad5ef 108 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 109 mbedtls_fprintf( stderr, "TRACE: \n" );
Christopher Haster 1:24750b9ad5ef 110 for( i = 0; i < hdr->trace_count; i++ )
Christopher Haster 1:24750b9ad5ef 111 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
Christopher Haster 1:24750b9ad5ef 112 mbedtls_fprintf( stderr, "\n" );
Christopher Haster 1:24750b9ad5ef 113 #endif
Christopher Haster 1:24750b9ad5ef 114 }
Christopher Haster 1:24750b9ad5ef 115
Christopher Haster 1:24750b9ad5ef 116 static void debug_chain()
Christopher Haster 1:24750b9ad5ef 117 {
Christopher Haster 1:24750b9ad5ef 118 memory_header *cur = heap.first;
Christopher Haster 1:24750b9ad5ef 119
Christopher Haster 1:24750b9ad5ef 120 mbedtls_fprintf( stderr, "\nBlock list\n" );
Christopher Haster 1:24750b9ad5ef 121 while( cur != NULL )
Christopher Haster 1:24750b9ad5ef 122 {
Christopher Haster 1:24750b9ad5ef 123 debug_header( cur );
Christopher Haster 1:24750b9ad5ef 124 cur = cur->next;
Christopher Haster 1:24750b9ad5ef 125 }
Christopher Haster 1:24750b9ad5ef 126
Christopher Haster 1:24750b9ad5ef 127 mbedtls_fprintf( stderr, "Free list\n" );
Christopher Haster 1:24750b9ad5ef 128 cur = heap.first_free;
Christopher Haster 1:24750b9ad5ef 129
Christopher Haster 1:24750b9ad5ef 130 while( cur != NULL )
Christopher Haster 1:24750b9ad5ef 131 {
Christopher Haster 1:24750b9ad5ef 132 debug_header( cur );
Christopher Haster 1:24750b9ad5ef 133 cur = cur->next_free;
Christopher Haster 1:24750b9ad5ef 134 }
Christopher Haster 1:24750b9ad5ef 135 }
Christopher Haster 1:24750b9ad5ef 136 #endif /* MBEDTLS_MEMORY_DEBUG */
Christopher Haster 1:24750b9ad5ef 137
Christopher Haster 1:24750b9ad5ef 138 static int verify_header( memory_header *hdr )
Christopher Haster 1:24750b9ad5ef 139 {
Christopher Haster 1:24750b9ad5ef 140 if( hdr->magic1 != MAGIC1 )
Christopher Haster 1:24750b9ad5ef 141 {
Christopher Haster 1:24750b9ad5ef 142 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 143 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Christopher Haster 1:24750b9ad5ef 144 #endif
Christopher Haster 1:24750b9ad5ef 145 return( 1 );
Christopher Haster 1:24750b9ad5ef 146 }
Christopher Haster 1:24750b9ad5ef 147
Christopher Haster 1:24750b9ad5ef 148 if( hdr->magic2 != MAGIC2 )
Christopher Haster 1:24750b9ad5ef 149 {
Christopher Haster 1:24750b9ad5ef 150 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 151 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Christopher Haster 1:24750b9ad5ef 152 #endif
Christopher Haster 1:24750b9ad5ef 153 return( 1 );
Christopher Haster 1:24750b9ad5ef 154 }
Christopher Haster 1:24750b9ad5ef 155
Christopher Haster 1:24750b9ad5ef 156 if( hdr->alloc > 1 )
Christopher Haster 1:24750b9ad5ef 157 {
Christopher Haster 1:24750b9ad5ef 158 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 159 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Christopher Haster 1:24750b9ad5ef 160 #endif
Christopher Haster 1:24750b9ad5ef 161 return( 1 );
Christopher Haster 1:24750b9ad5ef 162 }
Christopher Haster 1:24750b9ad5ef 163
Christopher Haster 1:24750b9ad5ef 164 if( hdr->prev != NULL && hdr->prev == hdr->next )
Christopher Haster 1:24750b9ad5ef 165 {
Christopher Haster 1:24750b9ad5ef 166 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 167 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Christopher Haster 1:24750b9ad5ef 168 #endif
Christopher Haster 1:24750b9ad5ef 169 return( 1 );
Christopher Haster 1:24750b9ad5ef 170 }
Christopher Haster 1:24750b9ad5ef 171
Christopher Haster 1:24750b9ad5ef 172 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
Christopher Haster 1:24750b9ad5ef 173 {
Christopher Haster 1:24750b9ad5ef 174 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 175 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Christopher Haster 1:24750b9ad5ef 176 #endif
Christopher Haster 1:24750b9ad5ef 177 return( 1 );
Christopher Haster 1:24750b9ad5ef 178 }
Christopher Haster 1:24750b9ad5ef 179
Christopher Haster 1:24750b9ad5ef 180 return( 0 );
Christopher Haster 1:24750b9ad5ef 181 }
Christopher Haster 1:24750b9ad5ef 182
Christopher Haster 1:24750b9ad5ef 183 static int verify_chain()
Christopher Haster 1:24750b9ad5ef 184 {
Christopher Haster 1:24750b9ad5ef 185 memory_header *prv = heap.first, *cur = heap.first->next;
Christopher Haster 1:24750b9ad5ef 186
Christopher Haster 1:24750b9ad5ef 187 if( verify_header( heap.first ) != 0 )
Christopher Haster 1:24750b9ad5ef 188 {
Christopher Haster 1:24750b9ad5ef 189 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 190 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Christopher Haster 1:24750b9ad5ef 191 "failed\n" );
Christopher Haster 1:24750b9ad5ef 192 #endif
Christopher Haster 1:24750b9ad5ef 193 return( 1 );
Christopher Haster 1:24750b9ad5ef 194 }
Christopher Haster 1:24750b9ad5ef 195
Christopher Haster 1:24750b9ad5ef 196 if( heap.first->prev != NULL )
Christopher Haster 1:24750b9ad5ef 197 {
Christopher Haster 1:24750b9ad5ef 198 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 199 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Christopher Haster 1:24750b9ad5ef 200 "first->prev != NULL\n" );
Christopher Haster 1:24750b9ad5ef 201 #endif
Christopher Haster 1:24750b9ad5ef 202 return( 1 );
Christopher Haster 1:24750b9ad5ef 203 }
Christopher Haster 1:24750b9ad5ef 204
Christopher Haster 1:24750b9ad5ef 205 while( cur != NULL )
Christopher Haster 1:24750b9ad5ef 206 {
Christopher Haster 1:24750b9ad5ef 207 if( verify_header( cur ) != 0 )
Christopher Haster 1:24750b9ad5ef 208 {
Christopher Haster 1:24750b9ad5ef 209 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 210 mbedtls_fprintf( stderr, "FATAL: verification of header "
Christopher Haster 1:24750b9ad5ef 211 "failed\n" );
Christopher Haster 1:24750b9ad5ef 212 #endif
Christopher Haster 1:24750b9ad5ef 213 return( 1 );
Christopher Haster 1:24750b9ad5ef 214 }
Christopher Haster 1:24750b9ad5ef 215
Christopher Haster 1:24750b9ad5ef 216 if( cur->prev != prv )
Christopher Haster 1:24750b9ad5ef 217 {
Christopher Haster 1:24750b9ad5ef 218 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 219 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Christopher Haster 1:24750b9ad5ef 220 "cur->prev != prv\n" );
Christopher Haster 1:24750b9ad5ef 221 #endif
Christopher Haster 1:24750b9ad5ef 222 return( 1 );
Christopher Haster 1:24750b9ad5ef 223 }
Christopher Haster 1:24750b9ad5ef 224
Christopher Haster 1:24750b9ad5ef 225 prv = cur;
Christopher Haster 1:24750b9ad5ef 226 cur = cur->next;
Christopher Haster 1:24750b9ad5ef 227 }
Christopher Haster 1:24750b9ad5ef 228
Christopher Haster 1:24750b9ad5ef 229 return( 0 );
Christopher Haster 1:24750b9ad5ef 230 }
Christopher Haster 1:24750b9ad5ef 231
Christopher Haster 1:24750b9ad5ef 232 static void *buffer_alloc_calloc( size_t n, size_t size )
Christopher Haster 1:24750b9ad5ef 233 {
Christopher Haster 1:24750b9ad5ef 234 memory_header *new, *cur = heap.first_free;
Christopher Haster 1:24750b9ad5ef 235 unsigned char *p;
Christopher Haster 1:24750b9ad5ef 236 void *ret;
Christopher Haster 1:24750b9ad5ef 237 size_t original_len, len;
Christopher Haster 1:24750b9ad5ef 238 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 239 void *trace_buffer[MAX_BT];
Christopher Haster 1:24750b9ad5ef 240 size_t trace_cnt;
Christopher Haster 1:24750b9ad5ef 241 #endif
Christopher Haster 1:24750b9ad5ef 242
Christopher Haster 1:24750b9ad5ef 243 if( heap.buf == NULL || heap.first == NULL )
Christopher Haster 1:24750b9ad5ef 244 return( NULL );
Christopher Haster 1:24750b9ad5ef 245
Christopher Haster 1:24750b9ad5ef 246 original_len = len = n * size;
Christopher Haster 1:24750b9ad5ef 247
Christopher Haster 1:24750b9ad5ef 248 if( n != 0 && len / n != size )
Christopher Haster 1:24750b9ad5ef 249 return( NULL );
Christopher Haster 1:24750b9ad5ef 250
Christopher Haster 1:24750b9ad5ef 251 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Christopher Haster 1:24750b9ad5ef 252 {
Christopher Haster 1:24750b9ad5ef 253 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Christopher Haster 1:24750b9ad5ef 254 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Christopher Haster 1:24750b9ad5ef 255 }
Christopher Haster 1:24750b9ad5ef 256
Christopher Haster 1:24750b9ad5ef 257 // Find block that fits
Christopher Haster 1:24750b9ad5ef 258 //
Christopher Haster 1:24750b9ad5ef 259 while( cur != NULL )
Christopher Haster 1:24750b9ad5ef 260 {
Christopher Haster 1:24750b9ad5ef 261 if( cur->size >= len )
Christopher Haster 1:24750b9ad5ef 262 break;
Christopher Haster 1:24750b9ad5ef 263
Christopher Haster 1:24750b9ad5ef 264 cur = cur->next_free;
Christopher Haster 1:24750b9ad5ef 265 }
Christopher Haster 1:24750b9ad5ef 266
Christopher Haster 1:24750b9ad5ef 267 if( cur == NULL )
Christopher Haster 1:24750b9ad5ef 268 return( NULL );
Christopher Haster 1:24750b9ad5ef 269
Christopher Haster 1:24750b9ad5ef 270 if( cur->alloc != 0 )
Christopher Haster 1:24750b9ad5ef 271 {
Christopher Haster 1:24750b9ad5ef 272 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 273 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Christopher Haster 1:24750b9ad5ef 274 "data\n" );
Christopher Haster 1:24750b9ad5ef 275 #endif
Christopher Haster 1:24750b9ad5ef 276 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 277 }
Christopher Haster 1:24750b9ad5ef 278
Christopher Haster 1:24750b9ad5ef 279 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 280 heap.alloc_count++;
Christopher Haster 1:24750b9ad5ef 281 #endif
Christopher Haster 1:24750b9ad5ef 282
Christopher Haster 1:24750b9ad5ef 283 // Found location, split block if > memory_header + 4 room left
Christopher Haster 1:24750b9ad5ef 284 //
Christopher Haster 1:24750b9ad5ef 285 if( cur->size - len < sizeof(memory_header) +
Christopher Haster 1:24750b9ad5ef 286 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Christopher Haster 1:24750b9ad5ef 287 {
Christopher Haster 1:24750b9ad5ef 288 cur->alloc = 1;
Christopher Haster 1:24750b9ad5ef 289
Christopher Haster 1:24750b9ad5ef 290 // Remove from free_list
Christopher Haster 1:24750b9ad5ef 291 //
Christopher Haster 1:24750b9ad5ef 292 if( cur->prev_free != NULL )
Christopher Haster 1:24750b9ad5ef 293 cur->prev_free->next_free = cur->next_free;
Christopher Haster 1:24750b9ad5ef 294 else
Christopher Haster 1:24750b9ad5ef 295 heap.first_free = cur->next_free;
Christopher Haster 1:24750b9ad5ef 296
Christopher Haster 1:24750b9ad5ef 297 if( cur->next_free != NULL )
Christopher Haster 1:24750b9ad5ef 298 cur->next_free->prev_free = cur->prev_free;
Christopher Haster 1:24750b9ad5ef 299
Christopher Haster 1:24750b9ad5ef 300 cur->prev_free = NULL;
Christopher Haster 1:24750b9ad5ef 301 cur->next_free = NULL;
Christopher Haster 1:24750b9ad5ef 302
Christopher Haster 1:24750b9ad5ef 303 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 304 heap.total_used += cur->size;
Christopher Haster 1:24750b9ad5ef 305 if( heap.total_used > heap.maximum_used )
Christopher Haster 1:24750b9ad5ef 306 heap.maximum_used = heap.total_used;
Christopher Haster 1:24750b9ad5ef 307 #endif
Christopher Haster 1:24750b9ad5ef 308 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 309 trace_cnt = backtrace( trace_buffer, MAX_BT );
Christopher Haster 1:24750b9ad5ef 310 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
Christopher Haster 1:24750b9ad5ef 311 cur->trace_count = trace_cnt;
Christopher Haster 1:24750b9ad5ef 312 #endif
Christopher Haster 1:24750b9ad5ef 313
Christopher Haster 1:24750b9ad5ef 314 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Christopher Haster 1:24750b9ad5ef 315 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 316
Christopher Haster 1:24750b9ad5ef 317 ret = (unsigned char *) cur + sizeof( memory_header );
Christopher Haster 1:24750b9ad5ef 318 memset( ret, 0, original_len );
Christopher Haster 1:24750b9ad5ef 319
Christopher Haster 1:24750b9ad5ef 320 return( ret );
Christopher Haster 1:24750b9ad5ef 321 }
Christopher Haster 1:24750b9ad5ef 322
Christopher Haster 1:24750b9ad5ef 323 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
Christopher Haster 1:24750b9ad5ef 324 new = (memory_header *) p;
Christopher Haster 1:24750b9ad5ef 325
Christopher Haster 1:24750b9ad5ef 326 new->size = cur->size - len - sizeof(memory_header);
Christopher Haster 1:24750b9ad5ef 327 new->alloc = 0;
Christopher Haster 1:24750b9ad5ef 328 new->prev = cur;
Christopher Haster 1:24750b9ad5ef 329 new->next = cur->next;
Christopher Haster 1:24750b9ad5ef 330 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 331 new->trace = NULL;
Christopher Haster 1:24750b9ad5ef 332 new->trace_count = 0;
Christopher Haster 1:24750b9ad5ef 333 #endif
Christopher Haster 1:24750b9ad5ef 334 new->magic1 = MAGIC1;
Christopher Haster 1:24750b9ad5ef 335 new->magic2 = MAGIC2;
Christopher Haster 1:24750b9ad5ef 336
Christopher Haster 1:24750b9ad5ef 337 if( new->next != NULL )
Christopher Haster 1:24750b9ad5ef 338 new->next->prev = new;
Christopher Haster 1:24750b9ad5ef 339
Christopher Haster 1:24750b9ad5ef 340 // Replace cur with new in free_list
Christopher Haster 1:24750b9ad5ef 341 //
Christopher Haster 1:24750b9ad5ef 342 new->prev_free = cur->prev_free;
Christopher Haster 1:24750b9ad5ef 343 new->next_free = cur->next_free;
Christopher Haster 1:24750b9ad5ef 344 if( new->prev_free != NULL )
Christopher Haster 1:24750b9ad5ef 345 new->prev_free->next_free = new;
Christopher Haster 1:24750b9ad5ef 346 else
Christopher Haster 1:24750b9ad5ef 347 heap.first_free = new;
Christopher Haster 1:24750b9ad5ef 348
Christopher Haster 1:24750b9ad5ef 349 if( new->next_free != NULL )
Christopher Haster 1:24750b9ad5ef 350 new->next_free->prev_free = new;
Christopher Haster 1:24750b9ad5ef 351
Christopher Haster 1:24750b9ad5ef 352 cur->alloc = 1;
Christopher Haster 1:24750b9ad5ef 353 cur->size = len;
Christopher Haster 1:24750b9ad5ef 354 cur->next = new;
Christopher Haster 1:24750b9ad5ef 355 cur->prev_free = NULL;
Christopher Haster 1:24750b9ad5ef 356 cur->next_free = NULL;
Christopher Haster 1:24750b9ad5ef 357
Christopher Haster 1:24750b9ad5ef 358 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 359 heap.header_count++;
Christopher Haster 1:24750b9ad5ef 360 if( heap.header_count > heap.maximum_header_count )
Christopher Haster 1:24750b9ad5ef 361 heap.maximum_header_count = heap.header_count;
Christopher Haster 1:24750b9ad5ef 362 heap.total_used += cur->size;
Christopher Haster 1:24750b9ad5ef 363 if( heap.total_used > heap.maximum_used )
Christopher Haster 1:24750b9ad5ef 364 heap.maximum_used = heap.total_used;
Christopher Haster 1:24750b9ad5ef 365 #endif
Christopher Haster 1:24750b9ad5ef 366 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 367 trace_cnt = backtrace( trace_buffer, MAX_BT );
Christopher Haster 1:24750b9ad5ef 368 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
Christopher Haster 1:24750b9ad5ef 369 cur->trace_count = trace_cnt;
Christopher Haster 1:24750b9ad5ef 370 #endif
Christopher Haster 1:24750b9ad5ef 371
Christopher Haster 1:24750b9ad5ef 372 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Christopher Haster 1:24750b9ad5ef 373 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 374
Christopher Haster 1:24750b9ad5ef 375 ret = (unsigned char *) cur + sizeof( memory_header );
Christopher Haster 1:24750b9ad5ef 376 memset( ret, 0, original_len );
Christopher Haster 1:24750b9ad5ef 377
Christopher Haster 1:24750b9ad5ef 378 return( ret );
Christopher Haster 1:24750b9ad5ef 379 }
Christopher Haster 1:24750b9ad5ef 380
Christopher Haster 1:24750b9ad5ef 381 static void buffer_alloc_free( void *ptr )
Christopher Haster 1:24750b9ad5ef 382 {
Christopher Haster 1:24750b9ad5ef 383 memory_header *hdr, *old = NULL;
Christopher Haster 1:24750b9ad5ef 384 unsigned char *p = (unsigned char *) ptr;
Christopher Haster 1:24750b9ad5ef 385
Christopher Haster 1:24750b9ad5ef 386 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
Christopher Haster 1:24750b9ad5ef 387 return;
Christopher Haster 1:24750b9ad5ef 388
Christopher Haster 1:24750b9ad5ef 389 if( p < heap.buf || p > heap.buf + heap.len )
Christopher Haster 1:24750b9ad5ef 390 {
Christopher Haster 1:24750b9ad5ef 391 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 392 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Christopher Haster 1:24750b9ad5ef 393 "space\n" );
Christopher Haster 1:24750b9ad5ef 394 #endif
Christopher Haster 1:24750b9ad5ef 395 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 396 }
Christopher Haster 1:24750b9ad5ef 397
Christopher Haster 1:24750b9ad5ef 398 p -= sizeof(memory_header);
Christopher Haster 1:24750b9ad5ef 399 hdr = (memory_header *) p;
Christopher Haster 1:24750b9ad5ef 400
Christopher Haster 1:24750b9ad5ef 401 if( verify_header( hdr ) != 0 )
Christopher Haster 1:24750b9ad5ef 402 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 403
Christopher Haster 1:24750b9ad5ef 404 if( hdr->alloc != 1 )
Christopher Haster 1:24750b9ad5ef 405 {
Christopher Haster 1:24750b9ad5ef 406 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 407 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Christopher Haster 1:24750b9ad5ef 408 "data\n" );
Christopher Haster 1:24750b9ad5ef 409 #endif
Christopher Haster 1:24750b9ad5ef 410 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 411 }
Christopher Haster 1:24750b9ad5ef 412
Christopher Haster 1:24750b9ad5ef 413 hdr->alloc = 0;
Christopher Haster 1:24750b9ad5ef 414
Christopher Haster 1:24750b9ad5ef 415 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 416 heap.free_count++;
Christopher Haster 1:24750b9ad5ef 417 heap.total_used -= hdr->size;
Christopher Haster 1:24750b9ad5ef 418 #endif
Christopher Haster 1:24750b9ad5ef 419
Christopher Haster 1:24750b9ad5ef 420 // Regroup with block before
Christopher Haster 1:24750b9ad5ef 421 //
Christopher Haster 1:24750b9ad5ef 422 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
Christopher Haster 1:24750b9ad5ef 423 {
Christopher Haster 1:24750b9ad5ef 424 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 425 heap.header_count--;
Christopher Haster 1:24750b9ad5ef 426 #endif
Christopher Haster 1:24750b9ad5ef 427 hdr->prev->size += sizeof(memory_header) + hdr->size;
Christopher Haster 1:24750b9ad5ef 428 hdr->prev->next = hdr->next;
Christopher Haster 1:24750b9ad5ef 429 old = hdr;
Christopher Haster 1:24750b9ad5ef 430 hdr = hdr->prev;
Christopher Haster 1:24750b9ad5ef 431
Christopher Haster 1:24750b9ad5ef 432 if( hdr->next != NULL )
Christopher Haster 1:24750b9ad5ef 433 hdr->next->prev = hdr;
Christopher Haster 1:24750b9ad5ef 434
Christopher Haster 1:24750b9ad5ef 435 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 436 free( old->trace );
Christopher Haster 1:24750b9ad5ef 437 #endif
Christopher Haster 1:24750b9ad5ef 438 memset( old, 0, sizeof(memory_header) );
Christopher Haster 1:24750b9ad5ef 439 }
Christopher Haster 1:24750b9ad5ef 440
Christopher Haster 1:24750b9ad5ef 441 // Regroup with block after
Christopher Haster 1:24750b9ad5ef 442 //
Christopher Haster 1:24750b9ad5ef 443 if( hdr->next != NULL && hdr->next->alloc == 0 )
Christopher Haster 1:24750b9ad5ef 444 {
Christopher Haster 1:24750b9ad5ef 445 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 446 heap.header_count--;
Christopher Haster 1:24750b9ad5ef 447 #endif
Christopher Haster 1:24750b9ad5ef 448 hdr->size += sizeof(memory_header) + hdr->next->size;
Christopher Haster 1:24750b9ad5ef 449 old = hdr->next;
Christopher Haster 1:24750b9ad5ef 450 hdr->next = hdr->next->next;
Christopher Haster 1:24750b9ad5ef 451
Christopher Haster 1:24750b9ad5ef 452 if( hdr->prev_free != NULL || hdr->next_free != NULL )
Christopher Haster 1:24750b9ad5ef 453 {
Christopher Haster 1:24750b9ad5ef 454 if( hdr->prev_free != NULL )
Christopher Haster 1:24750b9ad5ef 455 hdr->prev_free->next_free = hdr->next_free;
Christopher Haster 1:24750b9ad5ef 456 else
Christopher Haster 1:24750b9ad5ef 457 heap.first_free = hdr->next_free;
Christopher Haster 1:24750b9ad5ef 458
Christopher Haster 1:24750b9ad5ef 459 if( hdr->next_free != NULL )
Christopher Haster 1:24750b9ad5ef 460 hdr->next_free->prev_free = hdr->prev_free;
Christopher Haster 1:24750b9ad5ef 461 }
Christopher Haster 1:24750b9ad5ef 462
Christopher Haster 1:24750b9ad5ef 463 hdr->prev_free = old->prev_free;
Christopher Haster 1:24750b9ad5ef 464 hdr->next_free = old->next_free;
Christopher Haster 1:24750b9ad5ef 465
Christopher Haster 1:24750b9ad5ef 466 if( hdr->prev_free != NULL )
Christopher Haster 1:24750b9ad5ef 467 hdr->prev_free->next_free = hdr;
Christopher Haster 1:24750b9ad5ef 468 else
Christopher Haster 1:24750b9ad5ef 469 heap.first_free = hdr;
Christopher Haster 1:24750b9ad5ef 470
Christopher Haster 1:24750b9ad5ef 471 if( hdr->next_free != NULL )
Christopher Haster 1:24750b9ad5ef 472 hdr->next_free->prev_free = hdr;
Christopher Haster 1:24750b9ad5ef 473
Christopher Haster 1:24750b9ad5ef 474 if( hdr->next != NULL )
Christopher Haster 1:24750b9ad5ef 475 hdr->next->prev = hdr;
Christopher Haster 1:24750b9ad5ef 476
Christopher Haster 1:24750b9ad5ef 477 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 478 free( old->trace );
Christopher Haster 1:24750b9ad5ef 479 #endif
Christopher Haster 1:24750b9ad5ef 480 memset( old, 0, sizeof(memory_header) );
Christopher Haster 1:24750b9ad5ef 481 }
Christopher Haster 1:24750b9ad5ef 482
Christopher Haster 1:24750b9ad5ef 483 // Prepend to free_list if we have not merged
Christopher Haster 1:24750b9ad5ef 484 // (Does not have to stay in same order as prev / next list)
Christopher Haster 1:24750b9ad5ef 485 //
Christopher Haster 1:24750b9ad5ef 486 if( old == NULL )
Christopher Haster 1:24750b9ad5ef 487 {
Christopher Haster 1:24750b9ad5ef 488 hdr->next_free = heap.first_free;
Christopher Haster 1:24750b9ad5ef 489 if( heap.first_free != NULL )
Christopher Haster 1:24750b9ad5ef 490 heap.first_free->prev_free = hdr;
Christopher Haster 1:24750b9ad5ef 491 heap.first_free = hdr;
Christopher Haster 1:24750b9ad5ef 492 }
Christopher Haster 1:24750b9ad5ef 493
Christopher Haster 1:24750b9ad5ef 494 #if defined(MBEDTLS_MEMORY_BACKTRACE)
Christopher Haster 1:24750b9ad5ef 495 hdr->trace = NULL;
Christopher Haster 1:24750b9ad5ef 496 hdr->trace_count = 0;
Christopher Haster 1:24750b9ad5ef 497 #endif
Christopher Haster 1:24750b9ad5ef 498
Christopher Haster 1:24750b9ad5ef 499 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
Christopher Haster 1:24750b9ad5ef 500 mbedtls_exit( 1 );
Christopher Haster 1:24750b9ad5ef 501 }
Christopher Haster 1:24750b9ad5ef 502
Christopher Haster 1:24750b9ad5ef 503 void mbedtls_memory_buffer_set_verify( int verify )
Christopher Haster 1:24750b9ad5ef 504 {
Christopher Haster 1:24750b9ad5ef 505 heap.verify = verify;
Christopher Haster 1:24750b9ad5ef 506 }
Christopher Haster 1:24750b9ad5ef 507
Christopher Haster 1:24750b9ad5ef 508 int mbedtls_memory_buffer_alloc_verify()
Christopher Haster 1:24750b9ad5ef 509 {
Christopher Haster 1:24750b9ad5ef 510 return verify_chain();
Christopher Haster 1:24750b9ad5ef 511 }
Christopher Haster 1:24750b9ad5ef 512
Christopher Haster 1:24750b9ad5ef 513 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 514 void mbedtls_memory_buffer_alloc_status()
Christopher Haster 1:24750b9ad5ef 515 {
Christopher Haster 1:24750b9ad5ef 516 mbedtls_fprintf( stderr,
Christopher Haster 1:24750b9ad5ef 517 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Christopher Haster 1:24750b9ad5ef 518 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Christopher Haster 1:24750b9ad5ef 519 heap.header_count, heap.total_used,
Christopher Haster 1:24750b9ad5ef 520 heap.maximum_header_count, heap.maximum_used,
Christopher Haster 1:24750b9ad5ef 521 heap.maximum_header_count * sizeof( memory_header )
Christopher Haster 1:24750b9ad5ef 522 + heap.maximum_used,
Christopher Haster 1:24750b9ad5ef 523 heap.alloc_count, heap.free_count );
Christopher Haster 1:24750b9ad5ef 524
Christopher Haster 1:24750b9ad5ef 525 if( heap.first->next == NULL )
Christopher Haster 1:24750b9ad5ef 526 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Christopher Haster 1:24750b9ad5ef 527 else
Christopher Haster 1:24750b9ad5ef 528 {
Christopher Haster 1:24750b9ad5ef 529 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Christopher Haster 1:24750b9ad5ef 530 debug_chain();
Christopher Haster 1:24750b9ad5ef 531 }
Christopher Haster 1:24750b9ad5ef 532 }
Christopher Haster 1:24750b9ad5ef 533
Christopher Haster 1:24750b9ad5ef 534 void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Christopher Haster 1:24750b9ad5ef 535 {
Christopher Haster 1:24750b9ad5ef 536 *max_used = heap.maximum_used;
Christopher Haster 1:24750b9ad5ef 537 *max_blocks = heap.maximum_header_count;
Christopher Haster 1:24750b9ad5ef 538 }
Christopher Haster 1:24750b9ad5ef 539
Christopher Haster 1:24750b9ad5ef 540 void mbedtls_memory_buffer_alloc_max_reset( void )
Christopher Haster 1:24750b9ad5ef 541 {
Christopher Haster 1:24750b9ad5ef 542 heap.maximum_used = 0;
Christopher Haster 1:24750b9ad5ef 543 heap.maximum_header_count = 0;
Christopher Haster 1:24750b9ad5ef 544 }
Christopher Haster 1:24750b9ad5ef 545
Christopher Haster 1:24750b9ad5ef 546 void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Christopher Haster 1:24750b9ad5ef 547 {
Christopher Haster 1:24750b9ad5ef 548 *cur_used = heap.total_used;
Christopher Haster 1:24750b9ad5ef 549 *cur_blocks = heap.header_count;
Christopher Haster 1:24750b9ad5ef 550 }
Christopher Haster 1:24750b9ad5ef 551 #endif /* MBEDTLS_MEMORY_DEBUG */
Christopher Haster 1:24750b9ad5ef 552
Christopher Haster 1:24750b9ad5ef 553 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 554 static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Christopher Haster 1:24750b9ad5ef 555 {
Christopher Haster 1:24750b9ad5ef 556 void *buf;
Christopher Haster 1:24750b9ad5ef 557 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
Christopher Haster 1:24750b9ad5ef 558 return( NULL );
Christopher Haster 1:24750b9ad5ef 559 buf = buffer_alloc_calloc( n, size );
Christopher Haster 1:24750b9ad5ef 560 if( mbedtls_mutex_unlock( &heap.mutex ) )
Christopher Haster 1:24750b9ad5ef 561 return( NULL );
Christopher Haster 1:24750b9ad5ef 562 return( buf );
Christopher Haster 1:24750b9ad5ef 563 }
Christopher Haster 1:24750b9ad5ef 564
Christopher Haster 1:24750b9ad5ef 565 static void buffer_alloc_free_mutexed( void *ptr )
Christopher Haster 1:24750b9ad5ef 566 {
Christopher Haster 1:24750b9ad5ef 567 /* We have to good option here, but corrupting the heap seems
Christopher Haster 1:24750b9ad5ef 568 * worse than loosing memory. */
Christopher Haster 1:24750b9ad5ef 569 if( mbedtls_mutex_lock( &heap.mutex ) )
Christopher Haster 1:24750b9ad5ef 570 return;
Christopher Haster 1:24750b9ad5ef 571 buffer_alloc_free( ptr );
Christopher Haster 1:24750b9ad5ef 572 (void) mbedtls_mutex_unlock( &heap.mutex );
Christopher Haster 1:24750b9ad5ef 573 }
Christopher Haster 1:24750b9ad5ef 574 #endif /* MBEDTLS_THREADING_C */
Christopher Haster 1:24750b9ad5ef 575
Christopher Haster 1:24750b9ad5ef 576 void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Christopher Haster 1:24750b9ad5ef 577 {
Christopher Haster 1:24750b9ad5ef 578 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
Christopher Haster 1:24750b9ad5ef 579 memset( buf, 0, len );
Christopher Haster 1:24750b9ad5ef 580
Christopher Haster 1:24750b9ad5ef 581 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 582 mbedtls_mutex_init( &heap.mutex );
Christopher Haster 1:24750b9ad5ef 583 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Christopher Haster 1:24750b9ad5ef 584 buffer_alloc_free_mutexed );
Christopher Haster 1:24750b9ad5ef 585 #else
Christopher Haster 1:24750b9ad5ef 586 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Christopher Haster 1:24750b9ad5ef 587 #endif
Christopher Haster 1:24750b9ad5ef 588
Christopher Haster 1:24750b9ad5ef 589 if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Christopher Haster 1:24750b9ad5ef 590 {
Christopher Haster 1:24750b9ad5ef 591 /* Adjust len first since buf is used in the computation */
Christopher Haster 1:24750b9ad5ef 592 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Christopher Haster 1:24750b9ad5ef 593 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Christopher Haster 1:24750b9ad5ef 594 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Christopher Haster 1:24750b9ad5ef 595 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Christopher Haster 1:24750b9ad5ef 596 }
Christopher Haster 1:24750b9ad5ef 597
Christopher Haster 1:24750b9ad5ef 598 heap.buf = buf;
Christopher Haster 1:24750b9ad5ef 599 heap.len = len;
Christopher Haster 1:24750b9ad5ef 600
Christopher Haster 1:24750b9ad5ef 601 heap.first = (memory_header *) buf;
Christopher Haster 1:24750b9ad5ef 602 heap.first->size = len - sizeof(memory_header);
Christopher Haster 1:24750b9ad5ef 603 heap.first->magic1 = MAGIC1;
Christopher Haster 1:24750b9ad5ef 604 heap.first->magic2 = MAGIC2;
Christopher Haster 1:24750b9ad5ef 605 heap.first_free = heap.first;
Christopher Haster 1:24750b9ad5ef 606 }
Christopher Haster 1:24750b9ad5ef 607
Christopher Haster 1:24750b9ad5ef 608 void mbedtls_memory_buffer_alloc_free()
Christopher Haster 1:24750b9ad5ef 609 {
Christopher Haster 1:24750b9ad5ef 610 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 611 mbedtls_mutex_free( &heap.mutex );
Christopher Haster 1:24750b9ad5ef 612 #endif
Christopher Haster 1:24750b9ad5ef 613 mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Christopher Haster 1:24750b9ad5ef 614 }
Christopher Haster 1:24750b9ad5ef 615
Christopher Haster 1:24750b9ad5ef 616 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 617 static int check_pointer( void *p )
Christopher Haster 1:24750b9ad5ef 618 {
Christopher Haster 1:24750b9ad5ef 619 if( p == NULL )
Christopher Haster 1:24750b9ad5ef 620 return( -1 );
Christopher Haster 1:24750b9ad5ef 621
Christopher Haster 1:24750b9ad5ef 622 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Christopher Haster 1:24750b9ad5ef 623 return( -1 );
Christopher Haster 1:24750b9ad5ef 624
Christopher Haster 1:24750b9ad5ef 625 return( 0 );
Christopher Haster 1:24750b9ad5ef 626 }
Christopher Haster 1:24750b9ad5ef 627
Christopher Haster 1:24750b9ad5ef 628 static int check_all_free( )
Christopher Haster 1:24750b9ad5ef 629 {
Christopher Haster 1:24750b9ad5ef 630 if(
Christopher Haster 1:24750b9ad5ef 631 #if defined(MBEDTLS_MEMORY_DEBUG)
Christopher Haster 1:24750b9ad5ef 632 heap.total_used != 0 ||
Christopher Haster 1:24750b9ad5ef 633 #endif
Christopher Haster 1:24750b9ad5ef 634 heap.first != heap.first_free ||
Christopher Haster 1:24750b9ad5ef 635 (void *) heap.first != (void *) heap.buf )
Christopher Haster 1:24750b9ad5ef 636 {
Christopher Haster 1:24750b9ad5ef 637 return( -1 );
Christopher Haster 1:24750b9ad5ef 638 }
Christopher Haster 1:24750b9ad5ef 639
Christopher Haster 1:24750b9ad5ef 640 return( 0 );
Christopher Haster 1:24750b9ad5ef 641 }
Christopher Haster 1:24750b9ad5ef 642
Christopher Haster 1:24750b9ad5ef 643 #define TEST_ASSERT( condition ) \
Christopher Haster 1:24750b9ad5ef 644 if( ! (condition) ) \
Christopher Haster 1:24750b9ad5ef 645 { \
Christopher Haster 1:24750b9ad5ef 646 if( verbose != 0 ) \
Christopher Haster 1:24750b9ad5ef 647 mbedtls_printf( "failed\n" ); \
Christopher Haster 1:24750b9ad5ef 648 \
Christopher Haster 1:24750b9ad5ef 649 ret = 1; \
Christopher Haster 1:24750b9ad5ef 650 goto cleanup; \
Christopher Haster 1:24750b9ad5ef 651 }
Christopher Haster 1:24750b9ad5ef 652
Christopher Haster 1:24750b9ad5ef 653 int mbedtls_memory_buffer_alloc_self_test( int verbose )
Christopher Haster 1:24750b9ad5ef 654 {
Christopher Haster 1:24750b9ad5ef 655 unsigned char buf[1024];
Christopher Haster 1:24750b9ad5ef 656 unsigned char *p, *q, *r, *end;
Christopher Haster 1:24750b9ad5ef 657 int ret = 0;
Christopher Haster 1:24750b9ad5ef 658
Christopher Haster 1:24750b9ad5ef 659 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 660 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
Christopher Haster 1:24750b9ad5ef 661
Christopher Haster 1:24750b9ad5ef 662 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Christopher Haster 1:24750b9ad5ef 663
Christopher Haster 1:24750b9ad5ef 664 p = mbedtls_calloc( 1, 1 );
Christopher Haster 1:24750b9ad5ef 665 q = mbedtls_calloc( 1, 128 );
Christopher Haster 1:24750b9ad5ef 666 r = mbedtls_calloc( 1, 16 );
Christopher Haster 1:24750b9ad5ef 667
Christopher Haster 1:24750b9ad5ef 668 TEST_ASSERT( check_pointer( p ) == 0 &&
Christopher Haster 1:24750b9ad5ef 669 check_pointer( q ) == 0 &&
Christopher Haster 1:24750b9ad5ef 670 check_pointer( r ) == 0 );
Christopher Haster 1:24750b9ad5ef 671
Christopher Haster 1:24750b9ad5ef 672 mbedtls_free( r );
Christopher Haster 1:24750b9ad5ef 673 mbedtls_free( q );
Christopher Haster 1:24750b9ad5ef 674 mbedtls_free( p );
Christopher Haster 1:24750b9ad5ef 675
Christopher Haster 1:24750b9ad5ef 676 TEST_ASSERT( check_all_free( ) == 0 );
Christopher Haster 1:24750b9ad5ef 677
Christopher Haster 1:24750b9ad5ef 678 /* Memorize end to compare with the next test */
Christopher Haster 1:24750b9ad5ef 679 end = heap.buf + heap.len;
Christopher Haster 1:24750b9ad5ef 680
Christopher Haster 1:24750b9ad5ef 681 mbedtls_memory_buffer_alloc_free( );
Christopher Haster 1:24750b9ad5ef 682
Christopher Haster 1:24750b9ad5ef 683 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 684 mbedtls_printf( "passed\n" );
Christopher Haster 1:24750b9ad5ef 685
Christopher Haster 1:24750b9ad5ef 686 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 687 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Christopher Haster 1:24750b9ad5ef 688
Christopher Haster 1:24750b9ad5ef 689 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Christopher Haster 1:24750b9ad5ef 690
Christopher Haster 1:24750b9ad5ef 691 TEST_ASSERT( heap.buf + heap.len == end );
Christopher Haster 1:24750b9ad5ef 692
Christopher Haster 1:24750b9ad5ef 693 p = mbedtls_calloc( 1, 1 );
Christopher Haster 1:24750b9ad5ef 694 q = mbedtls_calloc( 1, 128 );
Christopher Haster 1:24750b9ad5ef 695 r = mbedtls_calloc( 1, 16 );
Christopher Haster 1:24750b9ad5ef 696
Christopher Haster 1:24750b9ad5ef 697 TEST_ASSERT( check_pointer( p ) == 0 &&
Christopher Haster 1:24750b9ad5ef 698 check_pointer( q ) == 0 &&
Christopher Haster 1:24750b9ad5ef 699 check_pointer( r ) == 0 );
Christopher Haster 1:24750b9ad5ef 700
Christopher Haster 1:24750b9ad5ef 701 mbedtls_free( r );
Christopher Haster 1:24750b9ad5ef 702 mbedtls_free( q );
Christopher Haster 1:24750b9ad5ef 703 mbedtls_free( p );
Christopher Haster 1:24750b9ad5ef 704
Christopher Haster 1:24750b9ad5ef 705 TEST_ASSERT( check_all_free( ) == 0 );
Christopher Haster 1:24750b9ad5ef 706
Christopher Haster 1:24750b9ad5ef 707 mbedtls_memory_buffer_alloc_free( );
Christopher Haster 1:24750b9ad5ef 708
Christopher Haster 1:24750b9ad5ef 709 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 710 mbedtls_printf( "passed\n" );
Christopher Haster 1:24750b9ad5ef 711
Christopher Haster 1:24750b9ad5ef 712 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 713 mbedtls_printf( " MBA test #3 (full): " );
Christopher Haster 1:24750b9ad5ef 714
Christopher Haster 1:24750b9ad5ef 715 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Christopher Haster 1:24750b9ad5ef 716
Christopher Haster 1:24750b9ad5ef 717 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Christopher Haster 1:24750b9ad5ef 718
Christopher Haster 1:24750b9ad5ef 719 TEST_ASSERT( check_pointer( p ) == 0 );
Christopher Haster 1:24750b9ad5ef 720 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Christopher Haster 1:24750b9ad5ef 721
Christopher Haster 1:24750b9ad5ef 722 mbedtls_free( p );
Christopher Haster 1:24750b9ad5ef 723
Christopher Haster 1:24750b9ad5ef 724 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
Christopher Haster 1:24750b9ad5ef 725 q = mbedtls_calloc( 1, 16 );
Christopher Haster 1:24750b9ad5ef 726
Christopher Haster 1:24750b9ad5ef 727 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Christopher Haster 1:24750b9ad5ef 728 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Christopher Haster 1:24750b9ad5ef 729
Christopher Haster 1:24750b9ad5ef 730 mbedtls_free( q );
Christopher Haster 1:24750b9ad5ef 731
Christopher Haster 1:24750b9ad5ef 732 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Christopher Haster 1:24750b9ad5ef 733
Christopher Haster 1:24750b9ad5ef 734 mbedtls_free( p );
Christopher Haster 1:24750b9ad5ef 735
Christopher Haster 1:24750b9ad5ef 736 TEST_ASSERT( check_all_free( ) == 0 );
Christopher Haster 1:24750b9ad5ef 737
Christopher Haster 1:24750b9ad5ef 738 mbedtls_memory_buffer_alloc_free( );
Christopher Haster 1:24750b9ad5ef 739
Christopher Haster 1:24750b9ad5ef 740 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 741 mbedtls_printf( "passed\n" );
Christopher Haster 1:24750b9ad5ef 742
Christopher Haster 1:24750b9ad5ef 743 cleanup:
Christopher Haster 1:24750b9ad5ef 744 mbedtls_memory_buffer_alloc_free( );
Christopher Haster 1:24750b9ad5ef 745
Christopher Haster 1:24750b9ad5ef 746 return( ret );
Christopher Haster 1:24750b9ad5ef 747 }
Christopher Haster 1:24750b9ad5ef 748 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 749
Christopher Haster 1:24750b9ad5ef 750 #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */