Replace OpenSSL SHA1 implementation with one copied from boost/uuid

Although the code already uses boost, boost/uuid hides the sha1
implementation in a deeply nested namespace, which is just too bizarre to
use. Also the name of that namespace suggests that it is just an
implementation detail that could go away without notice.
stable-5.2
Günther Brammer 2011-01-10 21:30:40 +01:00
parent b660f093fc
commit 0367dabb63
7 changed files with 226 additions and 27 deletions

View File

@ -396,6 +396,7 @@ set(OC_CLONK_SOURCES
src/lib/C4Stat.h
src/lib/PathFinder.cpp
src/lib/PathFinder.h
src/lib/SHA1.h
src/lib/Standard.cpp
src/lib/Standard.h
src/lib/StdAdaptors.h
@ -787,11 +788,6 @@ macro(FINDLIB lib)
mark_as_advanced(${lib})
endmacro(FINDLIB)
# isilkor 09-05-29: Don't use FindOpenSSL, because that wants to link to ssleay
FINDLIB(OPENSSL_LIBRARIES NAMES crypto libeay32)
FIND_PATH(OPENSSL_INCLUDE_DIR NAMES openssl/sha.h)
include_directories(${OPENSSL_INCLUDE_DIR})
if(USE_CONSOLE)
find_package(Readline)
include_directories(${READLINE_INCLUDE_DIRS})
@ -953,7 +949,6 @@ target_link_libraries(clonk
${ZLIB_LIBRARIES}
${PNG_LIBRARIES}
${JPEG_LIBRARIES}
${OPENSSL_LIBRARIES}
${ICONV_LIBRARY}
${VORBIS_LIBRARY}
${VORBISFILE_LIBRARY}
@ -961,11 +956,9 @@ target_link_libraries(clonk
)
target_link_libraries(c4group
${ZLIB_LIBRARIES}
${OPENSSL_LIBRARIES}
)
target_link_libraries(c4script
${ZLIB_LIBRARIES}
${OPENSSL_LIBRARIES}
)
if(HAVE_PTHREAD)
target_link_libraries(netpuncher
@ -1333,8 +1326,8 @@ set(CPACK_PACKAGE_FILE_NAME "openclonk-${C4XVER1}.${C4XVER2}.${C4XVER3}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "openclonk-src-${C4XVER1}.${C4XVER2}.${C4XVER3}")
set(CPACK_SOURCE_GENERATOR "TGZ;ZIP")
# Somebody who uses Debian/Ubuntu should set this
#set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libgcc1, libx11, libXrandr, libXpm, libGLEW, libGL, libpng, libssl, libSDL-1.2, libSDL_mixer-1.2, gtk2, libjpeg, zlib")
#set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libgcc1, libx11, libXrandr, libXpm, libGLEW, libGL, libpng, libSDL-1.2, libSDL_mixer-1.2, gtk2, libjpeg, zlib")
set(CPACK_RPM_PACKAGE_LICENSE "MIT")
set(CPACK_RPM_PACKAGE_REQUIRES "libc6, libgcc1, libx11, libXrandr, libXpm, libGLEW, libGL, libpng, libssl, libSDL-1.2, libSDL_mixer-1.2, gtk2, libjpeg, zlib")
set(CPACK_RPM_PACKAGE_REQUIRES "libc6, libgcc1, libx11, libXrandr, libXpm, libGLEW, libGL, libpng, libSDL-1.2, libSDL_mixer-1.2, gtk2, libjpeg, zlib")
include(CPack)

View File

@ -125,6 +125,7 @@ src/config/C4SecurityCertificates.cpp \
src/config/C4SecurityCertificates.h \
src/lib/C4InputValidation.cpp \
src/lib/C4InputValidation.h \
src/lib/SHA1.h \
src/lib/Standard.cpp \
src/lib/Standard.h \
src/lib/StdBuf.cpp \
@ -583,8 +584,7 @@ clonk_LDADD = \
$(SDL_LIBS) \
$(PTHREAD_LIBS) \
$(Z_LIBS) \
$(CLONK_LIBS) \
$(OPENSSL_LIBS)
$(CLONK_LIBS)
# build the resources
.rc.o:
@ -621,7 +621,6 @@ c4group_LDADD = \
lib.a \
$(LIBICONV) \
$(Z_LIBS) \
$(OPENSSL_LIBS) \
$(PTHREAD_LIBS)
## gunzip4c4group
@ -683,7 +682,6 @@ src/c4group/C4LangStringTable.cpp
c4script_LDADD = \
lib.a \
$(OPENSSL_LIBS) \
$(Z_LIBS)
dist_doc_DATA = planet/AUTHORS planet/COPYING licenses/LGPL.txt licenses/OpenSSL.txt Credits.txt

View File

@ -213,10 +213,6 @@ AX_BOOST_BASE([1.40.0], [], [
AC_MSG_ERROR([Boost not found.])
])
# OpenSSL
AC_CHECK_LIB(crypto, BIO_new, [OPENSSL_LIBS="-lcrypto"],
[AC_MSG_ERROR([libcrypto (OpenSSL) not found.])])
# Check for libjpeg
AC_CHECK_LIB(jpeg, jpeg_read_header, [
CLONK_LIBS="-ljpeg $CLONK_LIBS"

View File

@ -28,6 +28,7 @@
#endif
#include <StdFile.h>
#include <CStdFile.h>
#include <SHA1.h>
#include <zlib.h>
#include <stdio.h>
@ -36,7 +37,6 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <openssl/sha.h>
CStdFile::CStdFile()
{
@ -369,8 +369,7 @@ bool GetFileSHA1(const char *szFilename, BYTE *pSHA1)
if (!File.Open(szFilename))
return false;
// calculcate CRC
SHA_CTX ctx;
if (!SHA1_Init(&ctx)) return false;
sha1 ctx;
for (;;)
{
// read a chunk of data
@ -379,12 +378,11 @@ bool GetFileSHA1(const char *szFilename, BYTE *pSHA1)
if (!iSize)
break;
// update CRC
if (!SHA1_Update(&ctx, szData, iSize))
return false;
ctx.process_bytes(szData, iSize);
}
// close file
File.Close();
// finish calculation
SHA1_Final(pSHA1, &ctx);
ctx.get_digest((sha1::digest_type) pSHA1);
return true;
}

215
src/lib/SHA1.h 100644
View File

@ -0,0 +1,215 @@
/*
Copyright 2007 Andy Tompkins.
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// This is a byte oriented implementation
// Note: this implementation does not handle message longer than
// 2^32 bytes.
#ifndef LIB_SHA1_H
#define LIB_SHA1_H
#include <boost/static_assert.hpp>
#include <cstddef>
BOOST_STATIC_ASSERT(sizeof(unsigned char)*8 == 8);
BOOST_STATIC_ASSERT(sizeof(unsigned int)*8 == 32);
#define SHA_DIGEST_LENGTH 20
class sha1
{
public:
typedef unsigned int(&digest_type)[5];
public:
sha1();
void reset();
void process_byte(unsigned char byte);
void process_block(void const* bytes_begin, void const* bytes_end);
void process_bytes(void const* buffer, std::size_t byte_count);
void get_digest(digest_type digest);
private:
void process_block();
static unsigned int left_rotate(unsigned int x, std::size_t n)
{
return (x<<n) ^ (x>> (32-n));
}
private:
unsigned int h_[5];
unsigned char block_[64];
std::size_t block_byte_index_;
std::size_t byte_count_;
};
inline sha1::sha1()
{
reset();
}
inline void sha1::reset()
{
h_[0] = 0x67452301;
h_[1] = 0xEFCDAB89;
h_[2] = 0x98BADCFE;
h_[3] = 0x10325476;
h_[4] = 0xC3D2E1F0;
block_byte_index_ = 0;
byte_count_ = 0;
}
inline void sha1::process_byte(unsigned char byte)
{
block_[block_byte_index_++] = byte;
++byte_count_;
if (block_byte_index_ == 64) {
block_byte_index_ = 0;
process_block();
}
}
inline void sha1::process_block(void const* bytes_begin, void const* bytes_end)
{
unsigned char const* begin = static_cast<unsigned char const*>(bytes_begin);
unsigned char const* end = static_cast<unsigned char const*>(bytes_end);
for(; begin != end; ++begin) {
process_byte(*begin);
}
}
inline void sha1::process_bytes(void const* buffer, std::size_t byte_count)
{
unsigned char const* b = static_cast<unsigned char const*>(buffer);
process_block(b, b+byte_count);
}
inline void sha1::process_block()
{
unsigned int w[80];
for (std::size_t i=0; i<16; ++i) {
w[i] = (block_[i*4 + 0] << 24);
w[i] |= (block_[i*4 + 1] << 16);
w[i] |= (block_[i*4 + 2] << 8);
w[i] |= (block_[i*4 + 3]);
}
for (std::size_t i=16; i<80; ++i) {
w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
}
unsigned int a = h_[0];
unsigned int b = h_[1];
unsigned int c = h_[2];
unsigned int d = h_[3];
unsigned int e = h_[4];
for (std::size_t i=0; i<80; ++i) {
unsigned int f;
unsigned int k;
if (i<20) {
f = (b & c) | (~b & d);
k = 0x5A827999;
} else if (i<40) {
f = b ^ c ^ d;
k = 0x6ED9EBA1;
} else if (i<60) {
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
} else {
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
unsigned temp = left_rotate(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = left_rotate(b, 30);
b = a;
a = temp;
}
h_[0] += a;
h_[1] += b;
h_[2] += c;
h_[3] += d;
h_[4] += e;
}
inline void sha1::get_digest(digest_type digest)
{
std::size_t bit_count = byte_count_*8;
// append the bit '1' to the message
process_byte(0x80);
// append k bits '0', where k is the minimum number >= 0
// such that the resulting message length is congruent to 56 (mod 64)
// check if there is enough space for padding and bit_count
if (block_byte_index_ > 56) {
// finish this block
while (block_byte_index_ != 0) {
process_byte(0);
}
// one more block
while (block_byte_index_ < 56) {
process_byte(0);
}
} else {
while (block_byte_index_ < 56) {
process_byte(0);
}
}
// append length of message (before pre-processing)
// as a 64-bit big-endian integer
process_byte(0);
process_byte(0);
process_byte(0);
process_byte(0);
process_byte( static_cast<unsigned char>((bit_count>>24) & 0xFF));
process_byte( static_cast<unsigned char>((bit_count>>16) & 0xFF));
process_byte( static_cast<unsigned char>((bit_count>>8 ) & 0xFF));
process_byte( static_cast<unsigned char>((bit_count) & 0xFF));
// get final digest
digest[0] = h_[0];
digest[1] = h_[1];
digest[2] = h_[2];
digest[3] = h_[3];
digest[4] = h_[4];
}
#endif

View File

@ -22,10 +22,9 @@
#ifndef C4LEAGUE_H_INCLUDED
#define C4LEAGUE_H_INCLUDED
#include <openssl/sha.h>
#include <C4Network2Reference.h>
#include <C4Gui.h>
#include <SHA1.h>
#define C4League_Name_Valid_Characters "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD9\xDA\xDB\xDC\xDD\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF\x20\x2E\x2D\x5F"

View File

@ -24,7 +24,7 @@
#include <StdAdaptors.h>
#include <StdSync.h>
#include <openssl/sha.h>
#include <SHA1.h>
const uint32_t C4NetResChunkSize = 10U * 1024U;