EGong

An UDP Multicast messaging application
git clone git://xatko.vsos.ethz.ch/EGong.git
Log | Files | Refs

commit fca55fc9e22305d8e1f98cb57e203b4004c54d37
parent 89f564c974aea859c440f637b8f9ec4f13167b1b
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Sun,  6 Jul 2014 22:23:29 +0200

Use own implementation of SHA256 rather than linking against OpenSSL.

Diffstat:
CMakeLists.txt | 5+----
include/Packet.h | 2+-
include/Util/Hash.h | 21+++++++++++++++++++++
src/Packet.c | 2+-
src/Util/Hash.c | 215+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 239 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -30,7 +30,7 @@ project(EGong) set(EGong_VERSION_MAJOR 0) set(EGong_VERSION_MINOR 1) set(EGONG_SRCDIR src/) -set(EGONG_LIBS Interfaces/CMD Util/Command Interfaces Interfaces/STDIO CNC Util/Config Util/Socket Packet Util/Waiter Util/Log Util/Misc) +set(EGONG_LIBS Interfaces/CMD Util/Command Interfaces Interfaces/STDIO CNC Util/Config Util/Socket Packet Util/Waiter Util/Log Util/Misc Util/Hash) set(EGONG_EXT_LIBS) include_directories("${PROJECT_SOURCE_DIR}/include") configure_file( @@ -82,9 +82,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows") endif(USE_WINGUI) endif(CMAKE_SYSTEM_NAME STREQUAL "Windows") -find_package(OpenSSL REQUIRED) -include_directories(${OPENSSL_INCLUDE_DIRS}) -target_link_libraries(EGong ${OPENSSL_LIBRARIES}) foreach(lib ${EGONG_LIBS}) diff --git a/include/Packet.h b/include/Packet.h @@ -1,7 +1,7 @@ #pragma once #include <stdint.h> #include <stddef.h> -#include <openssl/sha.h> +#include <EGong/Util/Hash.h> #include <EGong/Util/Socket.h> diff --git a/include/Util/Hash.h b/include/Util/Hash.h @@ -0,0 +1,21 @@ +/* Crypto/Sha256.h -- SHA-256 Hash function +2008-10-04 : Igor Pavlov : Public domain */ + +#pragma once +#include <stdint.h> +#include <stdlib.h> + +#define SHA256_DIGEST_LENGTH 32 + +struct CSHA256_s +{ + uint32_t state[8]; + uint64_t count; + char buffer[64]; +}; +typedef struct CSHA256_s CSHA256; +typedef struct CSHA256_s SHA256_CTX; + +void SHA256_Init(CSHA256 *p); +void SHA256_Update(CSHA256 *p, const char *data, size_t size); +void SHA256_Final(char *digest, CSHA256 *p); diff --git a/src/Packet.c b/src/Packet.c @@ -1,4 +1,4 @@ -#include <openssl/sha.h> +#include <EGong/Util/Hash.h> #include <EGong/Packet.h> #include <EGong/Util/Socket.h> #include <EGong/Util/Log.h> diff --git a/src/Util/Hash.c b/src/Util/Hash.c @@ -0,0 +1,215 @@ +/* Crypto/SHA256.c -- SHA-256 Hash function +2008-11-06 : Igor Pavlov : Public domain +This code is based on public domain code from Wei Dai's Crypto++ library. */ + +#include <EGong/Util/Hash.h> +#ifdef _MSC_VER + +#include <stdlib.h> +#define rotlFixed(x, n) _rotl((x), (n)) +#define rotrFixed(x, n) _rotr((x), (n)) + +#else + +#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) + +#endif +/* define it for speed optimization */ +/* #define _SHA256_UNROLL */ +/* #define _SHA256_UNROLL2 */ + +void SHA256_Init(CSHA256 *p) +{ + p->state[0] = 0x6a09e667; + p->state[1] = 0xbb67ae85; + p->state[2] = 0x3c6ef372; + p->state[3] = 0xa54ff53a; + p->state[4] = 0x510e527f; + p->state[5] = 0x9b05688c; + p->state[6] = 0x1f83d9ab; + p->state[7] = 0x5be0cd19; + p->count = 0; +} + +#define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22)) +#define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25)) +#define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3)) +#define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10)) + +#define blk0(i) (W[i] = data[i]) +#define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15])) + +#define Ch(x,y,z) (z^(x&(y^z))) +#define Maj(x,y,z) ((x&y)|(z&(x|y))) + +#define a(i) T[(0-(i))&7] +#define b(i) T[(1-(i))&7] +#define c(i) T[(2-(i))&7] +#define d(i) T[(3-(i))&7] +#define e(i) T[(4-(i))&7] +#define f(i) T[(5-(i))&7] +#define g(i) T[(6-(i))&7] +#define h(i) T[(7-(i))&7] + + +#ifdef _SHA256_UNROLL2 + +#define R(a,b,c,d,e,f,g,h, i) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\ + d += h; h += S0(a) + Maj(a, b, c) + +#define RX_8(i) \ + R(a,b,c,d,e,f,g,h, i); \ + R(h,a,b,c,d,e,f,g, i+1); \ + R(g,h,a,b,c,d,e,f, i+2); \ + R(f,g,h,a,b,c,d,e, i+3); \ + R(e,f,g,h,a,b,c,d, i+4); \ + R(d,e,f,g,h,a,b,c, i+5); \ + R(c,d,e,f,g,h,a,b, i+6); \ + R(b,c,d,e,f,g,h,a, i+7) + +#else + +#define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\ + d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) + +#ifdef _SHA256_UNROLL + +#define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7); + +#endif + +#endif + +const uint32_t K[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +static void SHA256_Transform(uint32_t *state, const uint32_t *data) +{ + uint32_t W[16]; + unsigned j; + #ifdef _SHA256_UNROLL2 + uint32_t a,b,c,d,e,f,g,h; + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + f = state[5]; + g = state[6]; + h = state[7]; + #else + uint32_t T[8]; + for (j = 0; j < 8; j++) + T[j] = state[j]; + #endif + + for (j = 0; j < 64; j += 16) + { + #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2) + RX_8(0); RX_8(8); + #else + unsigned i; + for (i = 0; i < 16; i++) { R(i); } + #endif + } + + #ifdef _SHA256_UNROLL2 + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + state[5] += f; + state[6] += g; + state[7] += h; + #else + for (j = 0; j < 8; j++) + state[j] += T[j]; + #endif + + /* Wipe variables */ + /* memset(W, 0, sizeof(W)); */ + /* memset(T, 0, sizeof(T)); */ +} + +#undef S0 +#undef S1 +#undef s0 +#undef s1 + +static void SHA256_WritecharBlock(CSHA256 *p) +{ + uint32_t data32[16]; + unsigned i; + for (i = 0; i < 16; i++) + data32[i] = + ((uint32_t)(p->buffer[i * 4 ]) << 24) + + ((uint32_t)(p->buffer[i * 4 + 1]) << 16) + + ((uint32_t)(p->buffer[i * 4 + 2]) << 8) + + ((uint32_t)(p->buffer[i * 4 + 3])); + SHA256_Transform(p->state, data32); +} + +void SHA256_Update(CSHA256 *p, const char *data, size_t size) +{ + uint32_t curBufferPos = (uint32_t)p->count & 0x3F; + while (size > 0) + { + p->buffer[curBufferPos++] = *data++; + p->count++; + size--; + if (curBufferPos == 64) + { + curBufferPos = 0; + SHA256_WritecharBlock(p); + } + } +} + +void SHA256_Final(char *digest, CSHA256 *p) +{ + uint64_t lenInBits = (p->count << 3); + uint32_t curBufferPos = (uint32_t)p->count & 0x3F; + unsigned i; + p->buffer[curBufferPos++] = 0x80; + while (curBufferPos != (64 - 8)) + { + curBufferPos &= 0x3F; + if (curBufferPos == 0) + SHA256_WritecharBlock(p); + p->buffer[curBufferPos++] = 0; + } + for (i = 0; i < 8; i++) + { + p->buffer[curBufferPos++] = (char)(lenInBits >> 56); + lenInBits <<= 8; + } + SHA256_WritecharBlock(p); + + for (i = 0; i < 8; i++) + { + *digest++ = (char)((p->state[i] >> 24) & 0xFF); + *digest++ = (char)((p->state[i] >> 16) & 0xFF); + *digest++ = (char)((p->state[i] >> 8) & 0xFF); + *digest++ = (char)((p->state[i]) & 0xFF); + } + SHA256_Init(p); +} +