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:
electronichamsters
Date:
Mon Jul 10 04:00:25 2017 +0000
Revision:
5:f09f5ed830ca
Parent:
1:24750b9ad5ef
working gateway

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:24750b9ad5ef 1 /**
Christopher Haster 1:24750b9ad5ef 2 * \file entropy.h
Christopher Haster 1:24750b9ad5ef 3 *
Christopher Haster 1:24750b9ad5ef 4 * \brief Entropy accumulator implementation
Christopher Haster 1:24750b9ad5ef 5 *
Christopher Haster 1:24750b9ad5ef 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Christopher Haster 1:24750b9ad5ef 7 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:24750b9ad5ef 8 *
Christopher Haster 1:24750b9ad5ef 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Christopher Haster 1:24750b9ad5ef 10 * not use this file except in compliance with the License.
Christopher Haster 1:24750b9ad5ef 11 * You may obtain a copy of the License at
Christopher Haster 1:24750b9ad5ef 12 *
Christopher Haster 1:24750b9ad5ef 13 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:24750b9ad5ef 14 *
Christopher Haster 1:24750b9ad5ef 15 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:24750b9ad5ef 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Christopher Haster 1:24750b9ad5ef 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:24750b9ad5ef 18 * See the License for the specific language governing permissions and
Christopher Haster 1:24750b9ad5ef 19 * limitations under the License.
Christopher Haster 1:24750b9ad5ef 20 *
Christopher Haster 1:24750b9ad5ef 21 * This file is part of mbed TLS (https://tls.mbed.org)
Christopher Haster 1:24750b9ad5ef 22 */
Christopher Haster 1:24750b9ad5ef 23 #ifndef MBEDTLS_ENTROPY_H
Christopher Haster 1:24750b9ad5ef 24 #define MBEDTLS_ENTROPY_H
Christopher Haster 1:24750b9ad5ef 25
Christopher Haster 1:24750b9ad5ef 26 #if !defined(MBEDTLS_CONFIG_FILE)
Christopher Haster 1:24750b9ad5ef 27 #include "config.h"
Christopher Haster 1:24750b9ad5ef 28 #else
Christopher Haster 1:24750b9ad5ef 29 #include MBEDTLS_CONFIG_FILE
Christopher Haster 1:24750b9ad5ef 30 #endif
Christopher Haster 1:24750b9ad5ef 31
Christopher Haster 1:24750b9ad5ef 32 #include <stddef.h>
Christopher Haster 1:24750b9ad5ef 33
Christopher Haster 1:24750b9ad5ef 34 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Christopher Haster 1:24750b9ad5ef 35 #include "sha512.h"
Christopher Haster 1:24750b9ad5ef 36 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
Christopher Haster 1:24750b9ad5ef 37 #else
Christopher Haster 1:24750b9ad5ef 38 #if defined(MBEDTLS_SHA256_C)
Christopher Haster 1:24750b9ad5ef 39 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
Christopher Haster 1:24750b9ad5ef 40 #include "sha256.h"
Christopher Haster 1:24750b9ad5ef 41 #endif
Christopher Haster 1:24750b9ad5ef 42 #endif
Christopher Haster 1:24750b9ad5ef 43
Christopher Haster 1:24750b9ad5ef 44 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 45 #include "threading.h"
Christopher Haster 1:24750b9ad5ef 46 #endif
Christopher Haster 1:24750b9ad5ef 47
Christopher Haster 1:24750b9ad5ef 48 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 49 #include "havege.h"
Christopher Haster 1:24750b9ad5ef 50 #endif
Christopher Haster 1:24750b9ad5ef 51
Christopher Haster 1:24750b9ad5ef 52 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
Christopher Haster 1:24750b9ad5ef 53 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
Christopher Haster 1:24750b9ad5ef 54 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Christopher Haster 1:24750b9ad5ef 55 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D /**< No strong sources have been added to poll. */
Christopher Haster 1:24750b9ad5ef 56 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F /**< Read/write error in file. */
Christopher Haster 1:24750b9ad5ef 57
Christopher Haster 1:24750b9ad5ef 58 /**
Christopher Haster 1:24750b9ad5ef 59 * \name SECTION: Module settings
Christopher Haster 1:24750b9ad5ef 60 *
Christopher Haster 1:24750b9ad5ef 61 * The configuration options you can set for this module are in this section.
Christopher Haster 1:24750b9ad5ef 62 * Either change them in config.h or define them on the compiler command line.
Christopher Haster 1:24750b9ad5ef 63 * \{
Christopher Haster 1:24750b9ad5ef 64 */
Christopher Haster 1:24750b9ad5ef 65
Christopher Haster 1:24750b9ad5ef 66 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
Christopher Haster 1:24750b9ad5ef 67 #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Christopher Haster 1:24750b9ad5ef 68 #endif
Christopher Haster 1:24750b9ad5ef 69
Christopher Haster 1:24750b9ad5ef 70 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
Christopher Haster 1:24750b9ad5ef 71 #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Christopher Haster 1:24750b9ad5ef 72 #endif
Christopher Haster 1:24750b9ad5ef 73
Christopher Haster 1:24750b9ad5ef 74 /* \} name SECTION: Module settings */
Christopher Haster 1:24750b9ad5ef 75
Christopher Haster 1:24750b9ad5ef 76 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 77 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Christopher Haster 1:24750b9ad5ef 78 #else
Christopher Haster 1:24750b9ad5ef 79 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
Christopher Haster 1:24750b9ad5ef 80 #endif
Christopher Haster 1:24750b9ad5ef 81
Christopher Haster 1:24750b9ad5ef 82 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Christopher Haster 1:24750b9ad5ef 83 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
Christopher Haster 1:24750b9ad5ef 84
Christopher Haster 1:24750b9ad5ef 85 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */
Christopher Haster 1:24750b9ad5ef 86 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */
Christopher Haster 1:24750b9ad5ef 87
Christopher Haster 1:24750b9ad5ef 88 #ifdef __cplusplus
Christopher Haster 1:24750b9ad5ef 89 extern "C" {
Christopher Haster 1:24750b9ad5ef 90 #endif
Christopher Haster 1:24750b9ad5ef 91
Christopher Haster 1:24750b9ad5ef 92 /**
Christopher Haster 1:24750b9ad5ef 93 * \brief Entropy poll callback pointer
Christopher Haster 1:24750b9ad5ef 94 *
Christopher Haster 1:24750b9ad5ef 95 * \param data Callback-specific data pointer
Christopher Haster 1:24750b9ad5ef 96 * \param output Data to fill
Christopher Haster 1:24750b9ad5ef 97 * \param len Maximum size to provide
Christopher Haster 1:24750b9ad5ef 98 * \param olen The actual amount of bytes put into the buffer (Can be 0)
Christopher Haster 1:24750b9ad5ef 99 *
Christopher Haster 1:24750b9ad5ef 100 * \return 0 if no critical failures occurred,
Christopher Haster 1:24750b9ad5ef 101 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
Christopher Haster 1:24750b9ad5ef 102 */
Christopher Haster 1:24750b9ad5ef 103 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
Christopher Haster 1:24750b9ad5ef 104 size_t *olen);
Christopher Haster 1:24750b9ad5ef 105
Christopher Haster 1:24750b9ad5ef 106 /**
Christopher Haster 1:24750b9ad5ef 107 * \brief Entropy source state
Christopher Haster 1:24750b9ad5ef 108 */
Christopher Haster 1:24750b9ad5ef 109 typedef struct
Christopher Haster 1:24750b9ad5ef 110 {
Christopher Haster 1:24750b9ad5ef 111 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
Christopher Haster 1:24750b9ad5ef 112 void * p_source; /**< The callback data pointer */
Christopher Haster 1:24750b9ad5ef 113 size_t size; /**< Amount received in bytes */
Christopher Haster 1:24750b9ad5ef 114 size_t threshold; /**< Minimum bytes required before release */
Christopher Haster 1:24750b9ad5ef 115 int strong; /**< Is the source strong? */
Christopher Haster 1:24750b9ad5ef 116 }
Christopher Haster 1:24750b9ad5ef 117 mbedtls_entropy_source_state;
Christopher Haster 1:24750b9ad5ef 118
Christopher Haster 1:24750b9ad5ef 119 /**
Christopher Haster 1:24750b9ad5ef 120 * \brief Entropy context structure
Christopher Haster 1:24750b9ad5ef 121 */
Christopher Haster 1:24750b9ad5ef 122 typedef struct
Christopher Haster 1:24750b9ad5ef 123 {
Christopher Haster 1:24750b9ad5ef 124 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
Christopher Haster 1:24750b9ad5ef 125 mbedtls_sha512_context accumulator;
Christopher Haster 1:24750b9ad5ef 126 #else
Christopher Haster 1:24750b9ad5ef 127 mbedtls_sha256_context accumulator;
Christopher Haster 1:24750b9ad5ef 128 #endif
Christopher Haster 1:24750b9ad5ef 129 int source_count;
Christopher Haster 1:24750b9ad5ef 130 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
Christopher Haster 1:24750b9ad5ef 131 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 132 mbedtls_havege_state havege_data;
Christopher Haster 1:24750b9ad5ef 133 #endif
Christopher Haster 1:24750b9ad5ef 134 #if defined(MBEDTLS_THREADING_C)
Christopher Haster 1:24750b9ad5ef 135 mbedtls_threading_mutex_t mutex; /*!< mutex */
Christopher Haster 1:24750b9ad5ef 136 #endif
Christopher Haster 1:24750b9ad5ef 137 }
Christopher Haster 1:24750b9ad5ef 138 mbedtls_entropy_context;
Christopher Haster 1:24750b9ad5ef 139
Christopher Haster 1:24750b9ad5ef 140 /**
Christopher Haster 1:24750b9ad5ef 141 * \brief Initialize the context
Christopher Haster 1:24750b9ad5ef 142 *
Christopher Haster 1:24750b9ad5ef 143 * \param ctx Entropy context to initialize
Christopher Haster 1:24750b9ad5ef 144 */
Christopher Haster 1:24750b9ad5ef 145 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
Christopher Haster 1:24750b9ad5ef 146
Christopher Haster 1:24750b9ad5ef 147 /**
Christopher Haster 1:24750b9ad5ef 148 * \brief Free the data in the context
Christopher Haster 1:24750b9ad5ef 149 *
Christopher Haster 1:24750b9ad5ef 150 * \param ctx Entropy context to free
Christopher Haster 1:24750b9ad5ef 151 */
Christopher Haster 1:24750b9ad5ef 152 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
Christopher Haster 1:24750b9ad5ef 153
Christopher Haster 1:24750b9ad5ef 154 /**
Christopher Haster 1:24750b9ad5ef 155 * \brief Adds an entropy source to poll
Christopher Haster 1:24750b9ad5ef 156 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Christopher Haster 1:24750b9ad5ef 157 *
Christopher Haster 1:24750b9ad5ef 158 * \param ctx Entropy context
Christopher Haster 1:24750b9ad5ef 159 * \param f_source Entropy function
Christopher Haster 1:24750b9ad5ef 160 * \param p_source Function data
Christopher Haster 1:24750b9ad5ef 161 * \param threshold Minimum required from source before entropy is released
Christopher Haster 1:24750b9ad5ef 162 * ( with mbedtls_entropy_func() ) (in bytes)
Christopher Haster 1:24750b9ad5ef 163 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or
Christopher Haster 1:24750b9ad5ef 164 * MBEDTSL_ENTROPY_SOURCE_WEAK.
Christopher Haster 1:24750b9ad5ef 165 * At least one strong source needs to be added.
Christopher Haster 1:24750b9ad5ef 166 * Weaker sources (such as the cycle counter) can be used as
Christopher Haster 1:24750b9ad5ef 167 * a complement.
Christopher Haster 1:24750b9ad5ef 168 *
Christopher Haster 1:24750b9ad5ef 169 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
Christopher Haster 1:24750b9ad5ef 170 */
Christopher Haster 1:24750b9ad5ef 171 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
Christopher Haster 1:24750b9ad5ef 172 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Christopher Haster 1:24750b9ad5ef 173 size_t threshold, int strong );
Christopher Haster 1:24750b9ad5ef 174
Christopher Haster 1:24750b9ad5ef 175 /**
Christopher Haster 1:24750b9ad5ef 176 * \brief Trigger an extra gather poll for the accumulator
Christopher Haster 1:24750b9ad5ef 177 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Christopher Haster 1:24750b9ad5ef 178 *
Christopher Haster 1:24750b9ad5ef 179 * \param ctx Entropy context
Christopher Haster 1:24750b9ad5ef 180 *
Christopher Haster 1:24750b9ad5ef 181 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Christopher Haster 1:24750b9ad5ef 182 */
Christopher Haster 1:24750b9ad5ef 183 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
Christopher Haster 1:24750b9ad5ef 184
Christopher Haster 1:24750b9ad5ef 185 /**
Christopher Haster 1:24750b9ad5ef 186 * \brief Retrieve entropy from the accumulator
Christopher Haster 1:24750b9ad5ef 187 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
Christopher Haster 1:24750b9ad5ef 188 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Christopher Haster 1:24750b9ad5ef 189 *
Christopher Haster 1:24750b9ad5ef 190 * \param data Entropy context
Christopher Haster 1:24750b9ad5ef 191 * \param output Buffer to fill
Christopher Haster 1:24750b9ad5ef 192 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
Christopher Haster 1:24750b9ad5ef 193 *
Christopher Haster 1:24750b9ad5ef 194 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Christopher Haster 1:24750b9ad5ef 195 */
Christopher Haster 1:24750b9ad5ef 196 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
Christopher Haster 1:24750b9ad5ef 197
Christopher Haster 1:24750b9ad5ef 198 /**
Christopher Haster 1:24750b9ad5ef 199 * \brief Add data to the accumulator manually
Christopher Haster 1:24750b9ad5ef 200 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Christopher Haster 1:24750b9ad5ef 201 *
Christopher Haster 1:24750b9ad5ef 202 * \param ctx Entropy context
Christopher Haster 1:24750b9ad5ef 203 * \param data Data to add
Christopher Haster 1:24750b9ad5ef 204 * \param len Length of data
Christopher Haster 1:24750b9ad5ef 205 *
Christopher Haster 1:24750b9ad5ef 206 * \return 0 if successful
Christopher Haster 1:24750b9ad5ef 207 */
Christopher Haster 1:24750b9ad5ef 208 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Christopher Haster 1:24750b9ad5ef 209 const unsigned char *data, size_t len );
Christopher Haster 1:24750b9ad5ef 210
Christopher Haster 1:24750b9ad5ef 211 #if defined(MBEDTLS_FS_IO)
Christopher Haster 1:24750b9ad5ef 212 /**
Christopher Haster 1:24750b9ad5ef 213 * \brief Write a seed file
Christopher Haster 1:24750b9ad5ef 214 *
Christopher Haster 1:24750b9ad5ef 215 * \param ctx Entropy context
Christopher Haster 1:24750b9ad5ef 216 * \param path Name of the file
Christopher Haster 1:24750b9ad5ef 217 *
Christopher Haster 1:24750b9ad5ef 218 * \return 0 if successful,
Christopher Haster 1:24750b9ad5ef 219 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
Christopher Haster 1:24750b9ad5ef 220 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Christopher Haster 1:24750b9ad5ef 221 */
Christopher Haster 1:24750b9ad5ef 222 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
Christopher Haster 1:24750b9ad5ef 223
Christopher Haster 1:24750b9ad5ef 224 /**
Christopher Haster 1:24750b9ad5ef 225 * \brief Read and update a seed file. Seed is added to this
Christopher Haster 1:24750b9ad5ef 226 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
Christopher Haster 1:24750b9ad5ef 227 * read from the seed file. The rest is ignored.
Christopher Haster 1:24750b9ad5ef 228 *
Christopher Haster 1:24750b9ad5ef 229 * \param ctx Entropy context
Christopher Haster 1:24750b9ad5ef 230 * \param path Name of the file
Christopher Haster 1:24750b9ad5ef 231 *
Christopher Haster 1:24750b9ad5ef 232 * \return 0 if successful,
Christopher Haster 1:24750b9ad5ef 233 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
Christopher Haster 1:24750b9ad5ef 234 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Christopher Haster 1:24750b9ad5ef 235 */
Christopher Haster 1:24750b9ad5ef 236 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
Christopher Haster 1:24750b9ad5ef 237 #endif /* MBEDTLS_FS_IO */
Christopher Haster 1:24750b9ad5ef 238
Christopher Haster 1:24750b9ad5ef 239 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 240 /**
Christopher Haster 1:24750b9ad5ef 241 * \brief Checkup routine
Christopher Haster 1:24750b9ad5ef 242 *
Christopher Haster 1:24750b9ad5ef 243 * \return 0 if successful, or 1 if a test failed
Christopher Haster 1:24750b9ad5ef 244 */
Christopher Haster 1:24750b9ad5ef 245 int mbedtls_entropy_self_test( int verbose );
Christopher Haster 1:24750b9ad5ef 246 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 247
Christopher Haster 1:24750b9ad5ef 248 #ifdef __cplusplus
Christopher Haster 1:24750b9ad5ef 249 }
Christopher Haster 1:24750b9ad5ef 250 #endif
Christopher Haster 1:24750b9ad5ef 251
Christopher Haster 1:24750b9ad5ef 252 #endif /* entropy.h */