Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/entropy.h@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file entropy.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief Entropy accumulator implementation |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | #ifndef POLARSSL_ENTROPY_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_ENTROPY_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 33 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 34 | #else |
HannesTschofenig | 0:796d0f61a05b | 35 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 36 | #endif |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include "sha512.h" |
HannesTschofenig | 0:796d0f61a05b | 40 | #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #if defined(POLARSSL_SHA256_C) |
HannesTschofenig | 0:796d0f61a05b | 43 | #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR |
HannesTschofenig | 0:796d0f61a05b | 44 | #include "sha256.h" |
HannesTschofenig | 0:796d0f61a05b | 45 | #endif |
HannesTschofenig | 0:796d0f61a05b | 46 | #endif |
HannesTschofenig | 0:796d0f61a05b | 47 | |
HannesTschofenig | 0:796d0f61a05b | 48 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 49 | #include "threading.h" |
HannesTschofenig | 0:796d0f61a05b | 50 | #endif |
HannesTschofenig | 0:796d0f61a05b | 51 | |
HannesTschofenig | 0:796d0f61a05b | 52 | #if defined(POLARSSL_HAVEGE_C) |
HannesTschofenig | 0:796d0f61a05b | 53 | #include "havege.h" |
HannesTschofenig | 0:796d0f61a05b | 54 | #endif |
HannesTschofenig | 0:796d0f61a05b | 55 | |
HannesTschofenig | 0:796d0f61a05b | 56 | #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ |
HannesTschofenig | 0:796d0f61a05b | 57 | #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */ |
HannesTschofenig | 0:796d0f61a05b | 58 | #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ |
HannesTschofenig | 0:796d0f61a05b | 59 | #define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */ |
HannesTschofenig | 0:796d0f61a05b | 60 | |
HannesTschofenig | 0:796d0f61a05b | 61 | /** |
HannesTschofenig | 0:796d0f61a05b | 62 | * \name SECTION: Module settings |
HannesTschofenig | 0:796d0f61a05b | 63 | * |
HannesTschofenig | 0:796d0f61a05b | 64 | * The configuration options you can set for this module are in this section. |
HannesTschofenig | 0:796d0f61a05b | 65 | * Either change them in config.h or define them on the compiler command line. |
HannesTschofenig | 0:796d0f61a05b | 66 | * \{ |
HannesTschofenig | 0:796d0f61a05b | 67 | */ |
HannesTschofenig | 0:796d0f61a05b | 68 | |
HannesTschofenig | 0:796d0f61a05b | 69 | #if !defined(ENTROPY_MAX_SOURCES) |
HannesTschofenig | 0:796d0f61a05b | 70 | #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ |
HannesTschofenig | 0:796d0f61a05b | 71 | #endif |
HannesTschofenig | 0:796d0f61a05b | 72 | |
HannesTschofenig | 0:796d0f61a05b | 73 | #if !defined(ENTROPY_MAX_GATHER) |
HannesTschofenig | 0:796d0f61a05b | 74 | #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ |
HannesTschofenig | 0:796d0f61a05b | 75 | #endif |
HannesTschofenig | 0:796d0f61a05b | 76 | |
HannesTschofenig | 0:796d0f61a05b | 77 | /* \} name SECTION: Module settings */ |
HannesTschofenig | 0:796d0f61a05b | 78 | |
HannesTschofenig | 0:796d0f61a05b | 79 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
HannesTschofenig | 0:796d0f61a05b | 80 | #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ |
HannesTschofenig | 0:796d0f61a05b | 81 | #else |
HannesTschofenig | 0:796d0f61a05b | 82 | #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ |
HannesTschofenig | 0:796d0f61a05b | 83 | #endif |
HannesTschofenig | 0:796d0f61a05b | 84 | |
HannesTschofenig | 0:796d0f61a05b | 85 | #define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ |
HannesTschofenig | 0:796d0f61a05b | 86 | #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES |
HannesTschofenig | 0:796d0f61a05b | 87 | |
HannesTschofenig | 0:796d0f61a05b | 88 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 89 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 90 | #endif |
HannesTschofenig | 0:796d0f61a05b | 91 | |
HannesTschofenig | 0:796d0f61a05b | 92 | /** |
HannesTschofenig | 0:796d0f61a05b | 93 | * \brief Entropy poll callback pointer |
HannesTschofenig | 0:796d0f61a05b | 94 | * |
HannesTschofenig | 0:796d0f61a05b | 95 | * \param data Callback-specific data pointer |
HannesTschofenig | 0:796d0f61a05b | 96 | * \param output Data to fill |
HannesTschofenig | 0:796d0f61a05b | 97 | * \param len Maximum size to provide |
HannesTschofenig | 0:796d0f61a05b | 98 | * \param olen The actual amount of bytes put into the buffer (Can be 0) |
HannesTschofenig | 0:796d0f61a05b | 99 | * |
HannesTschofenig | 0:796d0f61a05b | 100 | * \return 0 if no critical failures occurred, |
HannesTschofenig | 0:796d0f61a05b | 101 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise |
HannesTschofenig | 0:796d0f61a05b | 102 | */ |
HannesTschofenig | 0:796d0f61a05b | 103 | typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len, |
HannesTschofenig | 0:796d0f61a05b | 104 | size_t *olen); |
HannesTschofenig | 0:796d0f61a05b | 105 | |
HannesTschofenig | 0:796d0f61a05b | 106 | /** |
HannesTschofenig | 0:796d0f61a05b | 107 | * \brief Entropy source state |
HannesTschofenig | 0:796d0f61a05b | 108 | */ |
HannesTschofenig | 0:796d0f61a05b | 109 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 110 | { |
HannesTschofenig | 0:796d0f61a05b | 111 | f_source_ptr f_source; /**< The entropy source callback */ |
HannesTschofenig | 0:796d0f61a05b | 112 | void * p_source; /**< The callback data pointer */ |
HannesTschofenig | 0:796d0f61a05b | 113 | size_t size; /**< Amount received */ |
HannesTschofenig | 0:796d0f61a05b | 114 | size_t threshold; /**< Minimum level required before release */ |
HannesTschofenig | 0:796d0f61a05b | 115 | } |
HannesTschofenig | 0:796d0f61a05b | 116 | source_state; |
HannesTschofenig | 0:796d0f61a05b | 117 | |
HannesTschofenig | 0:796d0f61a05b | 118 | /** |
HannesTschofenig | 0:796d0f61a05b | 119 | * \brief Entropy context structure |
HannesTschofenig | 0:796d0f61a05b | 120 | */ |
HannesTschofenig | 0:796d0f61a05b | 121 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 122 | { |
HannesTschofenig | 0:796d0f61a05b | 123 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
HannesTschofenig | 0:796d0f61a05b | 124 | sha512_context accumulator; |
HannesTschofenig | 0:796d0f61a05b | 125 | #else |
HannesTschofenig | 0:796d0f61a05b | 126 | sha256_context accumulator; |
HannesTschofenig | 0:796d0f61a05b | 127 | #endif |
HannesTschofenig | 0:796d0f61a05b | 128 | int source_count; |
HannesTschofenig | 0:796d0f61a05b | 129 | source_state source[ENTROPY_MAX_SOURCES]; |
HannesTschofenig | 0:796d0f61a05b | 130 | #if defined(POLARSSL_HAVEGE_C) |
HannesTschofenig | 0:796d0f61a05b | 131 | havege_state havege_data; |
HannesTschofenig | 0:796d0f61a05b | 132 | #endif |
HannesTschofenig | 0:796d0f61a05b | 133 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 134 | threading_mutex_t mutex; /*!< mutex */ |
HannesTschofenig | 0:796d0f61a05b | 135 | #endif |
HannesTschofenig | 0:796d0f61a05b | 136 | } |
HannesTschofenig | 0:796d0f61a05b | 137 | entropy_context; |
HannesTschofenig | 0:796d0f61a05b | 138 | |
HannesTschofenig | 0:796d0f61a05b | 139 | /** |
HannesTschofenig | 0:796d0f61a05b | 140 | * \brief Initialize the context |
HannesTschofenig | 0:796d0f61a05b | 141 | * |
HannesTschofenig | 0:796d0f61a05b | 142 | * \param ctx Entropy context to initialize |
HannesTschofenig | 0:796d0f61a05b | 143 | */ |
HannesTschofenig | 0:796d0f61a05b | 144 | void entropy_init( entropy_context *ctx ); |
HannesTschofenig | 0:796d0f61a05b | 145 | |
HannesTschofenig | 0:796d0f61a05b | 146 | /** |
HannesTschofenig | 0:796d0f61a05b | 147 | * \brief Free the data in the context |
HannesTschofenig | 0:796d0f61a05b | 148 | * |
HannesTschofenig | 0:796d0f61a05b | 149 | * \param ctx Entropy context to free |
HannesTschofenig | 0:796d0f61a05b | 150 | */ |
HannesTschofenig | 0:796d0f61a05b | 151 | void entropy_free( entropy_context *ctx ); |
HannesTschofenig | 0:796d0f61a05b | 152 | |
HannesTschofenig | 0:796d0f61a05b | 153 | /** |
HannesTschofenig | 0:796d0f61a05b | 154 | * \brief Adds an entropy source to poll |
HannesTschofenig | 0:796d0f61a05b | 155 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
HannesTschofenig | 0:796d0f61a05b | 156 | * |
HannesTschofenig | 0:796d0f61a05b | 157 | * \param ctx Entropy context |
HannesTschofenig | 0:796d0f61a05b | 158 | * \param f_source Entropy function |
HannesTschofenig | 0:796d0f61a05b | 159 | * \param p_source Function data |
HannesTschofenig | 0:796d0f61a05b | 160 | * \param threshold Minimum required from source before entropy is released |
HannesTschofenig | 0:796d0f61a05b | 161 | * ( with entropy_func() ) |
HannesTschofenig | 0:796d0f61a05b | 162 | * |
HannesTschofenig | 0:796d0f61a05b | 163 | * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES |
HannesTschofenig | 0:796d0f61a05b | 164 | */ |
HannesTschofenig | 0:796d0f61a05b | 165 | int entropy_add_source( entropy_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 166 | f_source_ptr f_source, void *p_source, |
HannesTschofenig | 0:796d0f61a05b | 167 | size_t threshold ); |
HannesTschofenig | 0:796d0f61a05b | 168 | |
HannesTschofenig | 0:796d0f61a05b | 169 | /** |
HannesTschofenig | 0:796d0f61a05b | 170 | * \brief Trigger an extra gather poll for the accumulator |
HannesTschofenig | 0:796d0f61a05b | 171 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
HannesTschofenig | 0:796d0f61a05b | 172 | * |
HannesTschofenig | 0:796d0f61a05b | 173 | * \param ctx Entropy context |
HannesTschofenig | 0:796d0f61a05b | 174 | * |
HannesTschofenig | 0:796d0f61a05b | 175 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
HannesTschofenig | 0:796d0f61a05b | 176 | */ |
HannesTschofenig | 0:796d0f61a05b | 177 | int entropy_gather( entropy_context *ctx ); |
HannesTschofenig | 0:796d0f61a05b | 178 | |
HannesTschofenig | 0:796d0f61a05b | 179 | /** |
HannesTschofenig | 0:796d0f61a05b | 180 | * \brief Retrieve entropy from the accumulator |
HannesTschofenig | 0:796d0f61a05b | 181 | * (Maximum length: ENTROPY_BLOCK_SIZE) |
HannesTschofenig | 0:796d0f61a05b | 182 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
HannesTschofenig | 0:796d0f61a05b | 183 | * |
HannesTschofenig | 0:796d0f61a05b | 184 | * \param data Entropy context |
HannesTschofenig | 0:796d0f61a05b | 185 | * \param output Buffer to fill |
HannesTschofenig | 0:796d0f61a05b | 186 | * \param len Length of buffer |
HannesTschofenig | 0:796d0f61a05b | 187 | * |
HannesTschofenig | 0:796d0f61a05b | 188 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
HannesTschofenig | 0:796d0f61a05b | 189 | */ |
HannesTschofenig | 0:796d0f61a05b | 190 | int entropy_func( void *data, unsigned char *output, size_t len ); |
HannesTschofenig | 0:796d0f61a05b | 191 | |
HannesTschofenig | 0:796d0f61a05b | 192 | /** |
HannesTschofenig | 0:796d0f61a05b | 193 | * \brief Add data to the accumulator manually |
HannesTschofenig | 0:796d0f61a05b | 194 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
HannesTschofenig | 0:796d0f61a05b | 195 | * |
HannesTschofenig | 0:796d0f61a05b | 196 | * \param ctx Entropy context |
HannesTschofenig | 0:796d0f61a05b | 197 | * \param data Data to add |
HannesTschofenig | 0:796d0f61a05b | 198 | * \param len Length of data |
HannesTschofenig | 0:796d0f61a05b | 199 | * |
HannesTschofenig | 0:796d0f61a05b | 200 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 201 | */ |
HannesTschofenig | 0:796d0f61a05b | 202 | int entropy_update_manual( entropy_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 203 | const unsigned char *data, size_t len ); |
HannesTschofenig | 0:796d0f61a05b | 204 | |
HannesTschofenig | 0:796d0f61a05b | 205 | #if defined(POLARSSL_FS_IO) |
HannesTschofenig | 0:796d0f61a05b | 206 | /** |
HannesTschofenig | 0:796d0f61a05b | 207 | * \brief Write a seed file |
HannesTschofenig | 0:796d0f61a05b | 208 | * |
HannesTschofenig | 0:796d0f61a05b | 209 | * \param ctx Entropy context |
HannesTschofenig | 0:796d0f61a05b | 210 | * \param path Name of the file |
HannesTschofenig | 0:796d0f61a05b | 211 | * |
HannesTschofenig | 0:796d0f61a05b | 212 | * \return 0 if successful, |
HannesTschofenig | 0:796d0f61a05b | 213 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or |
HannesTschofenig | 0:796d0f61a05b | 214 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
HannesTschofenig | 0:796d0f61a05b | 215 | */ |
HannesTschofenig | 0:796d0f61a05b | 216 | int entropy_write_seed_file( entropy_context *ctx, const char *path ); |
HannesTschofenig | 0:796d0f61a05b | 217 | |
HannesTschofenig | 0:796d0f61a05b | 218 | /** |
HannesTschofenig | 0:796d0f61a05b | 219 | * \brief Read and update a seed file. Seed is added to this |
HannesTschofenig | 0:796d0f61a05b | 220 | * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are |
HannesTschofenig | 0:796d0f61a05b | 221 | * read from the seed file. The rest is ignored. |
HannesTschofenig | 0:796d0f61a05b | 222 | * |
HannesTschofenig | 0:796d0f61a05b | 223 | * \param ctx Entropy context |
HannesTschofenig | 0:796d0f61a05b | 224 | * \param path Name of the file |
HannesTschofenig | 0:796d0f61a05b | 225 | * |
HannesTschofenig | 0:796d0f61a05b | 226 | * \return 0 if successful, |
HannesTschofenig | 0:796d0f61a05b | 227 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, |
HannesTschofenig | 0:796d0f61a05b | 228 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
HannesTschofenig | 0:796d0f61a05b | 229 | */ |
HannesTschofenig | 0:796d0f61a05b | 230 | int entropy_update_seed_file( entropy_context *ctx, const char *path ); |
HannesTschofenig | 0:796d0f61a05b | 231 | #endif /* POLARSSL_FS_IO */ |
HannesTschofenig | 0:796d0f61a05b | 232 | |
HannesTschofenig | 0:796d0f61a05b | 233 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 234 | } |
HannesTschofenig | 0:796d0f61a05b | 235 | #endif |
HannesTschofenig | 0:796d0f61a05b | 236 | |
HannesTschofenig | 0:796d0f61a05b | 237 | #endif /* entropy.h */ |
HannesTschofenig | 0:796d0f61a05b | 238 | |
HannesTschofenig | 0:796d0f61a05b | 239 |