md5_openssl.c (4168B)
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_openssl.c 42 * @brief Wrapper for MD5 calculation performed by OpenSSL backend 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 <openssl/evp.h> 52 53 #include "md5_openssl.h" 54 55 56 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1) void 57 mhd_MD5_ossl_init (struct mhd_Md5CtxOssl *ctx) 58 { 59 ctx->ossl_ctx = EVP_MD_CTX_new (); 60 ctx->ext_error = (NULL == ctx->ossl_ctx); 61 62 mhd_MD5_ossl_reset (ctx); 63 } 64 65 66 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 67 mhd_MD5_ossl_reset (struct mhd_Md5CtxOssl *ctx) 68 { 69 if (ctx->ext_error) 70 return; 71 72 mhd_assert (NULL != ctx->ossl_ctx); 73 74 ctx->ext_error = (0 == EVP_DigestInit_ex (ctx->ossl_ctx, 75 EVP_md5 (), 76 NULL)); 77 } 78 79 80 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 81 MHD_FN_PAR_IN_SIZE_ (3, 2) void 82 mhd_MD5_ossl_update (struct mhd_Md5CtxOssl *restrict ctx, 83 size_t size, 84 const void *restrict data) 85 { 86 #ifndef MHD_UNIT_TESTING 87 mhd_assert (0 != size); 88 #endif 89 90 if (ctx->ext_error) 91 return; 92 93 mhd_assert (NULL != ctx->ossl_ctx); 94 95 ctx->ext_error = (0 == EVP_DigestUpdate (ctx->ossl_ctx, 96 data, 97 size)); 98 } 99 100 101 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 102 MHD_FN_PAR_OUT_ (2) void 103 mhd_MD5_ossl_finish ( 104 struct mhd_Md5CtxOssl *restrict ctx, 105 uint8_t digest[MHD_FN_PAR_FIX_ARR_SIZE_ (mhd_MD5_DIGEST_SIZE)]) 106 { 107 if (!ctx->ext_error) 108 { 109 #ifndef NDEBUG 110 unsigned int len; 111 112 mhd_assert (NULL != ctx->ossl_ctx); 113 114 ctx->ext_error = (0 == EVP_DigestFinal_ex (ctx->ossl_ctx, 115 digest, 116 &len)); 117 mhd_assert ((ctx->ext_error) || (mhd_MD5_DIGEST_SIZE == len)); 118 #else 119 ctx->ext_error = (0 == EVP_DigestFinal_ex (ctx->ossl_ctx, 120 digest, 121 NULL)); 122 #endif 123 } 124 } 125 126 127 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 128 mhd_MD5_ossl_deinit (struct mhd_Md5CtxOssl *ctx) 129 { 130 EVP_MD_CTX_free (ctx->ossl_ctx); 131 ctx->ossl_ctx = NULL; 132 }