From adb7322823f4956d1179e06fd982c7df6c22bd70 Mon Sep 17 00:00:00 2001 From: LDA Date: Sun, 25 Aug 2024 21:40:23 +0200 Subject: [PATCH 1/2] [ADD] Raw SHA inputs, hashtypes to ShaToHex --- include/Cytoplasm/Sha.h | 26 +++++++++++++++++++++++++- src/Sha.c | 33 ++++++++++++++++++++++++++++----- src/Sha/Sha1.c | 8 ++++---- src/Sha/Sha256.c | 8 ++++---- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/include/Cytoplasm/Sha.h b/include/Cytoplasm/Sha.h index a23ef35..8a94fec 100644 --- a/include/Cytoplasm/Sha.h +++ b/include/Cytoplasm/Sha.h @@ -41,6 +41,16 @@ * due to the lack of a 64-bit integer type, so that hash * function has been omitted. */ +#include + +/** + * This is an enum to be used to identify the type of SHA used. + */ +typedef enum HashType +{ + HASH_SHA1, + HASH_SHA256 +} HashType; /** * This function takes a pointer to a NULL-terminated C string, and @@ -64,6 +74,20 @@ extern unsigned char * Sha256(char *); */ extern unsigned char * Sha1(char *); +/** + * This function behaves just like + * .Fn Sha256 , + * except that it allows for a generic byte array, instead of a string. + */ +extern unsigned char * Sha256Raw(unsigned char *, size_t); + +/** + * This function behaves just like + * .Fn Sha1 , + * except that it allows for a generic byte array, instead of a string. + */ +extern unsigned char * Sha1Raw(unsigned char *, size_t); + /** * Convert a SHA byte buffer into a hex string. These hex strings * are typically what is transmitted, stored, and compared, however @@ -71,6 +95,6 @@ extern unsigned char * Sha1(char *); * bytes directly, which is why the conversion to a hex string is * a separate step. */ -extern char * ShaToHex(unsigned char *); +extern char * ShaToHex(unsigned char *, HashType); #endif /* CYTOPLASM_SHA_H */ diff --git a/src/Sha.c b/src/Sha.c index 377ae14..32e2e12 100644 --- a/src/Sha.c +++ b/src/Sha.c @@ -28,21 +28,44 @@ #include char * -ShaToHex(unsigned char *bytes) +ShaToHex(unsigned char *bytes, HashType type) { - size_t i = 0; - char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char)); + size_t i = 0, size; + char *str; + switch (type) + { + case HASH_SHA1: + size = 20; + break; + case HASH_SHA256: + size = 32; + break; + default: + return NULL; + } + + str = Malloc(((size * 2) + 1) * sizeof(char)); if (!str) { return NULL; } - while (bytes[i] != '\0') + for (i = 0; i < size; i++) { snprintf(str + (2 * i), 3, "%02x", bytes[i]); - i++; } return str; } +unsigned char * +Sha256(char *str) +{ + return Sha256Raw((unsigned char *) str, str ? strlen(str) : 0); +} + +unsigned char * +Sha1(char *str) +{ + return Sha1Raw((unsigned char *) str, str ? strlen(str) : 0); +} diff --git a/src/Sha/Sha1.c b/src/Sha/Sha1.c index 0b0d327..dc41922 100644 --- a/src/Sha/Sha1.c +++ b/src/Sha/Sha1.c @@ -34,7 +34,7 @@ #include unsigned char * -Sha1(char *str) +Sha1Raw(unsigned char *str, size_t len) { unsigned char *digest; if (!str) @@ -43,7 +43,7 @@ Sha1(char *str) } digest = Malloc(20 + 1); - SHA1((unsigned char *) str, strlen(str), digest); + SHA1(str, len, digest); digest[20] = '\0'; return digest; } @@ -261,7 +261,7 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out) } unsigned char * -Sha1(char *str) +Sha1Raw(unsigned char *str, size_t len) { Sha1Context ctx; unsigned char *out; @@ -278,7 +278,7 @@ Sha1(char *str) } Sha1Init(&ctx); - Sha1Update(&ctx, str, strlen(str)); + Sha1Update(&ctx, str, len); Sha1Calculate(&ctx, out); out[160 / 8] = '\0'; diff --git a/src/Sha/Sha256.c b/src/Sha/Sha256.c index 5a1e08c..7f25dae 100644 --- a/src/Sha/Sha256.c +++ b/src/Sha/Sha256.c @@ -36,7 +36,7 @@ #include unsigned char * -Sha256(char *str) +Sha256Raw(unsigned char *str, size_t len) { unsigned char *digest; if (!str) @@ -45,7 +45,7 @@ Sha256(char *str) } digest = Malloc(32 + 1); - SHA256((unsigned char *) str, strlen(str), digest); + SHA256(str, len, digest); digest[32] = '\0'; return digest; } @@ -192,7 +192,7 @@ Sha256Process(Sha256Context * context, unsigned char *data, size_t length) } unsigned char * -Sha256(char *str) +Sha256Raw(unsigned char *str, size_t len) { Sha256Context context; size_t i; @@ -228,7 +228,7 @@ Sha256(char *str) context.length = 0; memset(context.buffer, 0, 64); - Sha256Process(&context, (unsigned char *) str, strlen(str)); + Sha256Process(&context, str, len); memset(fill, 0, 64); fill[0] = 0x80; From 5df458f56804a8e8aeb18179b6485565fb5468c6 Mon Sep 17 00:00:00 2001 From: LDA Date: Mon, 26 Aug 2024 15:28:30 +0200 Subject: [PATCH 2/2] [FIX] Fix out define hjinks --- src/Sha/Sha1.c | 2 +- src/Sha/Sha256.c | 2 +- src/Tls/TlsLibreSSL.c | 2 +- src/Tls/TlsOpenSSL.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Sha/Sha1.c b/src/Sha/Sha1.c index dc41922..c4f9db8 100644 --- a/src/Sha/Sha1.c +++ b/src/Sha/Sha1.c @@ -29,7 +29,7 @@ #include /* TODO: Verify LibreSSL support later */ -#if TLS_IMPL == TLS_OPENSSL +#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL) #include diff --git a/src/Sha/Sha256.c b/src/Sha/Sha256.c index 7f25dae..0b44b7e 100644 --- a/src/Sha/Sha256.c +++ b/src/Sha/Sha256.c @@ -31,7 +31,7 @@ /* TODO: Verify LibreSSL support later */ -#if TLS_IMPL == TLS_OPENSSL +#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL) #include diff --git a/src/Tls/TlsLibreSSL.c b/src/Tls/TlsLibreSSL.c index dc09b11..3f805ae 100644 --- a/src/Tls/TlsLibreSSL.c +++ b/src/Tls/TlsLibreSSL.c @@ -23,7 +23,7 @@ */ #include -#if TLS_IMPL == TLS_LIBRESSL +#if defined(TLS_IMPL) && (TLS_IMPL == TLS_LIBRESSL) #include #include diff --git a/src/Tls/TlsOpenSSL.c b/src/Tls/TlsOpenSSL.c index cf07ce0..591c6f4 100644 --- a/src/Tls/TlsOpenSSL.c +++ b/src/Tls/TlsOpenSSL.c @@ -23,7 +23,7 @@ */ #include -#if TLS_IMPL == TLS_OPENSSL +#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL) #include #include