From b24f37fb4fe99bab9931c4222932cf5a3e28f7ef Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 23 Apr 2024 15:28:11 +0800 Subject: [PATCH] core: replace SHA1() function by SHA1_uint32() The SHA1() helper function was only used when calculating a SHA1 hash and taking the first four bytes of it as uint32. Make that explicit by renaming the function into SHA1_uint32() and directly returning an uint32_t. Note that the usage in cochran.cpp is sketchy: it generates a four-byte hash out of two-byte data. Why!? Signed-off-by: Berthold Stoeger --- core/cochran.cpp | 7 ++----- core/libdivecomputer.cpp | 5 +---- core/sha1.c | 12 ++++++++++++ core/sha1.h | 11 ++--------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/core/cochran.cpp b/core/cochran.cpp index ea8be2dc9..33adb80d2 100644 --- a/core/cochran.cpp +++ b/core/cochran.cpp @@ -610,7 +610,6 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod, struct dive *dive; struct divecomputer *dc; struct tm tm = {0}; - uint32_t csum[5]; double max_depth, avg_depth, min_temp; unsigned int duration = 0, corrupt_dive = 0; @@ -719,8 +718,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod, * (double) log[CMD_ALTITUDE] * 250 * FEET, 5.25588) * 1000); dc->salinity = 10000 + 150 * log[CMD_WATER_CONDUCTIVITY]; - SHA1(log + CMD_NUMBER, 2, (unsigned char *)csum); - dc->diveid = csum[0]; + dc->diveid = SHA1_uint32(log + CMD_NUMBER, 2); if (log[CMD_MAX_DEPTH] == 0xff && log[CMD_MAX_DEPTH + 1] == 0xff) corrupt_dive = 1; @@ -765,8 +763,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod, * (double) log[EMC_ALTITUDE] * 250 * FEET, 5.25588) * 1000); dc->salinity = 10000 + 150 * (log[EMC_WATER_CONDUCTIVITY] & 0x3); - SHA1(log + EMC_NUMBER, 2, (unsigned char *)csum); - dc->diveid = csum[0]; + dc->diveid = SHA1_uint32(log + EMC_NUMBER, 2); if (log[EMC_MAX_DEPTH] == 0xff && log[EMC_MAX_DEPTH + 1] == 0xff) corrupt_dive = 1; diff --git a/core/libdivecomputer.cpp b/core/libdivecomputer.cpp index 74bde8c8c..ecddab8b4 100644 --- a/core/libdivecomputer.cpp +++ b/core/libdivecomputer.cpp @@ -601,13 +601,10 @@ static char *str_printf(const char *fmt, ...) */ static uint32_t calculate_diveid(const unsigned char *fingerprint, unsigned int fsize) { - uint32_t csum[5]; - if (!fingerprint || !fsize) return 0; - SHA1(fingerprint, fsize, (unsigned char *)csum); - return csum[0]; + return SHA1_uint32(fingerprint, fsize); } uint32_t calculate_string_hash(const char *str) diff --git a/core/sha1.c b/core/sha1.c index acf8c5d9f..09da8da3f 100644 --- a/core/sha1.c +++ b/core/sha1.c @@ -298,3 +298,15 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx) for (i = 0; i < 5; i++) put_be32(hashout + i * 4, ctx->H[i]); } + +uint32_t SHA1_uint32(const void *dataIn, unsigned long len) +{ + uint32_t hashout[5]; + SHA_CTX ctx; + + SHA1_Init(&ctx); + SHA1_Update(&ctx, dataIn, len); + SHA1_Final((unsigned char *)hashout, &ctx); + + return hashout[0]; +} diff --git a/core/sha1.h b/core/sha1.h index 8a176f5e5..341d9231f 100644 --- a/core/sha1.h +++ b/core/sha1.h @@ -29,15 +29,8 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx); #define SHA1_Update blk_SHA1_Update #define SHA1_Final blk_SHA1_Final -/* Trivial helper function */ -static inline void SHA1(const void *dataIn, unsigned long len, unsigned char hashout[20]) -{ - SHA_CTX ctx; - - SHA1_Init(&ctx); - SHA1_Update(&ctx, dataIn, len); - SHA1_Final(hashout, &ctx); -} +/* Helper function that calculates an SHA1 has and returns the first 4 bytes as uint32_t */ +uint32_t SHA1_uint32(const void *dataIn, unsigned long len); #ifdef __cplusplus }