From 4272ff9ca3f350a9a48bb2ac8efa01ffdd0675a7 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Thu, 24 Sep 2020 11:19:32 -0600 Subject: [PATCH] Add simple example to hash contents of a file --- .gitignore | 1 + hash/Makefile | 38 ++++++++++++++++++++ hash/sha256-hash.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 hash/Makefile create mode 100644 hash/sha256-hash.c diff --git a/.gitignore b/.gitignore index e927ae87..4bc78c42 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ *.x86_64 *.hex *app +*run # IDE Temp Files **/*.swp diff --git a/hash/Makefile b/hash/Makefile new file mode 100644 index 00000000..8db47c14 --- /dev/null +++ b/hash/Makefile @@ -0,0 +1,38 @@ +CC := gcc -fsanitize=address +program_NAME := run +program_C_SRCS := $(wildcard *.c) +program_CXX_SRCS := $(wildcard *.cpp) +program_C_OBJS := ${program_C_SRCS:.c=.o} +program_CXX_OBJS := ${program_CXX_SRCS:.cpp=.o} +program_OBJS := $(program_C_OBJS) $(program_CXX_OBJS) +program_INCLUDE_DIRS := +program_LIBRARY_DIRS := +program_LIBRARIES := + +#WOLF_INSTALL_DIR := /Users/kalebhimes/work/testDir/wolf-install-dir-for-testing +WOLF_INSTALL_DIR := /usr/local/lib +program_INCLUDE_DIRS += $(WOLF_INSTALL_DIR)/include +program_INCLUDE_DIRS += ./include +program_LIBRARY_DIRS += $(WOLF_INSTALL_DIR)/lib +program_LIBRARIES += wolfssl + +CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir)) +#CPPFLAGS += -Werror +#CPPFLAGS += -Weverything +#CPPFLAGS += -Wsign-conversion +#CPPFLAGS += -Wshorten-64-to-32 #NOTE: Not supported on most linux distros. +LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir)) +LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library)) + +.PHONY: all clean distclean + +all: $(program_NAME) + +$(program_NAME): $(program_OBJS) + $(CC) $(CPPFLAGS) $(program_OBJS) -o $(program_NAME) $(LDFLAGS) + +clean: + @- $(RM) $(program_NAME) + @- $(RM) $(program_OBJS) + +distclean: clean diff --git a/hash/sha256-hash.c b/hash/sha256-hash.c new file mode 100644 index 00000000..5fdb09e8 --- /dev/null +++ b/hash/sha256-hash.c @@ -0,0 +1,89 @@ +/* sha256-hash.c + * + * Copyright (C) 2006-2020 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + + +#include +#include +#include + +void usage(void) +{ + printf("./run \n"); + exit(-99); +} + +int main(int argc, char** argv) +{ + int ret = -1; +#ifndef NO_SHA256 + byte hash[WC_SHA256_DIGEST_SIZE]; + byte* rawInput; + FILE* inputStream; + char* fName = NULL; + int numBytes = 0; + int fileLength = 0; + + if (argc < 2) + usage(); + fName = argv[1]; + printf("Hash input file %s\n", fName); + + inputStream = fopen(fName, "rb"); + if (inputStream == NULL) { + printf("ERROR: Unable to open file\n"); + return -1; + } + + /* find length of the file */ + fseek(inputStream, 0, SEEK_END); + fileLength = (int) ftell(inputStream); + fseek(inputStream, 0, SEEK_SET); + + /* allocate buffer based on file size */ + rawInput = (byte*) XMALLOC(fileLength*sizeof(byte), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + + numBytes = fread(rawInput, 1, fileLength, inputStream); + if (numBytes != fileLength) { + printf("ERROR: Failed to read the entire file\n"); + fclose(inputStream); + XFREE(rawInput, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return -2; + } + + ret = wc_Sha256Hash(rawInput, numBytes, hash); + if (ret != 0) + printf("ERROR: Hash operation failed"); + else { + int i; + printf("Hash result is: "); + for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++) + printf("%02x", hash[i]); + printf("\n"); + } + + fclose(inputStream); + XFREE(rawInput, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#else + printf("Please enabling sha256 and then try again\n"); +#endif + return ret; +}