772 changes: 772 additions & 0 deletions 3rdparty/sha2/sha2.c

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions 3rdparty/sha2/sha2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
---------------------------------------------------------------------------
Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
LICENSE TERMS
The free distribution and use of this software in both source and binary
form is allowed (with or without changes) provided that:
1. distributions of this source code include the above copyright
notice, this list of conditions and the following disclaimer;
2. distributions in binary form include the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other associated materials;
3. the copyright holder's name is not used to endorse products
built using this software without specific written permission.
ALTERNATIVELY, provided that this notice is retained in full, this product
may be distributed under the terms of the GNU General Public License (GPL),
in which case the provisions of the GPL apply INSTEAD OF those given above.
DISCLAIMER
This software is provided 'as is' with no explicit or implied warranties
in respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 01/08/2005
*/

#ifndef _SHA2_H
#define _SHA2_H

#include <stdlib.h>

#define SHA_64BIT

/* define the hash functions that you need */
#define SHA_2 /* for dynamic hash length */
#define SHA_224
#define SHA_256
#ifdef SHA_64BIT
# define SHA_384
# define SHA_512
# define NEED_UINT_64T
#endif

#include "brg_types.h"

#if defined(__cplusplus)
extern "C"
{
#endif

/* Note that the following function prototypes are the same */
/* for both the bit and byte oriented implementations. But */
/* the length fields are in bytes or bits as is appropriate */
/* for the version used. Bit sequences are arrays of bytes */
/* in which bit sequence indexes increase from the most to */
/* the least significant end of each byte */

#define SHA224_DIGEST_SIZE 28
#define SHA224_BLOCK_SIZE 64
#define SHA256_DIGEST_SIZE 32
#define SHA256_BLOCK_SIZE 64

/* type to hold the SHA256 (and SHA224) context */

typedef struct
{ uint_32t count[2];
uint_32t hash[8];
uint_32t wbuf[16];
} sha256_ctx;

typedef sha256_ctx sha224_ctx;

VOID_RETURN sha256_compile(sha256_ctx ctx[1]);

VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
#define sha224_hash sha256_hash
VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len);

VOID_RETURN sha256_begin(sha256_ctx ctx[1]);
VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len);

#ifndef SHA_64BIT

typedef struct
{ union
{ sha256_ctx ctx256[1];
} uu[1];
uint_32t sha2_len;
} sha2_ctx;

#define SHA2_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE

#else

#define SHA384_DIGEST_SIZE 48
#define SHA384_BLOCK_SIZE 128
#define SHA512_DIGEST_SIZE 64
#define SHA512_BLOCK_SIZE 128
#define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE

/* type to hold the SHA384 (and SHA512) context */

typedef struct
{ uint_64t count[2];
uint_64t hash[8];
uint_64t wbuf[16];
} sha512_ctx;

typedef sha512_ctx sha384_ctx;

typedef struct
{ union
{ sha256_ctx ctx256[1];
sha512_ctx ctx512[1];
} uu[1];
uint_32t sha2_len;
} sha2_ctx;

VOID_RETURN sha512_compile(sha512_ctx ctx[1]);

VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
#define sha384_hash sha512_hash
VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len);

VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len);

INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]);
VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);

#endif

#if defined(__cplusplus)
}
#endif

#endif
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ CHECK_FUNCTION_EXISTS(strlcpy HAVE_VALID_STRLCPY)
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

add_subdirectory(3rdparty/miniupnpc)
add_subdirectory(3rdparty/micro-ecc)
add_subdirectory(3rdparty/sha2)
add_subdirectory(lib)
add_subdirectory(src)
add_subdirectory(po)
Expand Down
4 changes: 4 additions & 0 deletions COPYING.NONGPL
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ data/texpages/bdrops/backdrop6.png
- CC0 - Copyright (c) 2010-2011 jorzi
data/base/texpages/page-25-sky-urban.png
- CC0, original work by cybersphinx
3rdparty/micro-ecc/*
- BSD, Copyright (c) 2014, Kenneth MacKay
3rdparty/sha2/*
- BSD, Copyright (c) 2002, Dr Brian Gladman, Worcester, UK
3rdparty/miniupnpc/*
- BSD, copyright Thomas Bernard (http://miniupnp.free.fr)
3rdparty/quesoglc/*
Expand Down
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ endif
SUBDIRS = \
win32 \
3rdparty/miniupnpc \
3rdparty/micro-ecc \
3rdparty/sha2 \
lib/framework \
lib/exceptionhandler \
lib/script \
Expand Down
5 changes: 3 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ case ${host_os} in
*mingw32*)
host_os_mingw32=yes
AC_CHECK_TOOL([WINDRES], [windres], AC_MSG_ERROR([windres not found]))
WIN32_LIBS='-ldbghelp -lshfolder -lshlwapi -lpsapi -lshell32 -lwinmm -lws2_32 -liphlpapi'
WIN32_LIBS='-ldbghelp -lshfolder -lshlwapi -lpsapi -lshell32 -lwinmm -lws2_32 -liphlpapi -ladvapi32'
AC_SUBST([WIN32_LIBS], [${WIN32_LIBS}])
LDFLAGS="-Wl,-subsystem,windows ${LDFLAGS}"
;;
Expand Down Expand Up @@ -418,7 +418,6 @@ PKG_CHECK_MODULES([VORBIS], [vorbis >= 1.1])
PKG_CHECK_MODULES([VORBISFILE], [vorbisfile >= 1.1])

PKG_CHECK_MODULES([GLEW], [glew >= 1.5.2])
PKG_CHECK_MODULES([LIBCRYPTO], [libcrypto >= 1.0.0])

# Checks for libraries:

Expand Down Expand Up @@ -503,6 +502,8 @@ AC_CONFIG_FILES([Makefile
win32/Makefile
tests/Makefile
3rdparty/miniupnpc/Makefile
3rdparty/micro-ecc/Makefile
3rdparty/sha2/Makefile
lib/framework/Makefile
lib/exceptionhandler/Makefile
lib/gamelib/Makefile
Expand Down
2 changes: 1 addition & 1 deletion docker/cross-compile/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN apt-get -u update && apt-get -y install \
autoconf automake autopoint bash bison bzip2 flex gettext\
git g++ gperf intltool libffi-dev libgdk-pixbuf2.0-dev \
libtool libtool-bin libltdl-dev libssl-dev libxml-parser-perl make \
openssl p7zip-full patch perl pkg-config python ruby scons \
p7zip-full patch perl pkg-config python ruby scons \
sed unzip wget xz-utils cmake asciidoc nsis ninja-build

RUN git clone https://github.com/mxe/mxe.git
Expand Down
4 changes: 2 additions & 2 deletions lib/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cmake_minimum_required (VERSION 3.5)

find_package(OpenSSL REQUIRED)
find_package(PhysFS REQUIRED)

file(GLOB HEADERS "*.h")
file(GLOB SRC "*.cpp")

add_library(framework STATIC ${HEADERS} ${SRC})
target_link_libraries(framework PUBLIC Qt5::Core OpenSSL::SSL OpenSSL::Crypto ${PHYSFS_LIBRARY})
target_link_libraries(framework PUBLIC Qt5::Core ${PHYSFS_LIBRARY})
target_link_libraries(framework PRIVATE microecc sha2)
target_include_directories(framework PUBLIC "${PHYSFS_INCLUDE_DIR}" "${CMAKE_SOURCE_DIR}/3rdparty")
target_compile_definitions(framework PRIVATE "YY_NO_UNISTD_H")
target_compile_definitions(framework PUBLIC "_CRT_SECURE_NO_WARNINGS")
734 changes: 560 additions & 174 deletions lib/framework/crc.cpp

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions lib/framework/crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,21 @@ class EcKey
Sig sign(void const *data, size_t dataLen) const;
bool verify(Sig const &sig, void const *data, size_t dataLen) const;

// Exports the specified key (public / private) bytes as a Key (vector of uint8_t).
//
// For the public key, the format follows the ANSI X9.63 standard using a byte string of 0x04 || X || Y.
// (This is the format expected by OpenSSL's o2i_ECPublicKey function, and Apple's SecKeyCreateWithData
// function for EC public keys.)
//
// For the private key, the format is the DER-encoded ECPrivateKey SEQUENCE.
Key toBytes(Privacy privacy) const;

// Imports the specified (public / private) Key previously exported via toBytes().
//
// See the documentation for toBytes() for the expected format.
//
// If supplied the exported public key, the EcKey object supports verify.
// If supplied the exported private key, the EcKey object supports sign & verify.
void fromBytes(Key const &key, Privacy privacy);

static EcKey generate();
Expand Down
8 changes: 8 additions & 0 deletions lib/framework/framework.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\3rdparty\micro-ecc\uECC.c" />
<ClCompile Include="..\..\3rdparty\sha2\sha2.c" />
<ClCompile Include="crc.cpp" />
<ClCompile Include="debug.cpp" />
<ClCompile Include="frame.cpp" />
Expand All @@ -208,6 +210,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\3rdparty\micro-ecc\types.h" />
<ClInclude Include="..\..\3rdparty\micro-ecc\uECC_vli.h" />
<ClInclude Include="..\..\3rdparty\micro-ecc\uECC.h" />
<ClInclude Include="..\..\3rdparty\sha2\brg_endian.h" />
<ClInclude Include="..\..\3rdparty\sha2\brg_types.h" />
<ClInclude Include="..\..\3rdparty\sha2\sha2.h" />
<ClInclude Include="crc.h" />
<ClInclude Include="debug.h" />
<ClInclude Include="endian_hack.h" />
Expand Down
30 changes: 30 additions & 0 deletions lib/framework/framework.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Source Files\micro-ecc">
<UniqueIdentifier>{24dc2be6-a296-40f5-ad4d-4bf88530006f}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\sha2">
<UniqueIdentifier>{85c5fd1a-5056-4e4f-bed9-ff02463e1004}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="crc.cpp">
Expand Down Expand Up @@ -62,6 +68,12 @@
<ClCompile Include="strres_parser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\3rdparty\sha2\sha2.c">
<Filter>Source Files\sha2</Filter>
</ClCompile>
<ClCompile Include="..\..\3rdparty\micro-ecc\uECC.c">
<Filter>Source Files\micro-ecc</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="crc.h">
Expand Down Expand Up @@ -154,5 +166,23 @@
<ClInclude Include="resource_parser.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\micro-ecc\uECC.h">
<Filter>Source Files\micro-ecc</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\micro-ecc\uECC_vli.h">
<Filter>Source Files\micro-ecc</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\micro-ecc\types.h">
<Filter>Source Files\micro-ecc</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\sha2\sha2.h">
<Filter>Source Files\sha2</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\sha2\brg_types.h">
<Filter>Source Files\micro-ecc</Filter>
</ClInclude>
<ClInclude Include="..\..\3rdparty\sha2\brg_endian.h">
<Filter>Source Files\micro-ecc</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ SET_TARGET_PROPERTIES(warzone2100 PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${C
SET_TARGET_PROPERTIES(warzone2100 PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}")
SET_TARGET_PROPERTIES(warzone2100 PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}")

find_package(OpenSSL REQUIRED)

target_link_libraries(warzone2100 exception-handler gamelib ivis-opengl netplay script sdl-backend framework sequence sound widget OpenSSL::SSL)
target_link_libraries(warzone2100 exception-handler gamelib ivis-opengl netplay script sdl-backend framework sequence sound widget)
target_link_libraries(warzone2100
Qt5::Script
Qt5::Gui
Expand Down
31 changes: 20 additions & 11 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AM_CPPFLAGS = -DYY_NO_INPUT $(SDL_CFLAGS) $(PHYSFS_CFLAGS) $(PNG_CFLAGS) $(VORBISFILE_CFLAGS) $(VORBIS_CFLAGS) $(OPENAL_CFLAGS) $(FONT_CFLAGS) $(OPENGL_CFLAGS) $(WZ_CPPFLAGS) $(GLEW_CFLAGS) $(LIBCRYPTO_CFLAGS)
AM_CPPFLAGS = -DYY_NO_INPUT $(SDL_CFLAGS) $(PHYSFS_CFLAGS) $(PNG_CFLAGS) $(VORBISFILE_CFLAGS) $(VORBIS_CFLAGS) $(OPENAL_CFLAGS) $(FONT_CFLAGS) $(OPENGL_CFLAGS) $(WZ_CPPFLAGS) $(GLEW_CFLAGS)
AM_CFLAGS = $(WZ_CFLAGS)
AM_CXXFLAGS = $(WZ_CXXFLAGS) $(QT5_CFLAGS)

Expand Down Expand Up @@ -303,24 +303,33 @@ endif

if BACKEND_QT
if PORTABLE
warzone2100_portable_LIBS += $(top_builddir)/lib/qtgame/libqtgame.a \
$(top_builddir)/lib/framework/libframework.a
warzone2100_portable_LIBS += $(top_builddir)/lib/qtgame/libqtgame.a
else
warzone2100_LIBS += $(top_builddir)/lib/qtgame/libqtgame.a \
$(top_builddir)/lib/framework/libframework.a
warzone2100_LIBS += $(top_builddir)/lib/qtgame/libqtgame.a
endif
endif

if BACKEND_SDL
if PORTABLE
warzone2100_portable_LIBS += $(top_builddir)/lib/sdl/libsdl.a \
$(top_builddir)/lib/framework/libframework.a
warzone2100_portable_LIBS += $(top_builddir)/lib/sdl/libsdl.a
else
warzone2100_LIBS += $(top_builddir)/lib/sdl/libsdl.a \
$(top_builddir)/lib/framework/libframework.a
warzone2100_LIBS += $(top_builddir)/lib/sdl/libsdl.a
endif
endif

# lib/framework (and dependencies)
if PORTABLE
warzone2100_portable_LIBS += \
$(top_builddir)/lib/framework/libframework.a \
$(top_builddir)/3rdparty/micro-ecc/libmicroecc.a \
$(top_builddir)/3rdparty/sha2/libsha2.a
else
warzone2100_LIBS += \
$(top_builddir)/lib/framework/libframework.a \
$(top_builddir)/3rdparty/micro-ecc/libmicroecc.a \
$(top_builddir)/3rdparty/sha2/libsha2.a
endif

# Here, we need to bring in the "plugins" for Qt5 and they MUST come before the other QT libs.
# FIXME: At a future date, we should check if they want static builds, but, since we only support those for now...
if MINGW32
Expand All @@ -332,11 +341,11 @@ endif
if PORTABLE
warzone2100_portable_LDADD = $(warzone2100_portable_LIBS) $(LTLIBINTL) $(SDL_LIBS) $(PHYSFS_LIBS) $(PNG_LIBS) $(VORBISFILE_LIBS) $(VORBIS_LIBS) \
$(THEORA_LIBS) $(OPENAL_LIBS) $(FONT_LIBS) $(OPENGL_LIBS) $(SQT5_LIBS) $(QT5_LIBS) $(GLEW_LIBS) \
$(X_LIBS) $(X_EXTRA_LIBS) $(LIBCRYPTO_LIBS) $(LDFLAGS)
$(X_LIBS) $(X_EXTRA_LIBS) $(LDFLAGS)
else
warzone2100_LDADD = $(warzone2100_LIBS) $(LTLIBINTL) $(SDL_LIBS) $(PHYSFS_LIBS) $(PNG_LIBS) $(VORBISFILE_LIBS) $(VORBIS_LIBS) \
$(THEORA_LIBS) $(OPENAL_LIBS) $(FONT_LIBS) $(OPENGL_LIBS) $(SQT5_LIBS) $(QT5_LIBS) $(GLEW_LIBS) \
$(X_LIBS) $(X_EXTRA_LIBS) $(LIBCRYPTO_LIBS) $(LDFLAGS)
$(X_LIBS) $(X_EXTRA_LIBS) $(LDFLAGS)
endif


Expand Down
26 changes: 0 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@
# define WZ_DATADIR "data"
#endif

#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>


enum FOCUS_STATE
{
Expand Down Expand Up @@ -788,10 +783,6 @@ void mainLoop()
}
realTimeUpdate(); // Update realTime.
}

// Feed a bit of randomness into libcrypto.
unsigned buf[] = {mouseX(), mouseY(), realTime, graphicsTime, gameTime, (unsigned) rand(), 4}; // http://xkcd.com/221/
RAND_add(buf, sizeof(buf), 1);
}

bool getUTF8CmdLine(int *const utfargc WZ_DECL_UNUSED, const char *** const utfargv WZ_DECL_UNUSED) // explicitely pass by reference
Expand Down Expand Up @@ -849,23 +840,6 @@ int realmain(int argc, char *argv[])
const char **utfargv = (const char **)argv;
wzMain(argc, argv); // init Qt integration first

#if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < 0x10100000L
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// The libcrypto startup stuff... May or may not actually be needed for anything at all.
ERR_load_crypto_strings(); // This is needed for descriptive error messages.
OpenSSL_add_all_algorithms(); // Don't actually use the EVP functions, so probably not needed.
OPENSSL_config(nullptr); // What does this actually do?
#pragma GCC diagnostic pop
#else
// The libcrypto startup stuff... May or may not actually be needed for anything at all.
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, nullptr);
#endif

#ifdef WZ_OS_WIN
RAND_screen(); // Uses a screenshot as a random seed, on systems lacking /dev/random.
#endif

#ifdef WZ_OS_MAC
cocoaInit();
#endif
Expand Down
4 changes: 2 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AM_CPPFLAGS = $(SDL_CFLAGS) $(PHYSFS_CFLAGS) $(PNG_CFLAGS) $(OPENGL_CFLAGS) $(WZ_CPPFLAGS) -I$(top_srcdir)/tools \
-I. -I$(top_srcdir)/lib/framework $(QT5_CFLAGS) $(LIBCRYPTO_CFLAGS) $(OPENGLC_CFLAGS) $(GLEW_CFLAGS)
-I. -I$(top_srcdir)/lib/framework $(QT5_CFLAGS) $(OPENGLC_CFLAGS) $(GLEW_CFLAGS)
AM_CFLAGS = $(WZ_CFLAGS)
AM_CXXFLAGS = $(WZ_CXXFLAGS)

Expand All @@ -18,7 +18,7 @@ check_PROGRAMS = maptest modeltest framework_linktest ivis_linktest
#qtscripttest_LDADD = $(PHYSFS_LIBS) $(QT5_LIBS)

framework_linktest_SOURCES = framework_linktest.cpp
framework_linktest_LDADD = $(top_builddir)/lib/framework/libframework.a $(PHYSFS_LIBS) $(LIBCRYPTO_LIBS) $(LDFLAGS)
framework_linktest_LDADD = $(top_builddir)/lib/framework/libframework.a $(PHYSFS_LIBS) $(LDFLAGS)

ivis_linktest_SOURCES = ivis_linktest.cpp
ivis_linktest_LDADD =
Expand Down