From dfd55459d705eab2a9087849a49bb8d706149da3 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 12 Aug 2026 09:16:58 +0100 Subject: [PATCH] NoTLS: Use SHA1 for digest computations Removed: include/coap3/coap_hashkey_internal.h src/coap_hashkey.c --- CMakeLists.txt | 1 - Makefile.am | 2 - examples/lwip/Makefile | 1 - examples/riot/pkg_libcoap/Makefile.libcoap | 1 - include/coap3/coap_cache_internal.h | 2 +- include/coap3/coap_hashkey_internal.h | 65 ---------------------- include/coap3/coap_internal.h | 1 - man/coap_cache.txt.in | 2 +- src/coap_hashkey.c | 32 ----------- src/coap_notls.c | 26 ++++----- src/coap_sha1.c | 2 +- win32/libcoap.vcxproj | 2 - win32/libcoap.vcxproj.filters | 6 -- 13 files changed, 13 insertions(+), 130 deletions(-) delete mode 100644 include/coap3/coap_hashkey_internal.h delete mode 100644 src/coap_hashkey.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ad64e61f..a58acdcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -834,7 +834,6 @@ target_sources( ${CMAKE_CURRENT_LIST_DIR}/src/coap_dtls.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_encode.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_event.c - ${CMAKE_CURRENT_LIST_DIR}/src/coap_hashkey.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_io.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_io_posix.c ${CMAKE_CURRENT_LIST_DIR}/src/coap_layers.c diff --git a/Makefile.am b/Makefile.am index 2e67c753..7446d09f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -118,7 +118,6 @@ EXTRA_DIST = \ include/coap$(LIBCOAP_API_VERSION)/coap_debug_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_dgrm_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_dtls_internal.h \ - include/coap$(LIBCOAP_API_VERSION)/coap_hashkey_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_io_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_layers_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_mutex_internal.h \ @@ -220,7 +219,6 @@ libcoap_@LIBCOAP_NAME_SUFFIX@_la_SOURCES = \ src/coap_dtls.c \ src/coap_encode.c \ src/coap_event.c \ - src/coap_hashkey.c \ src/coap_gnutls.c \ src/coap_io.c \ src/coap_io_posix.c \ diff --git a/examples/lwip/Makefile b/examples/lwip/Makefile index b622ddfd..9539efdd 100644 --- a/examples/lwip/Makefile +++ b/examples/lwip/Makefile @@ -157,7 +157,6 @@ COAP_SRC = coap_address.c \ coap_dgrm_lwip.c \ coap_dtls.c \ coap_encode.c \ - coap_hashkey.c \ coap_io.c \ coap_io_lwip.c \ coap_layers.c \ diff --git a/examples/riot/pkg_libcoap/Makefile.libcoap b/examples/riot/pkg_libcoap/Makefile.libcoap index c5245b4d..2eed2234 100644 --- a/examples/riot/pkg_libcoap/Makefile.libcoap +++ b/examples/riot/pkg_libcoap/Makefile.libcoap @@ -11,7 +11,6 @@ SRC := coap_address.c \ coap_encode.c \ coap_event.c \ coap_gnutls.c\ - coap_hashkey.c \ coap_io.c \ coap_io_riot.c\ coap_layers.c\ diff --git a/include/coap3/coap_cache_internal.h b/include/coap3/coap_cache_internal.h index 1b98bcc2..026d204d 100644 --- a/include/coap3/coap_cache_internal.h +++ b/include/coap3/coap_cache_internal.h @@ -31,7 +31,7 @@ extern "C" { * @{ */ -/* Holds a digest in binary typically sha256 except for notls */ +/* Holds a digest in binary typically sha256 except for notls (sha1) */ typedef struct coap_digest_t { uint8_t key[32]; } coap_digest_t; diff --git a/include/coap3/coap_hashkey_internal.h b/include/coap3/coap_hashkey_internal.h deleted file mode 100644 index ec864d9d..00000000 --- a/include/coap3/coap_hashkey_internal.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * coap_hashkey_internal.h -- definition of hash key type and helper functions - * - * Copyright (C) 2010-2011 Olaf Bergmann - * - * SPDX-License-Identifier: BSD-2-Clause - * - * This file is part of the CoAP library libcoap. Please see README for terms - * of use. - */ - -/** - * @file coap_hashkey_internal.h - * @brief Definition of hash key type and helper functions - */ - -#ifndef COAP_HASHKEY_INTERNAL_H_ -#define COAP_HASHKEY_INTERNAL_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -typedef unsigned char coap_key_t[4]; - -#ifndef coap_hash -/** - * Calculates a fast hash over the given string @p s of length @p len and stores - * the result into @p h. Depending on the exact implementation, this function - * cannot be used as one-way function to check message integrity or similar. - * - * @param s The string used for hash calculation. - * @param len The length of @p s. - * @param h The result buffer to store the calculated hash key. - */ -void coap_hash_impl(const unsigned char *s, size_t len, coap_key_t h); - -#define coap_hash(String,Length,Result) \ - coap_hash_impl((String),(Length),(Result)) - -/* This is used to control the pre-set hash-keys for resources. */ -#define COAP_DEFAULT_HASH -#else -#undef COAP_DEFAULT_HASH -#endif /* coap_hash */ - -/** - * Calls coap_hash() with given @c coap_string_t object as parameter. - * - * @param Str Must contain a pointer to a coap string object. - * @param H A coap_key_t object to store the result. - * - * @hideinitializer - */ -#define coap_str_hash(Str,H) { \ - assert(Str); \ - memset((H), 0, sizeof(coap_key_t)); \ - coap_hash((Str)->s, (Str)->length, (H)); \ - } - -#ifdef __cplusplus -} -#endif - -#endif /* COAP_HASHKEY_INTERNAL_H_ */ diff --git a/include/coap3/coap_internal.h b/include/coap3/coap_internal.h index fff3e5b6..59b56758 100644 --- a/include/coap3/coap_internal.h +++ b/include/coap3/coap_internal.h @@ -149,7 +149,6 @@ typedef struct oscore_ctx_t oscore_ctx_t; #include "coap_debug_internal.h" #include "coap_dgrm_internal.h" #include "coap_dtls_internal.h" -#include "coap_hashkey_internal.h" #include "coap_io_internal.h" #include "coap_layers_internal.h" #include "coap_mutex_internal.h" diff --git a/man/coap_cache.txt.in b/man/coap_cache.txt.in index dd598606..02b7a20a 100644 --- a/man/coap_cache.txt.in +++ b/man/coap_cache.txt.in @@ -84,7 +84,7 @@ and "https://rfc-editor.org/rfc/rfc8132#section-2[RFC8132 2. Fetch Method]". The Cache Key is a SHA256 digest if libcoap was built with TLS support, -otherwise it uses the internal coap_hash() function, using the information +otherwise it uses a SHA1 digest, using the information abstracted from the PDU and (optionally) the CoAP session. This Cache Key can then be used to match against incoming PDUs and then diff --git a/src/coap_hashkey.c b/src/coap_hashkey.c deleted file mode 100644 index 48e85c46..00000000 --- a/src/coap_hashkey.c +++ /dev/null @@ -1,32 +0,0 @@ -/* coap_hashkey.c -- definition of hash key type and helper functions - * - * Copyright (C) 2010-2026 Olaf Bergmann - * - * SPDX-License-Identifier: BSD-2-Clause - * - * This file is part of the CoAP library libcoap. Please see - * README for terms of use. - */ - -/** - * @file coap_hashkey.c - * @brief Hashkey functions - */ - -#include "coap3/coap_libcoap_build.h" - -void -coap_hash_impl(const unsigned char *s, size_t len, coap_key_t h) { - size_t j; - - while (len--) { - j = sizeof(coap_key_t)-1; - - while (j) { - h[j] = ((h[j] << 7) | (h[j-1] >> 1)) + h[j]; - --j; - } - - h[0] = (h[0] << 7) + h[0] + *s++; - } -} diff --git a/src/coap_notls.c b/src/coap_notls.c index c35881e7..575e5204 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -205,17 +205,12 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, #endif /* COAP_OSCORE_SUPPORT */ #if COAP_SERVER_SUPPORT -typedef struct coap_local_hash_t { - size_t ofs; - coap_key_t key[8]; /* 32 bytes in total */ -} coap_local_hash_t; - coap_digest_ctx_t * coap_digest_setup(void) { - coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t)); + SHA1Context *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(SHA1Context)); if (digest_ctx) { - memset(digest_ctx, 0, sizeof(coap_local_hash_t)); + SHA1Reset(digest_ctx); } return digest_ctx; @@ -230,23 +225,22 @@ int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len) { - coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; - - coap_hash(data, data_len, local->key[local->ofs]); - - local->ofs = (local->ofs + 1) % 7; + if (SHA1Input(digest_ctx, data, data_len) != shaSuccess) + return 0; return 1; } int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer) { - coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; - - memcpy(digest_buffer, local->key, sizeof(coap_digest_t)); + int ret = 1; + memset(digest_buffer, 0, sizeof(coap_digest_t)); + if (SHA1Result(digest_ctx, (uint8_t *)digest_buffer) != shaSuccess) { + ret = 0; + } coap_digest_free(digest_ctx); - return 1; + return ret; } #endif /* COAP_SERVER_SUPPORT */ diff --git a/src/coap_sha1.c b/src/coap_sha1.c index e62603c3..bf7e2e24 100644 --- a/src/coap_sha1.c +++ b/src/coap_sha1.c @@ -70,7 +70,7 @@ #include "coap3/coap_libcoap_build.h" -#if COAP_WS_SUPPORT && ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE && ! COAP_WITH_LIBOPENHITLS && ! COAP_WITH_LIBOPENHITLS_OSCORE && ! COAP_WITH_LIBWOLFSSL +#if ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE && ! COAP_WITH_LIBOPENHITLS && ! COAP_WITH_LIBOPENHITLS_OSCORE && ! COAP_WITH_LIBWOLFSSL /* * Define the SHA1 circular left shift macro */ diff --git a/win32/libcoap.vcxproj b/win32/libcoap.vcxproj index ec4ea437..4aa5a68a 100644 --- a/win32/libcoap.vcxproj +++ b/win32/libcoap.vcxproj @@ -48,7 +48,6 @@ - @@ -103,7 +102,6 @@ - diff --git a/win32/libcoap.vcxproj.filters b/win32/libcoap.vcxproj.filters index 7895969f..c274ddc7 100644 --- a/win32/libcoap.vcxproj.filters +++ b/win32/libcoap.vcxproj.filters @@ -42,9 +42,6 @@ Source Files - - Source Files - Source Files @@ -200,9 +197,6 @@ Header Files - - Header Files - Header Files