md5_mbedtls.c (3604B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2026 Evgeny Grin (Karlson2k) 5 Copyright (C) 2025 Christian Grothoff 6 7 GNU libmicrohttpd is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public 9 License as published by the Free Software Foundation; either 10 version 2.1 of the License, or (at your option) any later version. 11 12 GNU libmicrohttpd is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Lesser General Public License for more details. 16 17 Alternatively, you can redistribute GNU libmicrohttpd and/or 18 modify it under the terms of the GNU General Public License as 19 published by the Free Software Foundation; either version 2 of 20 the License, or (at your option) any later version, together 21 with the eCos exception, as follows: 22 23 As a special exception, if other files instantiate templates or 24 use macros or inline functions from this file, or you compile this 25 file and link it with other works to produce a work based on this 26 file, this file does not by itself cause the resulting work to be 27 covered by the GNU General Public License. However the source code 28 for this file must still be made available in accordance with 29 section (3) of the GNU General Public License v2. 30 31 This exception does not invalidate any other reasons why a work 32 based on this file might be covered by the GNU General Public 33 License. 34 35 You should have received copies of the GNU Lesser General Public 36 License and the GNU General Public License along with this library; 37 if not, see <https://www.gnu.org/licenses/>. 38 */ 39 40 /** 41 * @file src/mhd2/md5_mbedtls.c 42 * @brief Wrapper for MD5 calculation performed by mbedTLS library 43 * @author Karlson2k (Evgeny Grin) 44 * @author Christian Grothoff 45 */ 46 47 #include "mhd_sys_options.h" 48 49 #include "mhd_assert.h" 50 51 #include "md5_mbedtls.h" 52 53 54 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1) void 55 mhd_MD5_mtls_init (struct mhd_Md5CtxMtls *ctx) 56 { 57 mbedtls_md5_init (&(ctx->mbed_ctx)); 58 ctx->ext_error = (0 != mbedtls_md5_starts (&(ctx->mbed_ctx))); 59 } 60 61 62 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 63 mhd_MD5_mtls_reset (struct mhd_Md5CtxMtls *ctx) 64 { 65 if (ctx->ext_error) 66 return; 67 68 ctx->ext_error = (0 != mbedtls_md5_starts (&(ctx->mbed_ctx))); 69 } 70 71 72 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 73 MHD_FN_PAR_IN_SIZE_ (3, 2) void 74 mhd_MD5_mtls_update (struct mhd_Md5CtxMtls *restrict ctx, 75 size_t size, 76 const void *restrict data) 77 { 78 #ifndef MHD_UNIT_TESTING 79 mhd_assert (0 != size); 80 #endif 81 82 if (ctx->ext_error) 83 return; 84 85 ctx->ext_error = (0 != mbedtls_md5_update (&(ctx->mbed_ctx), 86 (const unsigned char *)data, 87 size)); 88 } 89 90 91 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 92 MHD_FN_PAR_OUT_ (2) void 93 mhd_MD5_mtls_finish ( 94 struct mhd_Md5CtxMtls *restrict ctx, 95 uint8_t digest[MHD_FN_PAR_FIX_ARR_SIZE_ (mhd_MD5_DIGEST_SIZE)]) 96 { 97 if (ctx->ext_error) 98 return; 99 100 ctx->ext_error = (0 != mbedtls_md5_finish (&(ctx->mbed_ctx), 101 (unsigned char *)digest)); 102 } 103 104 105 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 106 mhd_MD5_mtls_deinit (struct mhd_Md5CtxMtls *ctx) 107 { 108 mbedtls_md5_free (&(ctx->mbed_ctx)); 109 }