Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
Manuel Pégourié-Gonnard | 50518f4 | 2015-05-26 11:04:15 +0200 | [diff] [blame] | 2 | * \file md_internal.h |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 4 | * \brief Message digest wrappers. |
| 5 | * |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 6 | * \warning This in an internal header. Do not include directly. |
| 7 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 8 | * \author Adriaan de Jong <dejong@fox-it.com> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 9 | */ |
| 10 | /* |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 11 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * SPDX-License-Identifier: Apache-2.0 |
| 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | * not use this file except in compliance with the License. |
| 16 | * You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, software |
| 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | * See the License for the specific language governing permissions and |
| 24 | * limitations under the License. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 25 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 26 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 27 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #ifndef MBEDTLS_MD_WRAP_H |
| 29 | #define MBEDTLS_MD_WRAP_H |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 32 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 35 | #endif |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 37 | #include "md.h" |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 38 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Hanno Becker | 62a44d4 | 2019-07-17 14:14:01 +0100 | [diff] [blame] | 43 | /* |
| 44 | * Message-digest information macro definition |
| 45 | */ |
| 46 | |
| 47 | /* SHA-256 */ |
| 48 | #define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256 |
| 49 | #define MBEDTLS_MD_INFO_SHA256_NAME "SHA256" |
| 50 | #define MBEDTLS_MD_INFO_SHA256_SIZE 32 |
| 51 | #define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64 |
| 52 | #define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC sha256_starts_wrap |
| 53 | #define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC sha224_update_wrap |
| 54 | #define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC sha224_finish_wrap |
| 55 | #define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC sha256_wrap |
| 56 | #define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC sha224_ctx_alloc |
| 57 | #define MBEDTLS_MD_INFO_SHA256_FREE_FUNC sha224_ctx_free |
| 58 | #define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC sha224_clone_wrap |
| 59 | #define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC sha224_process_wrap |
| 60 | |
| 61 | /* |
| 62 | * Helper macros to extract fields from ciphersuites. |
| 63 | */ |
| 64 | |
| 65 | #define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE |
| 66 | #define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME |
| 67 | #define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE |
| 68 | #define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE |
| 69 | #define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC |
| 70 | #define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC |
| 71 | #define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC |
| 72 | #define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC |
| 73 | #define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC |
| 74 | #define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC |
| 75 | #define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC |
| 76 | #define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC |
| 77 | |
| 78 | /* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that |
| 79 | * the argument is macro-expanded before concatenated with the |
| 80 | * field name. This allows to call these macros as |
| 81 | * MBEDTLS_MD_INFO_XXX( MBEDTLS_SSL_CONF_SINGLE_HASH ). |
| 82 | * where MBEDTLS_SSL_CONF_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */ |
| 83 | #define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD ) |
| 84 | #define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD ) |
| 85 | #define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD ) |
| 86 | #define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) |
| 87 | #define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) |
| 88 | #define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) |
| 89 | #define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) |
| 90 | #define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) |
| 91 | #define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) |
| 92 | #define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) |
| 93 | #define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) |
| 94 | #define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) |
| 95 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 96 | /** |
| 97 | * Message digest information. |
| 98 | * Allows message digest functions to be called in a generic way. |
| 99 | */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 100 | |
| 101 | typedef int mbedtls_md_starts_func_t( void *ctx ); |
| 102 | typedef int mbedtls_md_update_func_t( void *ctx, |
| 103 | const unsigned char *input, |
| 104 | size_t ilen ); |
| 105 | typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output ); |
| 106 | typedef int mbedtls_md_digest_func_t( const unsigned char *input, |
| 107 | size_t ilen, |
| 108 | unsigned char *output ); |
| 109 | typedef void* mbedtls_md_ctx_alloc_func_t( void ); |
| 110 | typedef void mbedtls_md_ctx_free_func_t( void *ctx ); |
| 111 | typedef void mbedtls_md_clone_func_t( void *st, const void *src ); |
| 112 | typedef int mbedtls_md_process_func_t( void *ctx, |
| 113 | const unsigned char *input ); |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | struct mbedtls_md_info_t |
Manuel Pégourié-Gonnard | f5fc649 | 2015-04-02 14:43:57 +0100 | [diff] [blame] | 116 | { |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 117 | /** Digest identifier */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_md_type_t type; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 119 | |
| 120 | /** Name of the message digest */ |
| 121 | const char * name; |
| 122 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 123 | /** Output length of the digest function in bytes */ |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 124 | int size; |
| 125 | |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 126 | /** Block length of the digest function in bytes */ |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 127 | int block_size; |
| 128 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 129 | /** Digest initialisation function */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 130 | mbedtls_md_starts_func_t *starts_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 131 | |
| 132 | /** Digest update function */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 133 | mbedtls_md_update_func_t *update_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 134 | |
| 135 | /** Digest finalisation function */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 136 | mbedtls_md_finish_func_t *finish_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 137 | |
| 138 | /** Generic digest function */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 139 | mbedtls_md_digest_func_t *digest_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 141 | /** Allocate a new context */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 142 | mbedtls_md_ctx_alloc_func_t *ctx_alloc_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 143 | |
| 144 | /** Free the given context */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 145 | mbedtls_md_ctx_free_func_t *ctx_free_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 147 | /** Clone state from a context */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 148 | mbedtls_md_clone_func_t *clone_func; |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 149 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 150 | /** Internal use only */ |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 151 | mbedtls_md_process_func_t *process_func; |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Hanno Becker | 62a44d4 | 2019-07-17 14:14:01 +0100 | [diff] [blame] | 154 | /** |
| 155 | * \brief This macro builds an instance of ::mbedtls_md_info_t |
| 156 | * from an \c MBEDTLS_MD_INFO_XXX identifier. |
| 157 | */ |
| 158 | #define MBEDTLS_MD_INFO( MD ) \ |
| 159 | { MBEDTLS_MD_INFO_TYPE( MD ), \ |
| 160 | MBEDTLS_MD_INFO_NAME( MD ), \ |
| 161 | MBEDTLS_MD_INFO_SIZE( MD ), \ |
| 162 | MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \ |
| 163 | MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \ |
| 164 | MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \ |
| 165 | MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \ |
| 166 | MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \ |
| 167 | MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \ |
| 168 | MBEDTLS_MD_INFO_FREE_FUNC( MD ), \ |
| 169 | MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \ |
| 170 | MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) } |
| 171 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Getter functions for MD info structure. |
| 174 | */ |
| 175 | |
| 176 | static inline mbedtls_md_type_t mbedtls_md_info_type( |
| 177 | mbedtls_md_handle_t info ) |
| 178 | { |
| 179 | return( info->type ); |
| 180 | } |
| 181 | |
| 182 | static inline const char * mbedtls_md_info_name( |
| 183 | mbedtls_md_handle_t info ) |
| 184 | { |
| 185 | return( info->name ); |
| 186 | } |
| 187 | |
| 188 | static inline int mbedtls_md_info_size( |
| 189 | mbedtls_md_handle_t info ) |
| 190 | { |
| 191 | return( info->size ); |
| 192 | } |
| 193 | |
| 194 | static inline int mbedtls_md_info_block_size( |
| 195 | mbedtls_md_handle_t info ) |
| 196 | { |
| 197 | return( info->block_size ); |
| 198 | } |
| 199 | |
| 200 | static inline mbedtls_md_starts_func_t *mbedtls_md_info_starts_func( |
| 201 | mbedtls_md_handle_t info ) |
| 202 | { |
| 203 | return( info->starts_func ); |
| 204 | } |
| 205 | |
| 206 | static inline mbedtls_md_update_func_t *mbedtls_md_info_update_func( |
| 207 | mbedtls_md_handle_t info ) |
| 208 | { |
| 209 | return( info->update_func ); |
| 210 | } |
| 211 | |
| 212 | static inline mbedtls_md_finish_func_t *mbedtls_md_info_finish_func( |
| 213 | mbedtls_md_handle_t info ) |
| 214 | { |
| 215 | return( info->finish_func ); |
| 216 | } |
| 217 | |
| 218 | static inline mbedtls_md_digest_func_t *mbedtls_md_info_digest_func( |
| 219 | mbedtls_md_handle_t info ) |
| 220 | { |
| 221 | return( info->digest_func ); |
| 222 | } |
| 223 | |
| 224 | static inline mbedtls_md_ctx_alloc_func_t *mbedtls_md_info_ctx_alloc_func( |
| 225 | mbedtls_md_handle_t info ) |
| 226 | { |
| 227 | return( info->ctx_alloc_func ); |
| 228 | } |
| 229 | |
| 230 | static inline mbedtls_md_ctx_free_func_t *mbedtls_md_info_ctx_free_func( |
| 231 | mbedtls_md_handle_t info ) |
| 232 | { |
| 233 | return( info->ctx_free_func ); |
| 234 | } |
| 235 | |
| 236 | static inline mbedtls_md_clone_func_t *mbedtls_md_info_clone_func( |
| 237 | mbedtls_md_handle_t info ) |
| 238 | { |
| 239 | return( info->clone_func ); |
| 240 | } |
| 241 | |
| 242 | static inline mbedtls_md_process_func_t *mbedtls_md_info_process_func( |
| 243 | mbedtls_md_handle_t info ) |
| 244 | { |
| 245 | return( info->process_func ); |
| 246 | } |
| 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | #if defined(MBEDTLS_MD2_C) |
| 249 | extern const mbedtls_md_info_t mbedtls_md2_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 250 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #if defined(MBEDTLS_MD4_C) |
| 252 | extern const mbedtls_md_info_t mbedtls_md4_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 253 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | #if defined(MBEDTLS_MD5_C) |
| 255 | extern const mbedtls_md_info_t mbedtls_md5_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 256 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | #if defined(MBEDTLS_RIPEMD160_C) |
| 258 | extern const mbedtls_md_info_t mbedtls_ripemd160_info; |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 259 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | #if defined(MBEDTLS_SHA1_C) |
| 261 | extern const mbedtls_md_info_t mbedtls_sha1_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 262 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 264 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | extern const mbedtls_md_info_t mbedtls_sha224_info; |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 266 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | extern const mbedtls_md_info_t mbedtls_sha256_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 268 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | #if defined(MBEDTLS_SHA512_C) |
| 270 | extern const mbedtls_md_info_t mbedtls_sha384_info; |
| 271 | extern const mbedtls_md_info_t mbedtls_sha512_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 272 | #endif |
| 273 | |
| 274 | #ifdef __cplusplus |
| 275 | } |
| 276 | #endif |
| 277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | #endif /* MBEDTLS_MD_WRAP_H */ |