Skip to content

Commit

Permalink
Merge pull request #173 from zvr/hash
Browse files Browse the repository at this point in the history
Update hashing code
  • Loading branch information
zvr authored Mar 21, 2023
2 parents c24f929 + e41a59a commit 36a4035
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .indent.pro
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
-Tva_list

/* OpenSSL types */
-TSHA_CTX
-TEVP_MD
-TEVP_MD_CTX

/* project-specific types */
-TFILE_INFO
Expand Down
14 changes: 6 additions & 8 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ AC_CONFIG_SRCDIR([src/main.c])

dnl Checks for programs
dnl Checks for libraries
AC_CHECK_LIB(crypto, SHA1_Init,, AC_MSG_ERROR([cannot find libcrypto]))
AC_CHECK_LIB(crypto, EVP_sha1,, AC_MSG_ERROR([cannot find libcrypto]))

dnl Checks for headers
AC_CHECK_HEADER([linux/ptrace.h],, AC_MSG_ERROR([cannot find linux/ptrace.h]))
AC_CHECK_HEADER([openssl/sha.h],, AC_MSG_ERROR([cannot find openssl/sha.h]))
AC_CHECK_HEADER([openssl/evp.h],, AC_MSG_ERROR([cannot find openssl/evp.h]))
AC_CHECK_HEADER([sys/mman.h],, AC_MSG_ERROR([cannot find sys/mman.h]))
AC_CHECK_HEADER([sys/ptrace.h],, AC_MSG_ERROR([cannot find sys/ptrace.h]))
AC_CHECK_HEADER([sys/signal.h],, AC_MSG_ERROR([cannot find sys/signal.h]))
Expand All @@ -47,8 +47,6 @@ AC_CHECK_TYPE([struct ptrace_syscall_info],, AC_MSG_ERROR([cannot find struct pt

AC_CHECK_TYPE([pid_t],, AC_MSG_ERROR([cannot find pid_t]), [#include <sys/types.h>])

AC_CHECK_TYPE([SHA_CTX],, AC_MSG_ERROR([cannot find SHA_CTX]), [#include <openssl/sha.h>])

AC_CHECK_DECL([PATH_MAX],, AC_MSG_ERROR([cannot find PATH_MAX]), [#include <linux/limits.h>])
AC_CHECK_DECL([ARG_MAX],, AC_MSG_ERROR([cannot find ARG_MAX]), [#include <linux/limits.h>])

Expand Down Expand Up @@ -76,17 +74,17 @@ AC_CHECK_DECL([SYS_close],, AC_MSG_ERROR([cannot find SYS_close]), [#include <sy
AC_CHECK_DECL([MADV_SEQUENTIAL],, AC_MSG_ERROR([cannot find MADV_SEQUENTIAL]), [#include <sys/mman.h>])

dnl Checks for functions
AC_CHECK_FUNC([SHA1_Init],, AC_MSG_ERROR([cannot find SHA1_Init(3)]))
AC_CHECK_FUNC([SHA1_Update],, AC_MSG_ERROR([cannot find SHA1_Update(3)]))
AC_CHECK_FUNC([SHA1_Final],, AC_MSG_ERROR([cannot find SHA1_Final(3)]))

AC_CHECK_FUNC([fork],, AC_MSG_ERROR([cannot find fork(2)]))
AC_CHECK_FUNC([ptrace],, AC_MSG_ERROR([cannot find ptrace(2)]))
AC_CHECK_FUNC([waitpid],, AC_MSG_ERROR([cannot find waitpid(2)]))

AC_CHECK_FUNC([mmap],, AC_MSG_ERROR([cannot find mmap(2)]))
AC_CHECK_FUNC([madvise],, AC_MSG_ERROR([cannot find madvise(2)]))

AC_CHECK_FUNC([EVP_DigestInit_ex],, AC_MSG_ERROR([cannot find EVP_DigestInit_ex(3)]))
AC_CHECK_FUNC([EVP_DigestUpdate],, AC_MSG_ERROR([cannot find EVP_DigestUpdate(3)]))
AC_CHECK_FUNC([EVP_DigestFinal_ex],, AC_MSG_ERROR([cannot find EVP_DigestFinal_ex(3)]))

dnl We need a C compiler
AC_PROG_CC

Expand Down
26 changes: 19 additions & 7 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ SPDX-License-Identifier: LGPL-2.1-or-later
#include <sys/types.h> //
#include <unistd.h> // optind, readlink(2)

#include <openssl/sha.h> // SHA_CTX, SHA1_Init, SHA1_Update,
// SHA1_Final
#include <openssl/evp.h> // EVP_sha1(), EVP_DigestInit_ex(),
// EVP_DigestUpdate(),
// EVP_DigestFinal_ex()

#define SHA1_OUTPUT_LEN 20
#define SHA1_HEXBUF_LEN (2 * SHA1_OUTPUT_LEN + 1)

Expand Down Expand Up @@ -77,7 +79,6 @@ hash_file_contents(char *name, size_t sz)

presize = sprintf(pre, "blob %lu%c", sz, 0);

SHA_CTX ctx;
unsigned char *hash;

hash = malloc(SHA1_OUTPUT_LEN);
Expand All @@ -86,10 +87,17 @@ hash_file_contents(char *name, size_t sz)
return NULL;
}

SHA1_Init(&ctx);
SHA1_Update(&ctx, pre, presize);
SHA1_Update(&ctx, buf, sz);
SHA1_Final(hash, &ctx);
const static EVP_MD *sha1_md;

if (sha1_md == 0)
sha1_md = EVP_sha1();

EVP_MD_CTX *ctx = EVP_MD_CTX_new();

EVP_DigestInit_ex(ctx, sha1_md, NULL);
EVP_DigestUpdate(ctx, pre, presize);
EVP_DigestUpdate(ctx, buf, sz);
EVP_DigestFinal_ex(ctx, hash, NULL);

close(fd);

Expand All @@ -114,6 +122,10 @@ get_file_hash(char *fname)

if (sz > 0) {
uint8_t *h = hash_file_contents(fname, sz);

if (h == NULL)
return NULL;

char *ret = hash_to_str(h);

free(h);
Expand Down
4 changes: 3 additions & 1 deletion src/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ find_finfo(char *abspath, char *hash)
while (i >= 0) {
if (!strcmp(abspath, finfo[i].abspath)
&& ((hash == NULL && finfo[i].hash == NULL)
|| (hash != NULL && finfo[i].hash != NULL && !strcmp(hash, finfo[i].hash)))) {
|| (hash != NULL && finfo[i].hash != NULL
&& !strcmp(hash, finfo[i].hash)))) {

break;
}

Expand Down

0 comments on commit 36a4035

Please sign in to comment.