Skip to content

Commit

Permalink
add check for return values of openssl functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cwansart committed Sep 24, 2023
1 parent 892a78f commit 4690db8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CC = cl

# Flags for the compiler
CFLAGS = /W4 /MT
CFLAGS = /W4 /MT /O2

# Include and lib directories for OpenSSL
OPENSSL_INCLUDE_DIR = include
Expand All @@ -28,6 +28,14 @@ $(OUTFILE): $(OBJECTS)
.c.obj:
$(CC) $(CFLAGS) /c $< /Fo$@ /I "$(OPENSSL_INCLUDE_DIR)"

# Assembly output with optimization
# asm-optimized: $(SOURCES)
# $(CC) /FAs /O2 /c $(SOURCES) /Foasm-optimized.obj /I "$(OPENSSL_INCLUDE_DIR)"

# Assembly output without optimization
# asm-unoptimized: $(SOURCES)
# $(CC) /FAs /c $(SOURCES) /Foasm-unoptimized.obj /I "$(OPENSSL_INCLUDE_DIR)"

# Clean rule
clean:
del *.obj $(OUTFILE)
Expand Down
37 changes: 29 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,60 @@

int main(int argc, char *argv[])
{
// args
if (argc < 2)
{
printf("missing file parameter");
printf("missing file parameter\n");
return 1;
}

// open file
FILE *file = NULL;
errno_t err = fopen_s(&file, argv[1], "rb");

if (err != 0)
{
printf("Failed to open the file. Error code: %d\n", err);
printf("failed to open the file, err code: %d\n", err);
return 1;
}

// hash
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;

EVP_MD_CTX *ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
if (!EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
{
printf("failed to init digest.\n");
EVP_MD_CTX_free(ctx);
fclose(file);
return 1;
}

char buf[1024];
size_t nread;
while ((nread = fread(buf, 1, sizeof buf, file)) > 0)
{
EVP_DigestUpdate(ctx, buf, nread);
if (!EVP_DigestUpdate(ctx, buf, nread))
{
printf("failed to update digest\n");
EVP_MD_CTX_free(ctx);
fclose(file);
return 1;
}
}

if (!EVP_DigestFinal_ex(ctx, hash, &hash_len))
{
printf("failed to finalize digest\n");
EVP_MD_CTX_free(ctx);
fclose(file);
return 1;
}

EVP_DigestFinal_ex(ctx, hash, &hash_len);
EVP_MD_CTX_free(ctx);

unsigned int i;
for (i = 0; i < hash_len; i++)
// print hash
for (unsigned int i = 0; i < hash_len; i++)
{
printf("%02x", hash[i]);
}
Expand Down

0 comments on commit 4690db8

Please sign in to comment.