Hanno Becker | c4e4210 | 2019-09-04 12:43:22 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file md.h |
| 3 | * |
| 4 | * \brief This file contains the generic message-digest wrapper. |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | */ |
| 8 | /* |
| 9 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
| 23 | * |
| 24 | * This file is part of Mbed TLS (https://tls.mbed.org) |
| 25 | */ |
| 26 | |
| 27 | #ifndef MBEDTLS_MD_INTERNAL_H |
| 28 | #define MBEDTLS_MD_INTERNAL_H |
| 29 | |
| 30 | #if defined(MBEDTLS_MD2_C) |
| 31 | #include "mbedtls/md2.h" |
| 32 | #endif |
| 33 | |
| 34 | #if defined(MBEDTLS_MD4_C) |
| 35 | #include "mbedtls/md4.h" |
| 36 | #endif |
| 37 | |
| 38 | #if defined(MBEDTLS_MD5_C) |
| 39 | #include "mbedtls/md5.h" |
| 40 | #endif |
| 41 | |
| 42 | #if defined(MBEDTLS_RIPEMD160_C) |
| 43 | #include "mbedtls/ripemd160.h" |
| 44 | #endif |
| 45 | |
| 46 | #if defined(MBEDTLS_SHA1_C) |
| 47 | #include "mbedtls/sha1.h" |
| 48 | #endif |
| 49 | |
| 50 | #if defined(MBEDTLS_SHA256_C) |
| 51 | #include "mbedtls/sha256.h" |
| 52 | #endif |
| 53 | |
| 54 | #if defined(MBEDTLS_SHA512_C) |
| 55 | #include "mbedtls/sha512.h" |
| 56 | #endif |
| 57 | |
| 58 | #include "mbedtls/platform_util.h" |
| 59 | |
| 60 | #if defined(MBEDTLS_PLATFORM_C) |
| 61 | #include "mbedtls/platform.h" |
| 62 | #else |
| 63 | #include <stdlib.h> |
| 64 | #define mbedtls_calloc calloc |
| 65 | #define mbedtls_free free |
| 66 | #endif |
| 67 | |
| 68 | #ifdef __cplusplus |
| 69 | extern "C" { |
| 70 | #endif |
| 71 | |
| 72 | /* |
| 73 | * Message-digest information macro definition |
| 74 | */ |
| 75 | |
Hanno Becker | 7a7b722 | 2019-09-04 12:47:48 +0100 | [diff] [blame] | 76 | /* Dummy definition to keep check-names.sh happy - don't uncomment */ |
| 77 | //#define MBEDTLS_MD_INFO_SHA256 |
| 78 | |
Hanno Becker | c4e4210 | 2019-09-04 12:43:22 +0100 | [diff] [blame] | 79 | /* SHA-256 */ |
| 80 | #define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256 |
| 81 | #define MBEDTLS_MD_INFO_SHA256_NAME "SHA256" |
| 82 | #define MBEDTLS_MD_INFO_SHA256_SIZE 32 |
| 83 | #define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64 |
| 84 | #define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC mbedtls_sha256_starts_wrap |
| 85 | #define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC mbedtls_sha224_update_wrap |
| 86 | #define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC mbedtls_sha224_finish_wrap |
| 87 | #define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC mbedtls_sha256_wrap |
| 88 | #define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC mbedtls_sha224_ctx_alloc |
| 89 | #define MBEDTLS_MD_INFO_SHA256_FREE_FUNC mbedtls_sha224_ctx_free |
| 90 | #define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC mbedtls_sha224_clone_wrap |
| 91 | #define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC mbedtls_sha224_process_wrap |
| 92 | |
| 93 | /* |
| 94 | * Helper macros to extract fields from ciphersuites. |
| 95 | */ |
| 96 | |
| 97 | #define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE |
| 98 | #define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME |
| 99 | #define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE |
| 100 | #define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE |
| 101 | #define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC |
| 102 | #define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC |
| 103 | #define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC |
| 104 | #define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC |
| 105 | #define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC |
| 106 | #define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC |
| 107 | #define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC |
| 108 | #define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC |
| 109 | |
| 110 | /* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that |
| 111 | * the argument is macro-expanded before concatenated with the |
| 112 | * field name. This allows to call these macros as |
| 113 | * MBEDTLS_MD_INFO_XXX( MBEDTLS_MD_SINGLE_HASH ). |
| 114 | * where MBEDTLS_MD_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */ |
| 115 | #define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD ) |
| 116 | #define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD ) |
| 117 | #define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD ) |
| 118 | #define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) |
| 119 | #define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) |
| 120 | #define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) |
| 121 | #define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) |
| 122 | #define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) |
| 123 | #define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) |
| 124 | #define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) |
| 125 | #define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) |
| 126 | #define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) |
| 127 | |
| 128 | /** |
| 129 | * Message digest information. |
| 130 | * Allows message digest functions to be called in a generic way. |
| 131 | */ |
| 132 | |
| 133 | typedef int mbedtls_md_starts_func_t( void *ctx ); |
| 134 | typedef int mbedtls_md_update_func_t( void *ctx, |
| 135 | const unsigned char *input, |
| 136 | size_t ilen ); |
| 137 | typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output ); |
| 138 | typedef int mbedtls_md_digest_func_t( const unsigned char *input, |
| 139 | size_t ilen, |
| 140 | unsigned char *output ); |
| 141 | typedef void* mbedtls_md_ctx_alloc_func_t( void ); |
| 142 | typedef void mbedtls_md_ctx_free_func_t( void *ctx ); |
| 143 | typedef void mbedtls_md_clone_func_t( void *st, const void *src ); |
| 144 | typedef int mbedtls_md_process_func_t( void *ctx, |
| 145 | const unsigned char *input ); |
| 146 | |
| 147 | #if !defined(MBEDTLS_MD_SINGLE_HASH) |
| 148 | struct mbedtls_md_info_t |
| 149 | { |
| 150 | /** Digest identifier */ |
| 151 | mbedtls_md_type_t type; |
| 152 | |
| 153 | /** Name of the message digest */ |
| 154 | const char * name; |
| 155 | |
| 156 | /** Output length of the digest function in bytes */ |
| 157 | int size; |
| 158 | |
| 159 | /** Block length of the digest function in bytes */ |
| 160 | int block_size; |
| 161 | |
| 162 | /** Digest initialisation function */ |
| 163 | mbedtls_md_starts_func_t *starts_func; |
| 164 | |
| 165 | /** Digest update function */ |
| 166 | mbedtls_md_update_func_t *update_func; |
| 167 | |
| 168 | /** Digest finalisation function */ |
| 169 | mbedtls_md_finish_func_t *finish_func; |
| 170 | |
| 171 | /** Generic digest function */ |
| 172 | mbedtls_md_digest_func_t *digest_func; |
| 173 | |
| 174 | /** Allocate a new context */ |
| 175 | mbedtls_md_ctx_alloc_func_t *ctx_alloc_func; |
| 176 | |
| 177 | /** Free the given context */ |
| 178 | mbedtls_md_ctx_free_func_t *ctx_free_func; |
| 179 | |
| 180 | /** Clone state from a context */ |
| 181 | mbedtls_md_clone_func_t *clone_func; |
| 182 | |
| 183 | /** Internal use only */ |
| 184 | mbedtls_md_process_func_t *process_func; |
| 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * \brief This macro builds an instance of ::mbedtls_md_info_t |
| 189 | * from an \c MBEDTLS_MD_INFO_XXX identifier. |
| 190 | */ |
| 191 | #define MBEDTLS_MD_INFO( MD ) \ |
| 192 | { MBEDTLS_MD_INFO_TYPE( MD ), \ |
| 193 | MBEDTLS_MD_INFO_NAME( MD ), \ |
| 194 | MBEDTLS_MD_INFO_SIZE( MD ), \ |
| 195 | MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \ |
| 196 | MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \ |
| 197 | MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \ |
| 198 | MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \ |
| 199 | MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \ |
| 200 | MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \ |
| 201 | MBEDTLS_MD_INFO_FREE_FUNC( MD ), \ |
| 202 | MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \ |
| 203 | MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) } |
| 204 | |
| 205 | #endif /* !MBEDTLS_MD_SINGLE_HASH */ |
| 206 | |
| 207 | /* |
| 208 | * |
| 209 | * Definitions of MD information structures for various digests. |
| 210 | * |
| 211 | */ |
| 212 | |
| 213 | /* |
| 214 | * MD-2 |
| 215 | */ |
| 216 | |
| 217 | #if defined(MBEDTLS_MD2_C) |
| 218 | |
| 219 | static int mbedtls_md2_starts_wrap( void *ctx ) |
| 220 | { |
| 221 | return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) ); |
| 222 | } |
| 223 | |
| 224 | static int mbedtls_md2_update_wrap( void *ctx, const unsigned char *input, |
| 225 | size_t ilen ) |
| 226 | { |
| 227 | return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) ); |
| 228 | } |
| 229 | |
| 230 | static int mbedtls_md2_finish_wrap( void *ctx, unsigned char *output ) |
| 231 | { |
| 232 | return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) ); |
| 233 | } |
| 234 | |
| 235 | static void* mbedtls_md2_ctx_alloc( void ) |
| 236 | { |
| 237 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) ); |
| 238 | |
| 239 | if( ctx != NULL ) |
| 240 | mbedtls_md2_init( (mbedtls_md2_context *) ctx ); |
| 241 | |
| 242 | return( ctx ); |
| 243 | } |
| 244 | |
| 245 | static void mbedtls_md2_ctx_free( void *ctx ) |
| 246 | { |
| 247 | mbedtls_md2_free( (mbedtls_md2_context *) ctx ); |
| 248 | mbedtls_free( ctx ); |
| 249 | } |
| 250 | |
| 251 | static void mbedtls_md2_clone_wrap( void *dst, const void *src ) |
| 252 | { |
| 253 | mbedtls_md2_clone( (mbedtls_md2_context *) dst, |
| 254 | (const mbedtls_md2_context *) src ); |
| 255 | } |
| 256 | |
| 257 | static int mbedtls_md2_process_wrap( void *ctx, const unsigned char *data ) |
| 258 | { |
| 259 | ((void) data); |
| 260 | |
| 261 | return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) ); |
| 262 | } |
| 263 | |
| 264 | #endif /* MBEDTLS_MD2_C */ |
| 265 | |
| 266 | /* |
| 267 | * MD-4 |
| 268 | */ |
| 269 | |
| 270 | #if defined(MBEDTLS_MD4_C) |
| 271 | |
| 272 | static int mbedtls_md4_starts_wrap( void *ctx ) |
| 273 | { |
| 274 | return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) ); |
| 275 | } |
| 276 | |
| 277 | static int mbedtls_md4_update_wrap( void *ctx, const unsigned char *input, |
| 278 | size_t ilen ) |
| 279 | { |
| 280 | return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) ); |
| 281 | } |
| 282 | |
| 283 | static int mbedtls_md4_finish_wrap( void *ctx, unsigned char *output ) |
| 284 | { |
| 285 | return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) ); |
| 286 | } |
| 287 | |
| 288 | static void* mbedtls_md4_ctx_alloc( void ) |
| 289 | { |
| 290 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) ); |
| 291 | |
| 292 | if( ctx != NULL ) |
| 293 | mbedtls_md4_init( (mbedtls_md4_context *) ctx ); |
| 294 | |
| 295 | return( ctx ); |
| 296 | } |
| 297 | |
| 298 | static void mbedtls_md4_ctx_free( void *ctx ) |
| 299 | { |
| 300 | mbedtls_md4_free( (mbedtls_md4_context *) ctx ); |
| 301 | mbedtls_free( ctx ); |
| 302 | } |
| 303 | |
| 304 | static void mbedtls_md4_clone_wrap( void *dst, const void *src ) |
| 305 | { |
| 306 | mbedtls_md4_clone( (mbedtls_md4_context *) dst, |
| 307 | (const mbedtls_md4_context *) src ); |
| 308 | } |
| 309 | |
| 310 | static int mbedtls_md4_process_wrap( void *ctx, const unsigned char *data ) |
| 311 | { |
| 312 | return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) ); |
| 313 | } |
| 314 | |
| 315 | #endif /* MBEDTLS_MD4_C */ |
| 316 | |
| 317 | /* |
| 318 | * MD-5 |
| 319 | */ |
| 320 | |
| 321 | #if defined(MBEDTLS_MD5_C) |
| 322 | |
| 323 | static int mbedtls_md5_starts_wrap( void *ctx ) |
| 324 | { |
| 325 | return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) ); |
| 326 | } |
| 327 | |
| 328 | static int mbedtls_md5_update_wrap( void *ctx, const unsigned char *input, |
| 329 | size_t ilen ) |
| 330 | { |
| 331 | return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) ); |
| 332 | } |
| 333 | |
| 334 | static int mbedtls_md5_finish_wrap( void *ctx, unsigned char *output ) |
| 335 | { |
| 336 | return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) ); |
| 337 | } |
| 338 | |
| 339 | static void* mbedtls_md5_ctx_alloc( void ) |
| 340 | { |
| 341 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) ); |
| 342 | |
| 343 | if( ctx != NULL ) |
| 344 | mbedtls_md5_init( (mbedtls_md5_context *) ctx ); |
| 345 | |
| 346 | return( ctx ); |
| 347 | } |
| 348 | |
| 349 | static void mbedtls_md5_ctx_free( void *ctx ) |
| 350 | { |
| 351 | mbedtls_md5_free( (mbedtls_md5_context *) ctx ); |
| 352 | mbedtls_free( ctx ); |
| 353 | } |
| 354 | |
| 355 | static void mbedtls_md5_clone_wrap( void *dst, const void *src ) |
| 356 | { |
| 357 | mbedtls_md5_clone( (mbedtls_md5_context *) dst, |
| 358 | (const mbedtls_md5_context *) src ); |
| 359 | } |
| 360 | |
| 361 | static int mbedtls_md5_process_wrap( void *ctx, const unsigned char *data ) |
| 362 | { |
| 363 | return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) ); |
| 364 | } |
| 365 | |
| 366 | #endif /* MBEDTLS_MD5_C */ |
| 367 | |
| 368 | /* |
| 369 | * RIPEMD-160 |
| 370 | */ |
| 371 | |
| 372 | #if defined(MBEDTLS_RIPEMD160_C) |
| 373 | |
| 374 | static int mbedtls_ripemd160_starts_wrap( void *ctx ) |
| 375 | { |
| 376 | return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) ); |
| 377 | } |
| 378 | |
| 379 | static int mbedtls_ripemd160_update_wrap( void *ctx, const unsigned char *input, |
| 380 | size_t ilen ) |
| 381 | { |
| 382 | return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx, |
| 383 | input, ilen ) ); |
| 384 | } |
| 385 | |
| 386 | static int mbedtls_ripemd160_finish_wrap( void *ctx, unsigned char *output ) |
| 387 | { |
| 388 | return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx, |
| 389 | output ) ); |
| 390 | } |
| 391 | |
| 392 | static void* mbedtls_ripemd160_ctx_alloc( void ) |
| 393 | { |
| 394 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) ); |
| 395 | |
| 396 | if( ctx != NULL ) |
| 397 | mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx ); |
| 398 | |
| 399 | return( ctx ); |
| 400 | } |
| 401 | |
| 402 | static void mbedtls_ripemd160_ctx_free( void *ctx ) |
| 403 | { |
| 404 | mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx ); |
| 405 | mbedtls_free( ctx ); |
| 406 | } |
| 407 | |
| 408 | static void mbedtls_ripemd160_clone_wrap( void *dst, const void *src ) |
| 409 | { |
| 410 | mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst, |
| 411 | (const mbedtls_ripemd160_context *) src ); |
| 412 | } |
| 413 | |
| 414 | static int mbedtls_ripemd160_process_wrap( void *ctx, const unsigned char *data ) |
| 415 | { |
| 416 | return( mbedtls_internal_ripemd160_process( |
| 417 | (mbedtls_ripemd160_context *) ctx, data ) ); |
| 418 | } |
| 419 | |
| 420 | #endif /* MBEDTLS_RIPEMD160_C */ |
| 421 | |
| 422 | /* |
| 423 | * SHA-1 |
| 424 | */ |
| 425 | |
| 426 | #if defined(MBEDTLS_SHA1_C) |
| 427 | |
| 428 | static int mbedtls_sha1_starts_wrap( void *ctx ) |
| 429 | { |
| 430 | return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) ); |
| 431 | } |
| 432 | |
| 433 | static int mbedtls_sha1_update_wrap( void *ctx, const unsigned char *input, |
| 434 | size_t ilen ) |
| 435 | { |
| 436 | return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx, |
| 437 | input, ilen ) ); |
| 438 | } |
| 439 | |
| 440 | static int mbedtls_sha1_finish_wrap( void *ctx, unsigned char *output ) |
| 441 | { |
| 442 | return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) ); |
| 443 | } |
| 444 | |
| 445 | static void* mbedtls_sha1_ctx_alloc( void ) |
| 446 | { |
| 447 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) ); |
| 448 | |
| 449 | if( ctx != NULL ) |
| 450 | mbedtls_sha1_init( (mbedtls_sha1_context *) ctx ); |
| 451 | |
| 452 | return( ctx ); |
| 453 | } |
| 454 | |
| 455 | static void mbedtls_sha1_clone_wrap( void *dst, const void *src ) |
| 456 | { |
| 457 | mbedtls_sha1_clone( (mbedtls_sha1_context *) dst, |
| 458 | (const mbedtls_sha1_context *) src ); |
| 459 | } |
| 460 | |
| 461 | static void mbedtls_sha1_ctx_free( void *ctx ) |
| 462 | { |
| 463 | mbedtls_sha1_free( (mbedtls_sha1_context *) ctx ); |
| 464 | mbedtls_free( ctx ); |
| 465 | } |
| 466 | |
| 467 | static int mbedtls_sha1_process_wrap( void *ctx, const unsigned char *data ) |
| 468 | { |
| 469 | return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx, |
| 470 | data ) ); |
| 471 | } |
| 472 | |
| 473 | #endif /* MBEDTLS_SHA1_C */ |
| 474 | |
| 475 | /* |
| 476 | * SHA-224 and SHA-256 |
| 477 | */ |
| 478 | |
| 479 | #if defined(MBEDTLS_SHA256_C) |
| 480 | |
| 481 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
| 482 | static int mbedtls_sha224_starts_wrap( void *ctx ) |
| 483 | { |
| 484 | return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) ); |
| 485 | } |
| 486 | #endif /* !MBEDTLS_SHA256_NO_SHA224 */ |
| 487 | |
| 488 | static int mbedtls_sha224_update_wrap( void *ctx, const unsigned char *input, |
| 489 | size_t ilen ) |
| 490 | { |
| 491 | return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx, |
| 492 | input, ilen ) ); |
| 493 | } |
| 494 | |
| 495 | static int mbedtls_sha224_finish_wrap( void *ctx, unsigned char *output ) |
| 496 | { |
| 497 | return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx, |
| 498 | output ) ); |
| 499 | } |
| 500 | |
| 501 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
| 502 | static int mbedtls_sha224_wrap( const unsigned char *input, size_t ilen, |
| 503 | unsigned char *output ) |
| 504 | { |
| 505 | return( mbedtls_sha256_ret( input, ilen, output, 1 ) ); |
| 506 | } |
| 507 | #endif /* !MBEDTLS_SHA256_NO_SHA224 */ |
| 508 | |
| 509 | static void* mbedtls_sha224_ctx_alloc( void ) |
| 510 | { |
| 511 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) ); |
| 512 | |
| 513 | if( ctx != NULL ) |
| 514 | mbedtls_sha256_init( (mbedtls_sha256_context *) ctx ); |
| 515 | |
| 516 | return( ctx ); |
| 517 | } |
| 518 | |
| 519 | static void mbedtls_sha224_ctx_free( void *ctx ) |
| 520 | { |
| 521 | mbedtls_sha256_free( (mbedtls_sha256_context *) ctx ); |
| 522 | mbedtls_free( ctx ); |
| 523 | } |
| 524 | |
| 525 | static void mbedtls_sha224_clone_wrap( void *dst, const void *src ) |
| 526 | { |
| 527 | mbedtls_sha256_clone( (mbedtls_sha256_context *) dst, |
| 528 | (const mbedtls_sha256_context *) src ); |
| 529 | } |
| 530 | |
| 531 | static int mbedtls_sha224_process_wrap( void *ctx, const unsigned char *data ) |
| 532 | { |
| 533 | return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx, |
| 534 | data ) ); |
| 535 | } |
| 536 | |
| 537 | static int mbedtls_sha256_starts_wrap( void *ctx ) |
| 538 | { |
| 539 | return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) ); |
| 540 | } |
| 541 | |
| 542 | static int mbedtls_sha256_wrap( const unsigned char *input, size_t ilen, |
| 543 | unsigned char *output ) |
| 544 | { |
| 545 | return( mbedtls_sha256_ret( input, ilen, output, 0 ) ); |
| 546 | } |
| 547 | |
| 548 | #endif /* MBEDTLS_SHA256_C */ |
| 549 | |
| 550 | /* |
| 551 | * SHA-384 and SHA-512 |
| 552 | */ |
| 553 | |
| 554 | #if defined(MBEDTLS_SHA512_C) |
| 555 | |
| 556 | static int mbedtls_sha384_starts_wrap( void *ctx ) |
| 557 | { |
| 558 | return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) ); |
| 559 | } |
| 560 | |
| 561 | static int mbedtls_sha384_update_wrap( void *ctx, const unsigned char *input, |
| 562 | size_t ilen ) |
| 563 | { |
| 564 | return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx, |
| 565 | input, ilen ) ); |
| 566 | } |
| 567 | |
| 568 | static int mbedtls_sha384_finish_wrap( void *ctx, unsigned char *output ) |
| 569 | { |
| 570 | return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx, |
| 571 | output ) ); |
| 572 | } |
| 573 | |
| 574 | static int mbedtls_sha384_wrap( const unsigned char *input, size_t ilen, |
| 575 | unsigned char *output ) |
| 576 | { |
| 577 | return( mbedtls_sha512_ret( input, ilen, output, 1 ) ); |
| 578 | } |
| 579 | |
| 580 | static void* mbedtls_sha384_ctx_alloc( void ) |
| 581 | { |
| 582 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) ); |
| 583 | |
| 584 | if( ctx != NULL ) |
| 585 | mbedtls_sha512_init( (mbedtls_sha512_context *) ctx ); |
| 586 | |
| 587 | return( ctx ); |
| 588 | } |
| 589 | |
| 590 | static void mbedtls_sha384_ctx_free( void *ctx ) |
| 591 | { |
| 592 | mbedtls_sha512_free( (mbedtls_sha512_context *) ctx ); |
| 593 | mbedtls_free( ctx ); |
| 594 | } |
| 595 | |
| 596 | static void mbedtls_sha384_clone_wrap( void *dst, const void *src ) |
| 597 | { |
| 598 | mbedtls_sha512_clone( (mbedtls_sha512_context *) dst, |
| 599 | (const mbedtls_sha512_context *) src ); |
| 600 | } |
| 601 | |
| 602 | static int mbedtls_sha384_process_wrap( void *ctx, const unsigned char *data ) |
| 603 | { |
| 604 | return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx, |
| 605 | data ) ); |
| 606 | } |
| 607 | |
| 608 | static int mbedtls_sha512_starts_wrap( void *ctx ) |
| 609 | { |
| 610 | return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) ); |
| 611 | } |
| 612 | |
| 613 | static int mbedtls_sha512_wrap( const unsigned char *input, size_t ilen, |
| 614 | unsigned char *output ) |
| 615 | { |
| 616 | return( mbedtls_sha512_ret( input, ilen, output, 0 ) ); |
| 617 | } |
| 618 | |
| 619 | #endif /* MBEDTLS_SHA512_C */ |
| 620 | |
| 621 | /* |
| 622 | * Getter functions for MD info structure. |
| 623 | */ |
| 624 | |
| 625 | #if !defined(MBEDTLS_MD_SINGLE_HASH) |
| 626 | |
| 627 | MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type( |
| 628 | mbedtls_md_handle_t info ) |
| 629 | { |
| 630 | return( info->type ); |
| 631 | } |
| 632 | |
| 633 | MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name( |
| 634 | mbedtls_md_handle_t info ) |
| 635 | { |
| 636 | return( info->name ); |
| 637 | } |
| 638 | |
| 639 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size( |
| 640 | mbedtls_md_handle_t info ) |
| 641 | { |
| 642 | return( info->size ); |
| 643 | } |
| 644 | |
| 645 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size( |
| 646 | mbedtls_md_handle_t info ) |
| 647 | { |
| 648 | return( info->block_size ); |
| 649 | } |
| 650 | |
| 651 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts( |
| 652 | mbedtls_md_handle_t info, |
| 653 | void *ctx ) |
| 654 | { |
| 655 | return( info->starts_func( ctx ) ); |
| 656 | } |
| 657 | |
| 658 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update( |
| 659 | mbedtls_md_handle_t info, |
| 660 | void *ctx, |
| 661 | const unsigned char *input, |
| 662 | size_t ilen ) |
| 663 | { |
| 664 | return( info->update_func( ctx, input, ilen ) ); |
| 665 | } |
| 666 | |
| 667 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish( |
| 668 | mbedtls_md_handle_t info, |
| 669 | void *ctx, |
| 670 | unsigned char *output ) |
| 671 | { |
| 672 | return( info->finish_func( ctx, output ) ); |
| 673 | } |
| 674 | |
| 675 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest( |
| 676 | mbedtls_md_handle_t info, |
| 677 | const unsigned char *input, |
| 678 | size_t ilen, |
| 679 | unsigned char *output ) |
| 680 | { |
| 681 | return( info->digest_func( input, ilen, output ) ); |
| 682 | } |
| 683 | |
| 684 | MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc( |
| 685 | mbedtls_md_handle_t info ) |
| 686 | { |
| 687 | return( info->ctx_alloc_func() ); |
| 688 | } |
| 689 | |
| 690 | MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free( |
| 691 | mbedtls_md_handle_t info, |
| 692 | void *ctx ) |
| 693 | { |
| 694 | info->ctx_free_func( ctx ); |
| 695 | } |
| 696 | |
| 697 | MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone( |
| 698 | mbedtls_md_handle_t info, |
| 699 | void *dst, |
| 700 | const void *src ) |
| 701 | { |
| 702 | info->clone_func( dst, src ); |
| 703 | } |
| 704 | |
| 705 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process( |
| 706 | mbedtls_md_handle_t info, |
| 707 | void *ctx, |
| 708 | const unsigned char *input ) |
| 709 | { |
| 710 | return( info->process_func( ctx, input ) ); |
| 711 | } |
| 712 | |
| 713 | #else /* !MBEDTLS_MD_SINGLE_HASH */ |
| 714 | |
| 715 | MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type( |
| 716 | mbedtls_md_handle_t info ) |
| 717 | { |
| 718 | ((void) info); |
| 719 | return( MBEDTLS_MD_INFO_TYPE( MBEDTLS_MD_SINGLE_HASH ) ); |
| 720 | } |
| 721 | |
| 722 | MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name( |
| 723 | mbedtls_md_handle_t info ) |
| 724 | { |
| 725 | ((void) info); |
| 726 | return( MBEDTLS_MD_INFO_NAME( MBEDTLS_MD_SINGLE_HASH ) ); |
| 727 | } |
| 728 | |
| 729 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size( |
| 730 | mbedtls_md_handle_t info ) |
| 731 | { |
| 732 | ((void) info); |
| 733 | return( MBEDTLS_MD_INFO_SIZE( MBEDTLS_MD_SINGLE_HASH ) ); |
| 734 | } |
| 735 | |
| 736 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size( |
| 737 | mbedtls_md_handle_t info ) |
| 738 | { |
| 739 | ((void) info); |
| 740 | return( MBEDTLS_MD_INFO_BLOCKSIZE( MBEDTLS_MD_SINGLE_HASH ) ); |
| 741 | } |
| 742 | |
| 743 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts( |
| 744 | mbedtls_md_handle_t info, |
| 745 | void *ctx ) |
| 746 | { |
| 747 | ((void) info); |
| 748 | return( MBEDTLS_MD_INFO_STARTS_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx ) ); |
| 749 | } |
| 750 | |
| 751 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update( |
| 752 | mbedtls_md_handle_t info, |
| 753 | void *ctx, |
| 754 | const unsigned char *input, |
| 755 | size_t ilen ) |
| 756 | { |
| 757 | ((void) info); |
| 758 | return( MBEDTLS_MD_INFO_UPDATE_FUNC( MBEDTLS_MD_SINGLE_HASH ) |
| 759 | ( ctx, input, ilen ) ); |
| 760 | } |
| 761 | |
| 762 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish( |
| 763 | mbedtls_md_handle_t info, |
| 764 | void *ctx, |
| 765 | unsigned char *output ) |
| 766 | { |
| 767 | ((void) info); |
| 768 | return( MBEDTLS_MD_INFO_FINISH_FUNC( MBEDTLS_MD_SINGLE_HASH ) |
| 769 | ( ctx, output ) ); |
| 770 | } |
| 771 | |
| 772 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest( |
| 773 | mbedtls_md_handle_t info, |
| 774 | const unsigned char *input, |
| 775 | size_t ilen, |
| 776 | unsigned char *output ) |
| 777 | { |
| 778 | ((void) info); |
| 779 | return( MBEDTLS_MD_INFO_DIGEST_FUNC( MBEDTLS_MD_SINGLE_HASH ) |
| 780 | ( input, ilen, output ) ); |
| 781 | } |
| 782 | |
| 783 | MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc( |
| 784 | mbedtls_md_handle_t info ) |
| 785 | { |
| 786 | ((void) info); |
| 787 | return( MBEDTLS_MD_INFO_ALLOC_FUNC( MBEDTLS_MD_SINGLE_HASH )() ); |
| 788 | } |
| 789 | |
| 790 | MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free( |
| 791 | mbedtls_md_handle_t info, |
| 792 | void *ctx ) |
| 793 | { |
| 794 | ((void) info); |
| 795 | MBEDTLS_MD_INFO_FREE_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx ); |
| 796 | } |
| 797 | |
| 798 | MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone( |
| 799 | mbedtls_md_handle_t info, |
| 800 | void *dst, |
| 801 | const void *src ) |
| 802 | { |
| 803 | ((void) info); |
| 804 | MBEDTLS_MD_INFO_CLONE_FUNC( MBEDTLS_MD_SINGLE_HASH )( dst, src ); |
| 805 | } |
| 806 | |
| 807 | MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process( |
| 808 | mbedtls_md_handle_t info, |
| 809 | void *ctx, |
| 810 | const unsigned char *input ) |
| 811 | { |
| 812 | ((void) info); |
| 813 | return( MBEDTLS_MD_INFO_PROCESS_FUNC( MBEDTLS_MD_SINGLE_HASH ) |
| 814 | ( ctx, input ) ); |
| 815 | } |
| 816 | |
| 817 | #endif /* MBEDTLS_MD_SINGLE_HASH */ |
| 818 | |
| 819 | #ifdef __cplusplus |
| 820 | } |
| 821 | #endif |
| 822 | |
| 823 | #endif /* MBEDTLS_MD_INTERNAL_H */ |