From fb6edf4c9c06fc857e6d5de9a7673489d93399a4 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 26 Jan 2022 17:35:53 +0100 Subject: [PATCH 1/2] sha: add new sha1() api --- include/re_sha.h | 27 ++++----------------------- src/hmac/hmac_sha1.c | 2 +- src/sha/mod.mk | 1 + src/sha/sha.h | 26 ++++++++++++++++++++++++++ src/sha/sha1.c | 2 +- src/sha/wrap.c | 33 +++++++++++++++++++++++++++++++++ src/websock/websock.c | 17 ++++++++++++----- 7 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 src/sha/sha.h create mode 100644 src/sha/wrap.c diff --git a/include/re_sha.h b/include/re_sha.h index 8fdd632cd..4434cfbcf 100644 --- a/include/re_sha.h +++ b/include/re_sha.h @@ -4,31 +4,12 @@ * Copyright (C) 2010 Creytiv.com */ - -#ifdef USE_OPENSSL -#include -#else - -/* public api for steve reid's public domain SHA-1 implementation */ -/* this file is in the public domain */ - -/** SHA-1 Context */ -typedef struct { - uint32_t state[5]; /**< Context state */ - uint32_t count[2]; /**< Counter */ - uint8_t buffer[64]; /**< SHA-1 buffer */ -} SHA1_CTX; - -/** SHA-1 Context (OpenSSL compat) */ -typedef SHA1_CTX SHA_CTX; - /** SHA-1 Digest size in bytes */ #define SHA1_DIGEST_SIZE 20 + +#ifndef SHA_DIGEST_LENGTH /** SHA-1 Digest size in bytes (OpenSSL compat) */ #define SHA_DIGEST_LENGTH SHA1_DIGEST_SIZE - -void SHA1_Init(SHA1_CTX* context); -void SHA1_Update(SHA1_CTX* context, const void *p, size_t len); -void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context); - #endif + +void sha1(const uint8_t *d, size_t n, uint8_t *md); diff --git a/src/hmac/hmac_sha1.c b/src/hmac/hmac_sha1.c index 1fdf0c590..3957a37c9 100644 --- a/src/hmac/hmac_sha1.c +++ b/src/hmac/hmac_sha1.c @@ -10,7 +10,7 @@ #include #include #else -#include +#include "../sha/sha.h" #endif #include diff --git a/src/sha/mod.mk b/src/sha/mod.mk index c40fdff08..fc52032ef 100644 --- a/src/sha/mod.mk +++ b/src/sha/mod.mk @@ -7,3 +7,4 @@ ifeq ($(USE_OPENSSL),) SRCS += sha/sha1.c endif +SRCS += sha/wrap.c diff --git a/src/sha/sha.h b/src/sha/sha.h new file mode 100644 index 000000000..10c42c9e6 --- /dev/null +++ b/src/sha/sha.h @@ -0,0 +1,26 @@ +/** + * @file re_sha.h Interface to SHA (Secure Hash Standard) functions + * + * Copyright (C) 2010 Creytiv.com + */ + +/* this file is in the public domain */ + +/** SHA-1 Context */ +typedef struct { + uint32_t state[5]; /**< Context state */ + uint32_t count[2]; /**< Counter */ + uint8_t buffer[64]; /**< SHA-1 buffer */ +} SHA1_CTX; + +/** SHA-1 Context (OpenSSL compat) */ +typedef SHA1_CTX SHA_CTX; + +/** SHA-1 Digest size in bytes */ +#define SHA1_DIGEST_SIZE 20 +/** SHA-1 Digest size in bytes (OpenSSL compat) */ +#define SHA_DIGEST_LENGTH SHA1_DIGEST_SIZE + +void SHA1_Init(SHA1_CTX* context); +void SHA1_Update(SHA1_CTX* context, const void *p, size_t len); +void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context); diff --git a/src/sha/sha1.c b/src/sha/sha1.c index 7395a02c3..3da041751 100644 --- a/src/sha/sha1.c +++ b/src/sha/sha1.c @@ -88,7 +88,7 @@ A million repetitions of "a" #include #include #include -#include +#include "sha.h" void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); diff --git a/src/sha/wrap.c b/src/sha/wrap.c new file mode 100644 index 000000000..a7074fb6e --- /dev/null +++ b/src/sha/wrap.c @@ -0,0 +1,33 @@ +/** + * @file wrap.c SHA wrappers + * + * Copyright (C) 2022 Sebastian Reimers + */ + +#include +#ifdef USE_OPENSSL +#include +#else +#include "sha.h" +#endif +#include + + +/** + * Calculate the SHA1 hash from a buffer + * + * @param d Data buffer (input) + * @param n Number of input bytes + * @param md Calculated SHA1 hash (output) + */ +void sha1(const uint8_t *d, size_t n, uint8_t *md) +{ +#ifdef USE_OPENSSL + (void)SHA1(d, n, md); +#else + SHA_CTX ctx; + SHA1_Init(&ctx); + SHA1_Update(&ctx, d, n); + SHA1_Final(md, &ctx); +#endif +} diff --git a/src/websock/websock.c b/src/websock/websock.c index a9ee2bd41..434f9a278 100644 --- a/src/websock/websock.c +++ b/src/websock/websock.c @@ -4,6 +4,7 @@ * Copyright (C) 2010 Creytiv.com */ +#include #include #include #include @@ -369,12 +370,18 @@ static void close_handler(int err, void *arg) static int accept_print(struct re_printf *pf, const struct pl *key) { uint8_t digest[SHA_DIGEST_LENGTH]; - SHA_CTX ctx; + uint8_t *data; + size_t len = key->l + sizeof(magic)-1; - SHA1_Init(&ctx); - SHA1_Update(&ctx, key->p, key->l); - SHA1_Update(&ctx, magic, sizeof(magic)-1); - SHA1_Final(digest, &ctx); + data = mem_zalloc(len, NULL); + if (!data) + return ENOMEM; + + memcpy(data, key->p, key->l); + memcpy(data + key->l, magic, sizeof(magic)-1); + + sha1(data, len, digest); + mem_deref(data); return base64_print(pf, digest, sizeof(digest)); } From 29e1367c1a503254e1de4f43c77a44fdd35d4b91 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 26 Jan 2022 20:22:16 +0100 Subject: [PATCH 2/2] fix ccheck --- src/websock/websock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websock/websock.c b/src/websock/websock.c index 434f9a278..cc493a346 100644 --- a/src/websock/websock.c +++ b/src/websock/websock.c @@ -371,7 +371,7 @@ static int accept_print(struct re_printf *pf, const struct pl *key) { uint8_t digest[SHA_DIGEST_LENGTH]; uint8_t *data; - size_t len = key->l + sizeof(magic)-1; + size_t len = key->l + sizeof(magic)-1; data = mem_zalloc(len, NULL); if (!data)