sha256_mbedtls.c (3934B)
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/sha256_mbedtls.c 42 * @brief Wrapper for SHA-256 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 "sha256_mbedtls.h" 52 53 54 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1) void 55 mhd_SHA256_mtls_init (struct mhd_Sha256CtxMtls *ctx) 56 { 57 mbedtls_sha256_init (&(ctx->mbed_ctx)); 58 /* The second argument must be zero to calculate SHA-256 (not SHA-224) */ 59 ctx->ext_error = (0 != mbedtls_sha256_starts (&(ctx->mbed_ctx), 60 0)); 61 } 62 63 64 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 65 mhd_SHA256_mtls_reset (struct mhd_Sha256CtxMtls *ctx) 66 { 67 if (ctx->ext_error) 68 return; 69 70 /* The second argument must be zero to calculate SHA-256 (not SHA-224) */ 71 ctx->ext_error = (0 != mbedtls_sha256_starts (&(ctx->mbed_ctx), 72 0)); 73 } 74 75 76 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 77 MHD_FN_PAR_IN_SIZE_ (3, 2) void 78 mhd_SHA256_mtls_update (struct mhd_Sha256CtxMtls *restrict ctx, 79 size_t size, 80 const void *restrict data) 81 { 82 #ifndef MHD_UNIT_TESTING 83 mhd_assert (0 != size); 84 #endif 85 86 if (ctx->ext_error) 87 return; 88 89 ctx->ext_error = (0 != mbedtls_sha256_update (&(ctx->mbed_ctx), 90 (const unsigned char *)data, 91 size)); 92 } 93 94 95 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) 96 MHD_FN_PAR_OUT_ (2) void 97 mhd_SHA256_mtls_finish ( 98 struct mhd_Sha256CtxMtls *restrict ctx, 99 uint8_t digest[MHD_FN_PAR_FIX_ARR_SIZE_ (mhd_SHA256_DIGEST_SIZE)]) 100 { 101 if (ctx->ext_error) 102 return; 103 104 ctx->ext_error = (0 != mbedtls_sha256_finish (&(ctx->mbed_ctx), 105 (unsigned char *)digest)); 106 } 107 108 109 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void 110 mhd_SHA256_mtls_deinit (struct mhd_Sha256CtxMtls *ctx) 111 { 112 mbedtls_sha256_free (&(ctx->mbed_ctx)); 113 }