Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2 | * \file mbedtls_md.c |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 4 | * \brief Generic message digest wrapper for mbed TLS |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 22 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 23 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_MD_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/md.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 35 | #include "mbedtls/platform_util.h" |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 38 | #include "mbedtls/platform.h" |
| 39 | #else |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 41 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 45 | #include <string.h> |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_FS_IO) |
| 48 | #include <stdio.h> |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Hanno Becker | d03949e | 2019-07-26 14:38:44 +0100 | [diff] [blame^] | 51 | #if defined(MBEDTLS_MD2_C) |
| 52 | #include "mbedtls/md2.h" |
| 53 | #endif |
| 54 | |
| 55 | #if defined(MBEDTLS_MD4_C) |
| 56 | #include "mbedtls/md4.h" |
| 57 | #endif |
| 58 | |
| 59 | #if defined(MBEDTLS_MD5_C) |
| 60 | #include "mbedtls/md5.h" |
| 61 | #endif |
| 62 | |
| 63 | #if defined(MBEDTLS_RIPEMD160_C) |
| 64 | #include "mbedtls/ripemd160.h" |
| 65 | #endif |
| 66 | |
| 67 | #if defined(MBEDTLS_SHA1_C) |
| 68 | #include "mbedtls/sha1.h" |
| 69 | #endif |
| 70 | |
| 71 | #if defined(MBEDTLS_SHA256_C) |
| 72 | #include "mbedtls/sha256.h" |
| 73 | #endif |
| 74 | |
| 75 | #if defined(MBEDTLS_SHA512_C) |
| 76 | #include "mbedtls/sha512.h" |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | * Message-digest information macro definition |
| 81 | */ |
| 82 | |
| 83 | /* SHA-256 */ |
| 84 | #define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256 |
| 85 | #define MBEDTLS_MD_INFO_SHA256_NAME "SHA256" |
| 86 | #define MBEDTLS_MD_INFO_SHA256_SIZE 32 |
| 87 | #define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64 |
| 88 | #define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC sha256_starts_wrap |
| 89 | #define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC sha224_update_wrap |
| 90 | #define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC sha224_finish_wrap |
| 91 | #define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC sha256_wrap |
| 92 | #define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC sha224_ctx_alloc |
| 93 | #define MBEDTLS_MD_INFO_SHA256_FREE_FUNC sha224_ctx_free |
| 94 | #define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC sha224_clone_wrap |
| 95 | #define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC sha224_process_wrap |
| 96 | |
| 97 | /* |
| 98 | * Helper macros to extract fields from ciphersuites. |
| 99 | */ |
| 100 | |
| 101 | #define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE |
| 102 | #define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME |
| 103 | #define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE |
| 104 | #define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE |
| 105 | #define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC |
| 106 | #define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC |
| 107 | #define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC |
| 108 | #define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC |
| 109 | #define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC |
| 110 | #define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC |
| 111 | #define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC |
| 112 | #define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC |
| 113 | |
| 114 | /* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that |
| 115 | * the argument is macro-expanded before concatenated with the |
| 116 | * field name. This allows to call these macros as |
| 117 | * MBEDTLS_MD_INFO_XXX( MBEDTLS_SSL_CONF_SINGLE_HASH ). |
| 118 | * where MBEDTLS_SSL_CONF_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */ |
| 119 | #define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD ) |
| 120 | #define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD ) |
| 121 | #define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD ) |
| 122 | #define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) |
| 123 | #define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) |
| 124 | #define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) |
| 125 | #define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) |
| 126 | #define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) |
| 127 | #define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) |
| 128 | #define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) |
| 129 | #define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) |
| 130 | #define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) |
| 131 | |
| 132 | /** |
| 133 | * Message digest information. |
| 134 | * Allows message digest functions to be called in a generic way. |
| 135 | */ |
| 136 | |
| 137 | typedef int mbedtls_md_starts_func_t( void *ctx ); |
| 138 | typedef int mbedtls_md_update_func_t( void *ctx, |
| 139 | const unsigned char *input, |
| 140 | size_t ilen ); |
| 141 | typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output ); |
| 142 | typedef int mbedtls_md_digest_func_t( const unsigned char *input, |
| 143 | size_t ilen, |
| 144 | unsigned char *output ); |
| 145 | typedef void* mbedtls_md_ctx_alloc_func_t( void ); |
| 146 | typedef void mbedtls_md_ctx_free_func_t( void *ctx ); |
| 147 | typedef void mbedtls_md_clone_func_t( void *st, const void *src ); |
| 148 | typedef int mbedtls_md_process_func_t( void *ctx, |
| 149 | const unsigned char *input ); |
| 150 | |
| 151 | struct mbedtls_md_info_t |
| 152 | { |
| 153 | /** Digest identifier */ |
| 154 | mbedtls_md_type_t type; |
| 155 | |
| 156 | /** Name of the message digest */ |
| 157 | const char * name; |
| 158 | |
| 159 | /** Output length of the digest function in bytes */ |
| 160 | int size; |
| 161 | |
| 162 | /** Block length of the digest function in bytes */ |
| 163 | int block_size; |
| 164 | |
| 165 | /** Digest initialisation function */ |
| 166 | mbedtls_md_starts_func_t *starts_func; |
| 167 | |
| 168 | /** Digest update function */ |
| 169 | mbedtls_md_update_func_t *update_func; |
| 170 | |
| 171 | /** Digest finalisation function */ |
| 172 | mbedtls_md_finish_func_t *finish_func; |
| 173 | |
| 174 | /** Generic digest function */ |
| 175 | mbedtls_md_digest_func_t *digest_func; |
| 176 | |
| 177 | /** Allocate a new context */ |
| 178 | mbedtls_md_ctx_alloc_func_t *ctx_alloc_func; |
| 179 | |
| 180 | /** Free the given context */ |
| 181 | mbedtls_md_ctx_free_func_t *ctx_free_func; |
| 182 | |
| 183 | /** Clone state from a context */ |
| 184 | mbedtls_md_clone_func_t *clone_func; |
| 185 | |
| 186 | /** Internal use only */ |
| 187 | mbedtls_md_process_func_t *process_func; |
| 188 | }; |
| 189 | |
| 190 | /** |
| 191 | * \brief This macro builds an instance of ::mbedtls_md_info_t |
| 192 | * from an \c MBEDTLS_MD_INFO_XXX identifier. |
| 193 | */ |
| 194 | #define MBEDTLS_MD_INFO( MD ) \ |
| 195 | { MBEDTLS_MD_INFO_TYPE( MD ), \ |
| 196 | MBEDTLS_MD_INFO_NAME( MD ), \ |
| 197 | MBEDTLS_MD_INFO_SIZE( MD ), \ |
| 198 | MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \ |
| 199 | MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \ |
| 200 | MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \ |
| 201 | MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \ |
| 202 | MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \ |
| 203 | MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \ |
| 204 | MBEDTLS_MD_INFO_FREE_FUNC( MD ), \ |
| 205 | MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \ |
| 206 | MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) } |
| 207 | |
| 208 | /* |
| 209 | * |
| 210 | * Definitions of MD information structures for various digests. |
| 211 | * |
| 212 | */ |
| 213 | |
| 214 | /* |
| 215 | * MD-2 |
| 216 | */ |
| 217 | |
| 218 | #if defined(MBEDTLS_MD2_C) |
| 219 | |
| 220 | static int md2_starts_wrap( void *ctx ) |
| 221 | { |
| 222 | return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) ); |
| 223 | } |
| 224 | |
| 225 | static int md2_update_wrap( void *ctx, const unsigned char *input, |
| 226 | size_t ilen ) |
| 227 | { |
| 228 | return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) ); |
| 229 | } |
| 230 | |
| 231 | static int md2_finish_wrap( void *ctx, unsigned char *output ) |
| 232 | { |
| 233 | return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) ); |
| 234 | } |
| 235 | |
| 236 | static void *md2_ctx_alloc( void ) |
| 237 | { |
| 238 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) ); |
| 239 | |
| 240 | if( ctx != NULL ) |
| 241 | mbedtls_md2_init( (mbedtls_md2_context *) ctx ); |
| 242 | |
| 243 | return( ctx ); |
| 244 | } |
| 245 | |
| 246 | static void md2_ctx_free( void *ctx ) |
| 247 | { |
| 248 | mbedtls_md2_free( (mbedtls_md2_context *) ctx ); |
| 249 | mbedtls_free( ctx ); |
| 250 | } |
| 251 | |
| 252 | static void md2_clone_wrap( void *dst, const void *src ) |
| 253 | { |
| 254 | mbedtls_md2_clone( (mbedtls_md2_context *) dst, |
| 255 | (const mbedtls_md2_context *) src ); |
| 256 | } |
| 257 | |
| 258 | static int md2_process_wrap( void *ctx, const unsigned char *data ) |
| 259 | { |
| 260 | ((void) data); |
| 261 | |
| 262 | return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) ); |
| 263 | } |
| 264 | |
| 265 | const mbedtls_md_info_t mbedtls_md2_info = { |
| 266 | MBEDTLS_MD_MD2, |
| 267 | "MD2", |
| 268 | 16, |
| 269 | 16, |
| 270 | md2_starts_wrap, |
| 271 | md2_update_wrap, |
| 272 | md2_finish_wrap, |
| 273 | mbedtls_md2_ret, |
| 274 | md2_ctx_alloc, |
| 275 | md2_ctx_free, |
| 276 | md2_clone_wrap, |
| 277 | md2_process_wrap, |
| 278 | }; |
| 279 | |
| 280 | #endif /* MBEDTLS_MD2_C */ |
| 281 | |
| 282 | /* |
| 283 | * MD-4 |
| 284 | */ |
| 285 | |
| 286 | #if defined(MBEDTLS_MD4_C) |
| 287 | |
| 288 | static int md4_starts_wrap( void *ctx ) |
| 289 | { |
| 290 | return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) ); |
| 291 | } |
| 292 | |
| 293 | static int md4_update_wrap( void *ctx, const unsigned char *input, |
| 294 | size_t ilen ) |
| 295 | { |
| 296 | return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) ); |
| 297 | } |
| 298 | |
| 299 | static int md4_finish_wrap( void *ctx, unsigned char *output ) |
| 300 | { |
| 301 | return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) ); |
| 302 | } |
| 303 | |
| 304 | static void *md4_ctx_alloc( void ) |
| 305 | { |
| 306 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) ); |
| 307 | |
| 308 | if( ctx != NULL ) |
| 309 | mbedtls_md4_init( (mbedtls_md4_context *) ctx ); |
| 310 | |
| 311 | return( ctx ); |
| 312 | } |
| 313 | |
| 314 | static void md4_ctx_free( void *ctx ) |
| 315 | { |
| 316 | mbedtls_md4_free( (mbedtls_md4_context *) ctx ); |
| 317 | mbedtls_free( ctx ); |
| 318 | } |
| 319 | |
| 320 | static void md4_clone_wrap( void *dst, const void *src ) |
| 321 | { |
| 322 | mbedtls_md4_clone( (mbedtls_md4_context *) dst, |
| 323 | (const mbedtls_md4_context *) src ); |
| 324 | } |
| 325 | |
| 326 | static int md4_process_wrap( void *ctx, const unsigned char *data ) |
| 327 | { |
| 328 | return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) ); |
| 329 | } |
| 330 | |
| 331 | const mbedtls_md_info_t mbedtls_md4_info = { |
| 332 | MBEDTLS_MD_MD4, |
| 333 | "MD4", |
| 334 | 16, |
| 335 | 64, |
| 336 | md4_starts_wrap, |
| 337 | md4_update_wrap, |
| 338 | md4_finish_wrap, |
| 339 | mbedtls_md4_ret, |
| 340 | md4_ctx_alloc, |
| 341 | md4_ctx_free, |
| 342 | md4_clone_wrap, |
| 343 | md4_process_wrap, |
| 344 | }; |
| 345 | |
| 346 | #endif /* MBEDTLS_MD4_C */ |
| 347 | |
| 348 | /* |
| 349 | * MD-5 |
| 350 | */ |
| 351 | |
| 352 | #if defined(MBEDTLS_MD5_C) |
| 353 | |
| 354 | static int md5_starts_wrap( void *ctx ) |
| 355 | { |
| 356 | return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) ); |
| 357 | } |
| 358 | |
| 359 | static int md5_update_wrap( void *ctx, const unsigned char *input, |
| 360 | size_t ilen ) |
| 361 | { |
| 362 | return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) ); |
| 363 | } |
| 364 | |
| 365 | static int md5_finish_wrap( void *ctx, unsigned char *output ) |
| 366 | { |
| 367 | return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) ); |
| 368 | } |
| 369 | |
| 370 | static void *md5_ctx_alloc( void ) |
| 371 | { |
| 372 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) ); |
| 373 | |
| 374 | if( ctx != NULL ) |
| 375 | mbedtls_md5_init( (mbedtls_md5_context *) ctx ); |
| 376 | |
| 377 | return( ctx ); |
| 378 | } |
| 379 | |
| 380 | static void md5_ctx_free( void *ctx ) |
| 381 | { |
| 382 | mbedtls_md5_free( (mbedtls_md5_context *) ctx ); |
| 383 | mbedtls_free( ctx ); |
| 384 | } |
| 385 | |
| 386 | static void md5_clone_wrap( void *dst, const void *src ) |
| 387 | { |
| 388 | mbedtls_md5_clone( (mbedtls_md5_context *) dst, |
| 389 | (const mbedtls_md5_context *) src ); |
| 390 | } |
| 391 | |
| 392 | static int md5_process_wrap( void *ctx, const unsigned char *data ) |
| 393 | { |
| 394 | return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) ); |
| 395 | } |
| 396 | |
| 397 | const mbedtls_md_info_t mbedtls_md5_info = { |
| 398 | MBEDTLS_MD_MD5, |
| 399 | "MD5", |
| 400 | 16, |
| 401 | 64, |
| 402 | md5_starts_wrap, |
| 403 | md5_update_wrap, |
| 404 | md5_finish_wrap, |
| 405 | mbedtls_md5_ret, |
| 406 | md5_ctx_alloc, |
| 407 | md5_ctx_free, |
| 408 | md5_clone_wrap, |
| 409 | md5_process_wrap, |
| 410 | }; |
| 411 | |
| 412 | #endif /* MBEDTLS_MD5_C */ |
| 413 | |
| 414 | /* |
| 415 | * RIPEMD-160 |
| 416 | */ |
| 417 | |
| 418 | #if defined(MBEDTLS_RIPEMD160_C) |
| 419 | |
| 420 | static int ripemd160_starts_wrap( void *ctx ) |
| 421 | { |
| 422 | return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) ); |
| 423 | } |
| 424 | |
| 425 | static int ripemd160_update_wrap( void *ctx, const unsigned char *input, |
| 426 | size_t ilen ) |
| 427 | { |
| 428 | return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx, |
| 429 | input, ilen ) ); |
| 430 | } |
| 431 | |
| 432 | static int ripemd160_finish_wrap( void *ctx, unsigned char *output ) |
| 433 | { |
| 434 | return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx, |
| 435 | output ) ); |
| 436 | } |
| 437 | |
| 438 | static void *ripemd160_ctx_alloc( void ) |
| 439 | { |
| 440 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) ); |
| 441 | |
| 442 | if( ctx != NULL ) |
| 443 | mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx ); |
| 444 | |
| 445 | return( ctx ); |
| 446 | } |
| 447 | |
| 448 | static void ripemd160_ctx_free( void *ctx ) |
| 449 | { |
| 450 | mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx ); |
| 451 | mbedtls_free( ctx ); |
| 452 | } |
| 453 | |
| 454 | static void ripemd160_clone_wrap( void *dst, const void *src ) |
| 455 | { |
| 456 | mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst, |
| 457 | (const mbedtls_ripemd160_context *) src ); |
| 458 | } |
| 459 | |
| 460 | static int ripemd160_process_wrap( void *ctx, const unsigned char *data ) |
| 461 | { |
| 462 | return( mbedtls_internal_ripemd160_process( |
| 463 | (mbedtls_ripemd160_context *) ctx, data ) ); |
| 464 | } |
| 465 | |
| 466 | const mbedtls_md_info_t mbedtls_ripemd160_info = { |
| 467 | MBEDTLS_MD_RIPEMD160, |
| 468 | "RIPEMD160", |
| 469 | 20, |
| 470 | 64, |
| 471 | ripemd160_starts_wrap, |
| 472 | ripemd160_update_wrap, |
| 473 | ripemd160_finish_wrap, |
| 474 | mbedtls_ripemd160_ret, |
| 475 | ripemd160_ctx_alloc, |
| 476 | ripemd160_ctx_free, |
| 477 | ripemd160_clone_wrap, |
| 478 | ripemd160_process_wrap, |
| 479 | }; |
| 480 | |
| 481 | #endif /* MBEDTLS_RIPEMD160_C */ |
| 482 | |
| 483 | /* |
| 484 | * SHA-1 |
| 485 | */ |
| 486 | |
| 487 | #if defined(MBEDTLS_SHA1_C) |
| 488 | |
| 489 | static int sha1_starts_wrap( void *ctx ) |
| 490 | { |
| 491 | return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) ); |
| 492 | } |
| 493 | |
| 494 | static int sha1_update_wrap( void *ctx, const unsigned char *input, |
| 495 | size_t ilen ) |
| 496 | { |
| 497 | return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx, |
| 498 | input, ilen ) ); |
| 499 | } |
| 500 | |
| 501 | static int sha1_finish_wrap( void *ctx, unsigned char *output ) |
| 502 | { |
| 503 | return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) ); |
| 504 | } |
| 505 | |
| 506 | static void *sha1_ctx_alloc( void ) |
| 507 | { |
| 508 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) ); |
| 509 | |
| 510 | if( ctx != NULL ) |
| 511 | mbedtls_sha1_init( (mbedtls_sha1_context *) ctx ); |
| 512 | |
| 513 | return( ctx ); |
| 514 | } |
| 515 | |
| 516 | static void sha1_clone_wrap( void *dst, const void *src ) |
| 517 | { |
| 518 | mbedtls_sha1_clone( (mbedtls_sha1_context *) dst, |
| 519 | (const mbedtls_sha1_context *) src ); |
| 520 | } |
| 521 | |
| 522 | static void sha1_ctx_free( void *ctx ) |
| 523 | { |
| 524 | mbedtls_sha1_free( (mbedtls_sha1_context *) ctx ); |
| 525 | mbedtls_free( ctx ); |
| 526 | } |
| 527 | |
| 528 | static int sha1_process_wrap( void *ctx, const unsigned char *data ) |
| 529 | { |
| 530 | return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx, |
| 531 | data ) ); |
| 532 | } |
| 533 | |
| 534 | const mbedtls_md_info_t mbedtls_sha1_info = { |
| 535 | MBEDTLS_MD_SHA1, |
| 536 | "SHA1", |
| 537 | 20, |
| 538 | 64, |
| 539 | sha1_starts_wrap, |
| 540 | sha1_update_wrap, |
| 541 | sha1_finish_wrap, |
| 542 | mbedtls_sha1_ret, |
| 543 | sha1_ctx_alloc, |
| 544 | sha1_ctx_free, |
| 545 | sha1_clone_wrap, |
| 546 | sha1_process_wrap, |
| 547 | }; |
| 548 | |
| 549 | #endif /* MBEDTLS_SHA1_C */ |
| 550 | |
| 551 | /* |
| 552 | * SHA-224 and SHA-256 |
| 553 | */ |
| 554 | |
| 555 | #if defined(MBEDTLS_SHA256_C) |
| 556 | |
| 557 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
| 558 | static int sha224_starts_wrap( void *ctx ) |
| 559 | { |
| 560 | return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) ); |
| 561 | } |
| 562 | #endif /* !MBEDTLS_SHA256_NO_SHA224 */ |
| 563 | |
| 564 | static int sha224_update_wrap( void *ctx, const unsigned char *input, |
| 565 | size_t ilen ) |
| 566 | { |
| 567 | return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx, |
| 568 | input, ilen ) ); |
| 569 | } |
| 570 | |
| 571 | static int sha224_finish_wrap( void *ctx, unsigned char *output ) |
| 572 | { |
| 573 | return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx, |
| 574 | output ) ); |
| 575 | } |
| 576 | |
| 577 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
| 578 | static int sha224_wrap( const unsigned char *input, size_t ilen, |
| 579 | unsigned char *output ) |
| 580 | { |
| 581 | return( mbedtls_sha256_ret( input, ilen, output, 1 ) ); |
| 582 | } |
| 583 | #endif /* !MBEDTLS_SHA256_NO_SHA224 */ |
| 584 | |
| 585 | static void *sha224_ctx_alloc( void ) |
| 586 | { |
| 587 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) ); |
| 588 | |
| 589 | if( ctx != NULL ) |
| 590 | mbedtls_sha256_init( (mbedtls_sha256_context *) ctx ); |
| 591 | |
| 592 | return( ctx ); |
| 593 | } |
| 594 | |
| 595 | static void sha224_ctx_free( void *ctx ) |
| 596 | { |
| 597 | mbedtls_sha256_free( (mbedtls_sha256_context *) ctx ); |
| 598 | mbedtls_free( ctx ); |
| 599 | } |
| 600 | |
| 601 | static void sha224_clone_wrap( void *dst, const void *src ) |
| 602 | { |
| 603 | mbedtls_sha256_clone( (mbedtls_sha256_context *) dst, |
| 604 | (const mbedtls_sha256_context *) src ); |
| 605 | } |
| 606 | |
| 607 | static int sha224_process_wrap( void *ctx, const unsigned char *data ) |
| 608 | { |
| 609 | return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx, |
| 610 | data ) ); |
| 611 | } |
| 612 | |
| 613 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
| 614 | const mbedtls_md_info_t mbedtls_sha224_info = { |
| 615 | MBEDTLS_MD_SHA224, |
| 616 | "SHA224", |
| 617 | 28, |
| 618 | 64, |
| 619 | sha224_starts_wrap, |
| 620 | sha224_update_wrap, |
| 621 | sha224_finish_wrap, |
| 622 | sha224_wrap, |
| 623 | sha224_ctx_alloc, |
| 624 | sha224_ctx_free, |
| 625 | sha224_clone_wrap, |
| 626 | sha224_process_wrap, |
| 627 | }; |
| 628 | #endif /* !MBEDTLS_SHA256_NO_SHA224 */ |
| 629 | |
| 630 | static int sha256_starts_wrap( void *ctx ) |
| 631 | { |
| 632 | return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) ); |
| 633 | } |
| 634 | |
| 635 | static int sha256_wrap( const unsigned char *input, size_t ilen, |
| 636 | unsigned char *output ) |
| 637 | { |
| 638 | return( mbedtls_sha256_ret( input, ilen, output, 0 ) ); |
| 639 | } |
| 640 | |
| 641 | const mbedtls_md_info_t mbedtls_sha256_info = |
| 642 | MBEDTLS_MD_INFO( MBEDTLS_MD_INFO_SHA256 ); |
| 643 | |
| 644 | #endif /* MBEDTLS_SHA256_C */ |
| 645 | |
| 646 | /* |
| 647 | * SHA-384 and SHA-512 |
| 648 | */ |
| 649 | |
| 650 | #if defined(MBEDTLS_SHA512_C) |
| 651 | |
| 652 | static int sha384_starts_wrap( void *ctx ) |
| 653 | { |
| 654 | return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) ); |
| 655 | } |
| 656 | |
| 657 | static int sha384_update_wrap( void *ctx, const unsigned char *input, |
| 658 | size_t ilen ) |
| 659 | { |
| 660 | return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx, |
| 661 | input, ilen ) ); |
| 662 | } |
| 663 | |
| 664 | static int sha384_finish_wrap( void *ctx, unsigned char *output ) |
| 665 | { |
| 666 | return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx, |
| 667 | output ) ); |
| 668 | } |
| 669 | |
| 670 | static int sha384_wrap( const unsigned char *input, size_t ilen, |
| 671 | unsigned char *output ) |
| 672 | { |
| 673 | return( mbedtls_sha512_ret( input, ilen, output, 1 ) ); |
| 674 | } |
| 675 | |
| 676 | static void *sha384_ctx_alloc( void ) |
| 677 | { |
| 678 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) ); |
| 679 | |
| 680 | if( ctx != NULL ) |
| 681 | mbedtls_sha512_init( (mbedtls_sha512_context *) ctx ); |
| 682 | |
| 683 | return( ctx ); |
| 684 | } |
| 685 | |
| 686 | static void sha384_ctx_free( void *ctx ) |
| 687 | { |
| 688 | mbedtls_sha512_free( (mbedtls_sha512_context *) ctx ); |
| 689 | mbedtls_free( ctx ); |
| 690 | } |
| 691 | |
| 692 | static void sha384_clone_wrap( void *dst, const void *src ) |
| 693 | { |
| 694 | mbedtls_sha512_clone( (mbedtls_sha512_context *) dst, |
| 695 | (const mbedtls_sha512_context *) src ); |
| 696 | } |
| 697 | |
| 698 | static int sha384_process_wrap( void *ctx, const unsigned char *data ) |
| 699 | { |
| 700 | return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx, |
| 701 | data ) ); |
| 702 | } |
| 703 | |
| 704 | const mbedtls_md_info_t mbedtls_sha384_info = { |
| 705 | MBEDTLS_MD_SHA384, |
| 706 | "SHA384", |
| 707 | 48, |
| 708 | 128, |
| 709 | sha384_starts_wrap, |
| 710 | sha384_update_wrap, |
| 711 | sha384_finish_wrap, |
| 712 | sha384_wrap, |
| 713 | sha384_ctx_alloc, |
| 714 | sha384_ctx_free, |
| 715 | sha384_clone_wrap, |
| 716 | sha384_process_wrap, |
| 717 | }; |
| 718 | |
| 719 | static int sha512_starts_wrap( void *ctx ) |
| 720 | { |
| 721 | return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) ); |
| 722 | } |
| 723 | |
| 724 | static int sha512_wrap( const unsigned char *input, size_t ilen, |
| 725 | unsigned char *output ) |
| 726 | { |
| 727 | return( mbedtls_sha512_ret( input, ilen, output, 0 ) ); |
| 728 | } |
| 729 | |
| 730 | const mbedtls_md_info_t mbedtls_sha512_info = { |
| 731 | MBEDTLS_MD_SHA512, |
| 732 | "SHA512", |
| 733 | 64, |
| 734 | 128, |
| 735 | sha512_starts_wrap, |
| 736 | sha384_update_wrap, |
| 737 | sha384_finish_wrap, |
| 738 | sha512_wrap, |
| 739 | sha384_ctx_alloc, |
| 740 | sha384_ctx_free, |
| 741 | sha384_clone_wrap, |
| 742 | sha384_process_wrap, |
| 743 | }; |
| 744 | |
| 745 | #endif /* MBEDTLS_SHA512_C */ |
| 746 | |
| 747 | /* |
| 748 | * Getter functions for MD info structure. |
| 749 | */ |
| 750 | |
| 751 | static inline mbedtls_md_type_t mbedtls_md_info_type( |
| 752 | mbedtls_md_handle_t info ) |
| 753 | { |
| 754 | return( info->type ); |
| 755 | } |
| 756 | |
| 757 | static inline const char * mbedtls_md_info_name( |
| 758 | mbedtls_md_handle_t info ) |
| 759 | { |
| 760 | return( info->name ); |
| 761 | } |
| 762 | |
| 763 | static inline int mbedtls_md_info_size( |
| 764 | mbedtls_md_handle_t info ) |
| 765 | { |
| 766 | return( info->size ); |
| 767 | } |
| 768 | |
| 769 | static inline int mbedtls_md_info_block_size( |
| 770 | mbedtls_md_handle_t info ) |
| 771 | { |
| 772 | return( info->block_size ); |
| 773 | } |
| 774 | |
| 775 | static inline mbedtls_md_starts_func_t *mbedtls_md_info_starts_func( |
| 776 | mbedtls_md_handle_t info ) |
| 777 | { |
| 778 | return( info->starts_func ); |
| 779 | } |
| 780 | |
| 781 | static inline mbedtls_md_update_func_t *mbedtls_md_info_update_func( |
| 782 | mbedtls_md_handle_t info ) |
| 783 | { |
| 784 | return( info->update_func ); |
| 785 | } |
| 786 | |
| 787 | static inline mbedtls_md_finish_func_t *mbedtls_md_info_finish_func( |
| 788 | mbedtls_md_handle_t info ) |
| 789 | { |
| 790 | return( info->finish_func ); |
| 791 | } |
| 792 | |
| 793 | static inline mbedtls_md_digest_func_t *mbedtls_md_info_digest_func( |
| 794 | mbedtls_md_handle_t info ) |
| 795 | { |
| 796 | return( info->digest_func ); |
| 797 | } |
| 798 | |
| 799 | static inline mbedtls_md_ctx_alloc_func_t *mbedtls_md_info_ctx_alloc_func( |
| 800 | mbedtls_md_handle_t info ) |
| 801 | { |
| 802 | return( info->ctx_alloc_func ); |
| 803 | } |
| 804 | |
| 805 | static inline mbedtls_md_ctx_free_func_t *mbedtls_md_info_ctx_free_func( |
| 806 | mbedtls_md_handle_t info ) |
| 807 | { |
| 808 | return( info->ctx_free_func ); |
| 809 | } |
| 810 | |
| 811 | static inline mbedtls_md_clone_func_t *mbedtls_md_info_clone_func( |
| 812 | mbedtls_md_handle_t info ) |
| 813 | { |
| 814 | return( info->clone_func ); |
| 815 | } |
| 816 | |
| 817 | static inline mbedtls_md_process_func_t *mbedtls_md_info_process_func( |
| 818 | mbedtls_md_handle_t info ) |
| 819 | { |
| 820 | return( info->process_func ); |
| 821 | } |
Hanno Becker | 0e7fc31 | 2019-07-17 11:23:12 +0100 | [diff] [blame] | 822 | |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 823 | /* |
| 824 | * Reminder: update profiles in x509_crt.c when adding a new hash! |
| 825 | */ |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 826 | static const int supported_digests[] = { |
| 827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | #if defined(MBEDTLS_SHA512_C) |
| 829 | MBEDTLS_MD_SHA512, |
| 830 | MBEDTLS_MD_SHA384, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 831 | #endif |
| 832 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | #if defined(MBEDTLS_SHA256_C) |
| 834 | MBEDTLS_MD_SHA256, |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 835 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | MBEDTLS_MD_SHA224, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 837 | #endif |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 838 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | #if defined(MBEDTLS_SHA1_C) |
| 841 | MBEDTLS_MD_SHA1, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 842 | #endif |
| 843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | #if defined(MBEDTLS_RIPEMD160_C) |
| 845 | MBEDTLS_MD_RIPEMD160, |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 846 | #endif |
| 847 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 848 | #if defined(MBEDTLS_MD5_C) |
| 849 | MBEDTLS_MD_MD5, |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 850 | #endif |
| 851 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 852 | #if defined(MBEDTLS_MD4_C) |
| 853 | MBEDTLS_MD_MD4, |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 854 | #endif |
| 855 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 856 | #if defined(MBEDTLS_MD2_C) |
| 857 | MBEDTLS_MD_MD2, |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 858 | #endif |
| 859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | MBEDTLS_MD_NONE |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 861 | }; |
| 862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 863 | const int *mbedtls_md_list( void ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 864 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 865 | return( supported_digests ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 868 | mbedtls_md_handle_t mbedtls_md_info_from_string( const char *md_name ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 869 | { |
| 870 | if( NULL == md_name ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 871 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 872 | |
| 873 | /* Get the appropriate digest information */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | #if defined(MBEDTLS_MD2_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 875 | if( !strcmp( "MD2", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 876 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 877 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #if defined(MBEDTLS_MD4_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 879 | if( !strcmp( "MD4", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 881 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | #if defined(MBEDTLS_MD5_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 883 | if( !strcmp( "MD5", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 885 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | #if defined(MBEDTLS_RIPEMD160_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 887 | if( !strcmp( "RIPEMD160", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 889 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 890 | #if defined(MBEDTLS_SHA1_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 891 | if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 893 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 894 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 895 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 896 | if( !strcmp( "SHA224", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 897 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 ); |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 898 | #endif |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 899 | if( !strcmp( "SHA256", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 900 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 901 | #endif /* MBEDTLS_SHA256_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 902 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 903 | if( !strcmp( "SHA384", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 ); |
Manuel Pégourié-Gonnard | cb46fd8 | 2015-05-28 17:06:07 +0200 | [diff] [blame] | 905 | if( !strcmp( "SHA512", md_name ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 907 | #endif |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 908 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 911 | mbedtls_md_handle_t mbedtls_md_info_from_type( mbedtls_md_type_t md_type ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 912 | { |
| 913 | switch( md_type ) |
| 914 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | #if defined(MBEDTLS_MD2_C) |
| 916 | case MBEDTLS_MD_MD2: |
| 917 | return( &mbedtls_md2_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 918 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | #if defined(MBEDTLS_MD4_C) |
| 920 | case MBEDTLS_MD_MD4: |
| 921 | return( &mbedtls_md4_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 922 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | #if defined(MBEDTLS_MD5_C) |
| 924 | case MBEDTLS_MD_MD5: |
| 925 | return( &mbedtls_md5_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 926 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | #if defined(MBEDTLS_RIPEMD160_C) |
| 928 | case MBEDTLS_MD_RIPEMD160: |
| 929 | return( &mbedtls_ripemd160_info ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 930 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | #if defined(MBEDTLS_SHA1_C) |
| 932 | case MBEDTLS_MD_SHA1: |
| 933 | return( &mbedtls_sha1_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 934 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 936 | #if !defined(MBEDTLS_SHA256_NO_SHA224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | case MBEDTLS_MD_SHA224: |
| 938 | return( &mbedtls_sha224_info ); |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 939 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 940 | case MBEDTLS_MD_SHA256: |
| 941 | return( &mbedtls_sha256_info ); |
Manuel Pégourié-Gonnard | 394c5fb | 2019-07-16 15:57:36 +0200 | [diff] [blame] | 942 | #endif /* MBEDTLS_SHA256_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | #if defined(MBEDTLS_SHA512_C) |
| 944 | case MBEDTLS_MD_SHA384: |
| 945 | return( &mbedtls_sha384_info ); |
| 946 | case MBEDTLS_MD_SHA512: |
| 947 | return( &mbedtls_sha512_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 948 | #endif |
| 949 | default: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 950 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | void mbedtls_md_init( mbedtls_md_context_t *ctx ) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 955 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | memset( ctx, 0, sizeof( mbedtls_md_context_t ) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | void mbedtls_md_free( mbedtls_md_context_t *ctx ) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 960 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 961 | if( ctx == NULL || mbedtls_md_get_handle( ctx ) == MBEDTLS_MD_INVALID_HANDLE ) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 962 | return; |
| 963 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 964 | if( ctx->md_ctx != NULL ) |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 965 | { |
| 966 | mbedtls_md_info_ctx_free_func( |
| 967 | mbedtls_md_get_handle( ctx ) )( ctx->md_ctx ); |
| 968 | } |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 969 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 970 | if( ctx->hmac_ctx != NULL ) |
| 971 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 972 | mbedtls_platform_zeroize( ctx->hmac_ctx, |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 973 | 2 * mbedtls_md_info_block_size( mbedtls_md_get_handle( ctx ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | mbedtls_free( ctx->hmac_ctx ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 977 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 980 | int mbedtls_md_clone( mbedtls_md_context_t *dst, |
| 981 | const mbedtls_md_context_t *src ) |
| 982 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 983 | if( dst == NULL || mbedtls_md_get_handle( dst ) == MBEDTLS_MD_INVALID_HANDLE || |
| 984 | src == NULL || mbedtls_md_get_handle( src ) == MBEDTLS_MD_INVALID_HANDLE || |
| 985 | mbedtls_md_get_handle( dst ) != mbedtls_md_get_handle( src ) ) |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 986 | { |
| 987 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 988 | } |
| 989 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 990 | mbedtls_md_info_clone_func( mbedtls_md_get_handle( dst ) ) |
| 991 | ( dst->md_ctx, src->md_ctx ); |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 992 | return( 0 ); |
| 993 | } |
| 994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 996 | int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info ) |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | return mbedtls_md_setup( ctx, md_info, 1 ); |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 999 | } |
| 1000 | #endif |
| 1001 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1002 | int mbedtls_md_setup( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info, int hmac ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1003 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1004 | if( md_info == MBEDTLS_MD_INVALID_HANDLE || ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1005 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1006 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1007 | ctx->md_ctx = mbedtls_md_info_ctx_alloc_func( md_info )(); |
| 1008 | if( ctx->md_ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1009 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1010 | |
Manuel Pégourié-Gonnard | 4063ceb | 2015-03-25 16:08:53 +0100 | [diff] [blame] | 1011 | if( hmac != 0 ) |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1012 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1013 | ctx->hmac_ctx = mbedtls_calloc( 2, |
| 1014 | mbedtls_md_info_block_size( md_info ) ); |
Manuel Pégourié-Gonnard | 4063ceb | 2015-03-25 16:08:53 +0100 | [diff] [blame] | 1015 | if( ctx->hmac_ctx == NULL ) |
| 1016 | { |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1017 | mbedtls_md_info_ctx_free_func( md_info )( ctx->md_ctx ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4063ceb | 2015-03-25 16:08:53 +0100 | [diff] [blame] | 1019 | } |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1022 | ctx->md_info = md_info; |
| 1023 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1024 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1027 | int mbedtls_md_starts( mbedtls_md_context_t *ctx ) |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 1028 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1029 | mbedtls_md_handle_t md_info; |
| 1030 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 1032 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1033 | md_info = mbedtls_md_get_handle( ctx ); |
| 1034 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1035 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1036 | |
| 1037 | return( mbedtls_md_info_starts_func( md_info )( ctx->md_ctx ) ); |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1041 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1042 | mbedtls_md_handle_t md_info; |
| 1043 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1045 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1046 | md_info = mbedtls_md_get_handle( ctx ); |
| 1047 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1048 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1049 | |
| 1050 | return( mbedtls_md_info_update_func( md_info )( ctx->md_ctx, |
| 1051 | input, ilen ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1054 | int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1055 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1056 | mbedtls_md_handle_t md_info; |
| 1057 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1059 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1060 | md_info = mbedtls_md_get_handle( ctx ); |
| 1061 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1062 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1063 | |
| 1064 | return( mbedtls_md_info_finish_func( md_info )( ctx->md_ctx, |
| 1065 | output ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1068 | int mbedtls_md( mbedtls_md_handle_t md_info, const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1069 | unsigned char *output ) |
| 1070 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1071 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1072 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1073 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1074 | return( mbedtls_md_info_digest_func( md_info )( |
| 1075 | input, ilen, output) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1078 | #if defined(MBEDTLS_FS_IO) |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1079 | int mbedtls_md_file( mbedtls_md_handle_t md_info, const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1080 | { |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 1081 | int ret; |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1082 | FILE *f; |
| 1083 | size_t n; |
| 1084 | mbedtls_md_context_t ctx; |
| 1085 | unsigned char buf[1024]; |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 1086 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1087 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1089 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1090 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
Manuel Pégourié-Gonnard | bcc0308 | 2015-06-24 00:09:29 +0200 | [diff] [blame] | 1091 | return( MBEDTLS_ERR_MD_FILE_IO_ERROR ); |
| 1092 | |
| 1093 | mbedtls_md_init( &ctx ); |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1094 | |
| 1095 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 1096 | goto cleanup; |
| 1097 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1098 | ret = mbedtls_md_info_starts_func( md_info )( ctx.md_ctx ); |
| 1099 | if( ret != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1100 | goto cleanup; |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1101 | |
| 1102 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1103 | { |
| 1104 | ret = mbedtls_md_info_update_func( md_info )( ctx.md_ctx, |
| 1105 | buf, n ); |
| 1106 | if( ret != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1107 | goto cleanup; |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1108 | } |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1109 | |
| 1110 | if( ferror( f ) != 0 ) |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1111 | { |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1112 | ret = MBEDTLS_ERR_MD_FILE_IO_ERROR; |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1113 | } |
Andres Amaya Garcia | eb132b6 | 2017-06-23 16:30:31 +0100 | [diff] [blame] | 1114 | else |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1115 | { |
| 1116 | ret = mbedtls_md_info_finish_func( md_info )( ctx.md_ctx, |
| 1117 | output ); |
| 1118 | } |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1119 | |
| 1120 | cleanup: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1121 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1122 | fclose( f ); |
| 1123 | mbedtls_md_free( &ctx ); |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 1124 | |
Paul Bakker | 8913f82 | 2012-01-14 18:07:41 +0000 | [diff] [blame] | 1125 | return( ret ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1126 | } |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 1127 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1130 | { |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1131 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | unsigned char sum[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1133 | unsigned char *ipad, *opad; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1134 | size_t i; |
| 1135 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1136 | mbedtls_md_starts_func_t *starts; |
| 1137 | mbedtls_md_update_func_t *update; |
| 1138 | mbedtls_md_finish_func_t *finish; |
| 1139 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1140 | mbedtls_md_handle_t md_info; |
| 1141 | |
| 1142 | if( ctx == NULL || ctx->hmac_ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1143 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1144 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1145 | md_info = mbedtls_md_get_handle( ctx ); |
| 1146 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1147 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1148 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1149 | starts = mbedtls_md_info_starts_func( md_info ); |
| 1150 | update = mbedtls_md_info_update_func( md_info ); |
| 1151 | finish = mbedtls_md_info_finish_func( md_info ); |
| 1152 | |
| 1153 | if( keylen > (size_t) mbedtls_md_info_block_size( md_info ) ) |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1154 | { |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1155 | if( ( ret = starts( ctx->md_ctx ) ) != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1156 | goto cleanup; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1157 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1158 | if( ( ret = update( ctx->md_ctx, key, keylen ) ) ) |
| 1159 | goto cleanup; |
| 1160 | |
| 1161 | if( ( ret = finish( ctx->md_ctx, sum ) ) != 0 ) |
| 1162 | goto cleanup; |
| 1163 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1164 | keylen = mbedtls_md_info_size( md_info ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1165 | key = sum; |
| 1166 | } |
| 1167 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1168 | ipad = (unsigned char *) ctx->hmac_ctx; |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1169 | opad = (unsigned char *) ctx->hmac_ctx + |
| 1170 | mbedtls_md_info_block_size( md_info ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1171 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1172 | memset( ipad, 0x36, mbedtls_md_info_block_size( md_info ) ); |
| 1173 | memset( opad, 0x5C, mbedtls_md_info_block_size( md_info ) ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1174 | |
| 1175 | for( i = 0; i < keylen; i++ ) |
| 1176 | { |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1177 | ipad[i] = (unsigned char)( ipad[i] ^ key[i] ); |
| 1178 | opad[i] = (unsigned char)( opad[i] ^ key[i] ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1181 | if( ( ret = starts( ctx->md_ctx ) ) != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1182 | goto cleanup; |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1183 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1184 | if( ( ret = update( ctx->md_ctx, ipad, |
| 1185 | mbedtls_md_info_block_size( md_info ) ) ) != 0 ) |
| 1186 | { |
Andres Amaya Garcia | 42e5e10 | 2017-07-20 16:27:03 +0100 | [diff] [blame] | 1187 | goto cleanup; |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1188 | } |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1189 | |
| 1190 | cleanup: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1191 | mbedtls_platform_zeroize( sum, sizeof( sum ) ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1192 | |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1193 | return( ret ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1196 | int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, |
| 1197 | const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1198 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1199 | mbedtls_md_handle_t md_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1200 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1201 | if( ctx == NULL || ctx->hmac_ctx == NULL ) |
| 1202 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1203 | |
| 1204 | md_info = mbedtls_md_get_handle( ctx ); |
| 1205 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1206 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1207 | |
| 1208 | return( mbedtls_md_info_update_func( md_info )( |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1209 | ctx->md_ctx, input, ilen ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1213 | { |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1214 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1215 | unsigned char tmp[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1216 | unsigned char *opad; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1217 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1218 | mbedtls_md_starts_func_t *starts; |
| 1219 | mbedtls_md_update_func_t *update; |
| 1220 | mbedtls_md_finish_func_t *finish; |
| 1221 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1222 | mbedtls_md_handle_t md_info; |
| 1223 | |
| 1224 | if( ctx == NULL || ctx->hmac_ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1226 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1227 | md_info = mbedtls_md_get_handle( ctx ); |
| 1228 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1229 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1230 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1231 | starts = mbedtls_md_info_starts_func( md_info ); |
| 1232 | update = mbedtls_md_info_update_func( md_info ); |
| 1233 | finish = mbedtls_md_info_finish_func( md_info ); |
| 1234 | |
| 1235 | opad = (unsigned char *) ctx->hmac_ctx + |
| 1236 | mbedtls_md_info_block_size( md_info ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1237 | |
| 1238 | if( ( ret = finish( ctx->md_ctx, tmp ) ) != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1239 | return( ret ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1240 | |
| 1241 | if( ( ret = starts( ctx->md_ctx ) ) != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1242 | return( ret ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1243 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1244 | if( ( ret = update( ctx->md_ctx, opad, |
| 1245 | mbedtls_md_info_block_size( md_info ) ) ) != 0 ) |
| 1246 | { |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1247 | return( ret ); |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1248 | } |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1249 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1250 | if( ( ret = update( ctx->md_ctx, tmp, |
| 1251 | mbedtls_md_info_size( md_info ) ) ) != 0 ) |
| 1252 | { |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1253 | return( ret ); |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1254 | } |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1255 | |
| 1256 | if( ( ret = finish( ctx->md_ctx, output ) ) != 0 ) |
| 1257 | return( ret ); |
| 1258 | |
| 1259 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1263 | { |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1264 | int ret; |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1265 | unsigned char *ipad; |
| 1266 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1267 | mbedtls_md_handle_t md_info; |
| 1268 | |
| 1269 | if( ctx == NULL || ctx->hmac_ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1270 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1271 | |
| 1272 | md_info = mbedtls_md_get_handle( ctx ); |
| 1273 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1274 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1275 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 1276 | ipad = (unsigned char *) ctx->hmac_ctx; |
| 1277 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1278 | ret = mbedtls_md_info_starts_func( md_info )( ctx->md_ctx ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1279 | if( ret != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1280 | return( ret ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1281 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1282 | ret = mbedtls_md_info_update_func( md_info )( |
| 1283 | ctx->md_ctx, ipad, |
| 1284 | mbedtls_md_info_block_size( md_info ) ); |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1285 | return( ret ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1288 | int mbedtls_md_hmac( mbedtls_md_handle_t md_info, |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1289 | const unsigned char *key, size_t keylen, |
| 1290 | const unsigned char *input, size_t ilen, |
| 1291 | unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | mbedtls_md_context_t ctx; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1294 | int ret; |
| 1295 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1296 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1297 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | mbedtls_md_init( &ctx ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1301 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 ) |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1302 | goto cleanup; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1303 | |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1304 | if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 ) |
| 1305 | goto cleanup; |
| 1306 | if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 ) |
| 1307 | goto cleanup; |
Andres Amaya Garcia | aa464ef | 2017-07-21 14:21:53 +0100 | [diff] [blame] | 1308 | if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 ) |
| 1309 | goto cleanup; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 1310 | |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1311 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | mbedtls_md_free( &ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1313 | |
Andres Amaya Garcia | 0dd4fa0 | 2017-06-28 14:16:07 +0100 | [diff] [blame] | 1314 | return( ret ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 1318 | { |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1319 | mbedtls_md_handle_t md_info; |
| 1320 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1321 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 1322 | |
Hanno Becker | d3827c7 | 2019-09-03 12:56:37 +0100 | [diff] [blame] | 1323 | md_info = mbedtls_md_get_handle( ctx ); |
| 1324 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
| 1325 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 1326 | |
| 1327 | return( mbedtls_md_info_process_func( md_info )( |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1328 | ctx->md_ctx, data ) ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1331 | unsigned char mbedtls_md_get_size( mbedtls_md_handle_t md_info ) |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1332 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1333 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1334 | return( 0 ); |
| 1335 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1336 | return mbedtls_md_info_size( md_info ); |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1339 | mbedtls_md_type_t mbedtls_md_get_type( mbedtls_md_handle_t md_info ) |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1340 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1341 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1342 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1343 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1344 | return mbedtls_md_info_type( md_info ); |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1347 | const char *mbedtls_md_get_name( mbedtls_md_handle_t md_info ) |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1348 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1349 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1350 | return( NULL ); |
| 1351 | |
Hanno Becker | 530387e | 2019-07-17 14:10:26 +0100 | [diff] [blame] | 1352 | return mbedtls_md_info_name( md_info ); |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | #endif /* MBEDTLS_MD_C */ |