Skip to content

Commit

Permalink
sha: add new sha1() api (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Jan 26, 2022
1 parent d6719b1 commit 9378a13
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
27 changes: 4 additions & 23 deletions include/re_sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,12 @@
* Copyright (C) 2010 Creytiv.com
*/


#ifdef USE_OPENSSL
#include <openssl/sha.h>
#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);
2 changes: 1 addition & 1 deletion src/hmac/hmac_sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <openssl/hmac.h>
#include <openssl/err.h>
#else
#include <re_sha.h>
#include "../sha/sha.h"
#endif
#include <re_hmac.h>

Expand Down
1 change: 1 addition & 0 deletions src/sha/mod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
ifeq ($(USE_OPENSSL),)
SRCS += sha/sha1.c
endif
SRCS += sha/wrap.c
26 changes: 26 additions & 0 deletions src/sha/sha.h
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/sha/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ A million repetitions of "a"
#include <stdio.h>
#include <string.h>
#include <re_types.h>
#include <re_sha.h>
#include "sha.h"

void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]);

Expand Down
33 changes: 33 additions & 0 deletions src/sha/wrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file wrap.c SHA wrappers
*
* Copyright (C) 2022 Sebastian Reimers <hallo@studio-link.de>
*/

#include <re_types.h>
#ifdef USE_OPENSSL
#include <openssl/sha.h>
#else
#include "sha.h"
#endif
#include <re_sha.h>


/**
* 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
}
17 changes: 12 additions & 5 deletions src/websock/websock.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (C) 2010 Creytiv.com
*/

#include <string.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_mem.h>
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 9378a13

Please sign in to comment.