From 86087e5f9cf4a0512ba36b4d01086b905574a47d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 11 May 2026 23:15:33 +0200 Subject: Misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- librecrypt_hash_.c | 62 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 15 deletions(-) (limited to 'librecrypt_hash_.c') diff --git a/librecrypt_hash_.c b/librecrypt_hash_.c index 2ee03ee..c66d4be 100644 --- a/librecrypt_hash_.c +++ b/librecrypt_hash_.c @@ -41,7 +41,7 @@ librecrypt_hash_(char *restrict out_buffer, size_t size, const char *phrase, siz size_t hash_size, digit, quotient, remainder; int has_next, phrase_scratch_i = 0; ssize_t r_len; - int r; + int r, saved_errno; void *new; /* Ensure the reserved parameter is NULL */ @@ -269,12 +269,18 @@ next: } /* Erase and deallocate scratch memory */ - librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]); - librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]); - librecrypt_wipe_str(settings_scratch); - free(phrase_scratches[0u]); - free(phrase_scratches[1u]); - free(settings_scratch); + if (phrase_scratches[0u]) { + librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]); + free(phrase_scratches[0u]); + } + if (phrase_scratches[1u]) { + librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]); + free(phrase_scratches[1u]); + } + if (settings_scratch) { + librecrypt_wipe_str(settings_scratch); + free(settings_scratch); + } /* NUL-terminate output if it is a string (`out_buffer` is offset at every write to it) */ if (size && action != BINARY_HASH) @@ -285,12 +291,20 @@ next: einval: errno = EINVAL; fail: - librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]); - librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]); - librecrypt_wipe_str(settings_scratch); - free(phrase_scratches[0u]); - free(phrase_scratches[1u]); - free(settings_scratch); + saved_errno = errno; + if (phrase_scratches[0u]) { + librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]); + free(phrase_scratches[0u]); + } + if (phrase_scratches[1u]) { + librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]); + free(phrase_scratches[1u]); + } + if (settings_scratch) { + librecrypt_wipe_str(settings_scratch); + free(settings_scratch); + } + errno = saved_errno; return -1; } @@ -298,8 +312,26 @@ fail: #else -/* Tested via librecrypt_hash_binary, librecrypt_hash, and librecrypt_crypt */ -CONST int main(void) { return 0; } +/* Mainly tested via librecrypt_hash_binary, librecrypt_hash, and librecrypt_crypt */ + + +int +main(void) +{ + SET_UP_ALARM(); + INIT_RESOURCE_TEST(); + + errno = 0; + EXPECT(librecrypt_hash_(NULL, 0u, NULL, 0u, "$~no~such~algorithm~$", &(char){0}, ASCII_CRYPT) == -1); + EXPECT(errno == EINVAL); + + errno = 0; + EXPECT(librecrypt_hash_(NULL, 0u, NULL, 0u, "$~no~such~algorithm~$", NULL, ASCII_CRYPT) == -1); + EXPECT(errno == ENOSYS); + + STOP_RESOURCE_TEST(); + return 0; +} #endif -- cgit v1.2.3-70-g09d2