summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am4
-rw-r--r--src/account-server/account.hpp4
-rw-r--r--src/account-server/accounthandler.cpp26
-rw-r--r--src/utils/encryption.cpp75
-rw-r--r--src/utils/encryption.h49
-rw-r--r--src/utils/sha2.cpp699
-rw-r--r--src/utils/sha2.h252
7 files changed, 1098 insertions, 11 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 482772b5..2bdb588b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -54,10 +54,14 @@ tmwserv_account_SOURCES = \
net/netcomputer.cpp \
serialize/characterdata.hpp \
utils/functors.h \
+ utils/encryption.h \
+ utils/encryption.cpp \
utils/logger.h \
utils/logger.cpp \
utils/processorutils.hpp \
utils/processorutils.cpp \
+ utils/sha2.h \
+ utils/sha2.cpp \
utils/stringfilter.h \
utils/stringfilter.cpp \
utils/timer.cpp \
diff --git a/src/account-server/account.hpp b/src/account-server/account.hpp
index 9b4abb68..4414cb27 100644
--- a/src/account-server/account.hpp
+++ b/src/account-server/account.hpp
@@ -191,8 +191,8 @@ class Account
private:
std::string mName; /**< user name */
- std::string mPassword; /**< user password (encrypted) */
- std::string mEmail; /**< user email address */
+ std::string mPassword; /**< user password (encrypted with salt) */
+ std::string mEmail; /**< user email address (encrypted) */
Characters mCharacters; /**< Character data */
int mID; /**< unique id */
unsigned char mLevel; /**< account level */
diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp
index dba3ca14..20b3b9be 100644
--- a/src/account-server/accounthandler.cpp
+++ b/src/account-server/accounthandler.cpp
@@ -40,6 +40,7 @@
#include "utils/stringfilter.h"
#include "utils/tokencollector.hpp"
#include "utils/tokendispenser.hpp"
+#include "utils/encryption.h"
class AccountHandler : public ConnectionHandler
{
@@ -293,8 +294,12 @@ static void handleRegisterMessage(AccountClient &computer, MessageIn &msg)
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
- else if ((password.length() < MIN_PASSWORD_LENGTH) ||
- (password.length() > MAX_PASSWORD_LENGTH))
+ else if (password.length() < MIN_PASSWORD_LENGTH ||
+ password.length() > MAX_PASSWORD_LENGTH)
+ {
+ reply.writeByte(ERRMSG_INVALID_ARGUMENT);
+ }
+ else if (stringFilter->findDoubleQuotes(password))
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
@@ -313,7 +318,7 @@ static void handleRegisterMessage(AccountClient &computer, MessageIn &msg)
reply.writeByte(REGISTER_EXISTS_USERNAME);
}
// Find out whether the email is already in use.
- else if (storage->doesEmailAddressExist(email))
+ else if (storage->doesEmailAddressExist(Encryption::GetSHA2Hash(email)))
{
reply.writeByte(REGISTER_EXISTS_EMAIL);
}
@@ -321,8 +326,12 @@ static void handleRegisterMessage(AccountClient &computer, MessageIn &msg)
{
Account *acc = new Account;
acc->setName(username);
- acc->setPassword(password);
- acc->setEmail(email);
+ // We hash the password using the username
+ // as salt.
+ acc->setPassword(Encryption::GetSHA2Hash(
+ std::string(username+password)));
+ // We hash Email server-side without using a salt.
+ acc->setEmail(Encryption::GetSHA2Hash(email));
acc->setLevel(AL_NORMAL);
storage->addAccount(acc);
@@ -400,13 +409,13 @@ static void handleEmailChangeMessage(AccountClient &computer, MessageIn &msg)
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
- else if (storage->doesEmailAddressExist(email))
+ else if (storage->doesEmailAddressExist(Encryption::GetSHA2Hash(email)))
{
reply.writeByte(EMAILCHG_EXISTS_EMAIL);
}
else
{
- acc->setEmail(email);
+ acc->setEmail(Encryption::GetSHA2Hash(email));
// Keep the database up to date otherwise we will go out of sync
storage->flush(acc);
reply.writeByte(ERRMSG_OK);
@@ -444,8 +453,7 @@ static void handlePasswordChangeMessage(AccountClient &computer, MessageIn &msg)
{
reply.writeByte(ERRMSG_NO_LOGIN);
}
- else if (newPassword.length() < MIN_PASSWORD_LENGTH ||
- newPassword.length() > MAX_PASSWORD_LENGTH)
+ else if (newPassword.length() != Encryption::SHA256HashLength)
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
diff --git a/src/utils/encryption.cpp b/src/utils/encryption.cpp
new file mode 100644
index 00000000..e3483e07
--- /dev/null
+++ b/src/utils/encryption.cpp
@@ -0,0 +1,75 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: $
+ */
+
+#include "encryption.h"
+
+#include <time.h>
+
+std::string Encryption::GetSHA2Hash(std::string stringToHash)
+{
+
+ if (stringToHash.length() == 0)
+ return "";
+
+ sha2 mySha;
+ return mySha.GetHash(shaType, (const sha_byte *)stringToHash.c_str(), stringToHash.size());
+}
+
+char _getRandomCharacter()
+{
+ char result = '0';
+
+ // Taking a number of character taken between 33 and 127
+ // (Every normal characters from ASCII table).
+ int number = (rand() % 94) + 33;
+
+ // Those characters are dodged to ease user input and avoid database
+ // breaks: " ' , ` \ ^ * / ~ |
+ if (number == 34 || number == 39 || number == 42 || number == 44 ||
+ number == 47 || number == 92 || number == 94 || number == 96 ||
+ number == 124 || number == 126)
+ number++;
+
+ result = char(number);
+ return result;
+}
+
+/**
+ * Using this function, the random salt changes at every second.
+ */
+std::string Encryption::CreateRandomPassword()
+{
+
+ std::string result = "";
+
+ // Ititializing random seed.
+ srand(time(NULL));
+ // Taking a number of character taken between 20 and 30.
+ int characterNumber = (rand() % 10) + 20;
+
+ for (int a = 1; a < characterNumber; a++)
+ {
+ result += _getRandomCharacter();
+ }
+ return result;
+}
diff --git a/src/utils/encryption.h b/src/utils/encryption.h
new file mode 100644
index 00000000..0a1538db
--- /dev/null
+++ b/src/utils/encryption.h
@@ -0,0 +1,49 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: $
+ */
+
+#ifndef _TMW_UTILS_ENCRYPTION_H
+#define _TMW_UTILS_ENCRYPTION_H
+
+#include <string>
+
+#include "sha2.h"
+
+namespace Encryption {
+
+/** SHA256 Encryption related */
+const char SHA256HashLength = 64;
+
+/** Set the encryption strength */
+const sha2::SHA_TYPE shaType = sha2::enuSHA256;
+
+/** Create an SHA2 hash from a given string */
+std::string GetSHA2Hash(std::string stringToHash);
+
+/** Create a random string, suitable for a user to type,
+ * and that doesn't break a database */
+std::string CreateRandomPassword();
+
+}
+
+#endif //TMW_UTILS_ENCRYPTION_H
+
diff --git a/src/utils/sha2.cpp b/src/utils/sha2.cpp
new file mode 100644
index 00000000..aba6a532
--- /dev/null
+++ b/src/utils/sha2.cpp
@@ -0,0 +1,699 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Additional information about this file can be read below:
+ * $Id:$
+ */
+
+/*************************************************************
+
+ This program is a C++ implementation of the Secure Hash Algorithm (SHA)
+ that handles the variations from the original 160 bit to 224, 256, 384
+ and 512 bit. The program is intended to be platform independant and
+ has been tested on little-endian (Intel) and big-endian (Sun) machines.
+
+ This program is based on a C version written by Aaron D. Gifford
+ (as of 11/22/2004 his code could be found at http://www.adg.us/computers/sha.html).
+ Attempts to contact him were unsuccessful. I greatly condensed his version
+ and shared as much code and data as I could think of. I also inlined
+ a lot of code that were macros in his version. My version detects
+ endian-ness automatically and adjusts itself accordingly. This program
+ has been tested with Visual C++ versions 6/7 and Dev-C++ on Windows,
+ g++ on Linux and CC on Solaris (g++ on Solaris gave a bus error).
+
+ While I did make half-hearted attempts to optimize as I went along
+ (testing on Wintel), any serious attempt at fast implementation is
+ probably going to need to make use of in-lined assembly which is not
+ very portable.
+
+ The goal of this implementation is ease of use. As much as possible
+ I tried to hide implementation details while making it trivial to change
+ the size of the hash and get the results. The string and charactar
+ array value of the hash is supplied as human-readable hex; the raw value
+ can also be obtained.
+
+ If you use this implementation somewhere I would like to be credited
+ with my work (a link to my page below is fine). I add no license
+ restriction beyond any that is made by the original author. This
+ code comes with no warrenty expressed or implied, use at your own
+ risk!
+
+ Keith Oxenrider
+ koxenrider[at]sol[dash]biotech[dot]com
+ The latest version of this code should be available via the page
+ sol-biotech.com/code.
+
+*************************************************************/
+
+#include "sha2.h"
+
+#include <iostream>
+
+using namespace std;
+
+// Hash constant words K for SHA-1:
+const sha_word32 K1_0_TO_19 = 0x5a827999UL;
+const sha_word32 K1_20_TO_39 = 0x6ed9eba1UL;
+const sha_word32 K1_40_TO_59 = 0x8f1bbcdcUL;
+const sha_word32 K1_60_TO_79 = 0xca62c1d6UL;
+
+
+
+//** SHA2 INITIAL HASH VALUES AND CONSTANTS **************************
+
+// Initial hash value H for SHA-1:
+const static sha_word32 sha1_initial_hash_value[5] = {
+ 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL,
+ 0xc3d2e1f0UL
+};
+
+// Hash constant words K for SHA-224 and SHA-256:
+const static sha_word32 K256[64] = {
+ 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
+ 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
+ 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
+ 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
+ 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
+ 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
+ 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
+ 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
+ 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
+ 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
+ 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
+ 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
+ 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
+ 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
+ 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
+ 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
+};
+
+// Initial hash value H for SHA-224:
+const static sha_word32 sha224_initial_hash_value[8] = {
+ 0xc1059ed8UL, 0x367cd507UL, 0x3070dd17UL, 0xf70e5939UL,
+ 0xffc00b31UL, 0x68581511UL, 0x64f98fa7UL, 0xbefa4fa4UL
+};
+
+// Initial hash value H for SHA-256:
+const static sha_word32 sha256_initial_hash_value[8] = {
+ 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
+ 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL
+};
+
+// ui64 Hash constant words K for SHA-384 and SHA-512:
+#ifdef _VC6
+ const static sha_word64 K512[80] = {
+ 0x428a2f98d728ae22ui64, 0x7137449123ef65cdui64,
+ 0xb5c0fbcfec4d3b2fui64, 0xe9b5dba58189dbbcui64,
+ 0x3956c25bf348b538ui64, 0x59f111f1b605d019ui64,
+ 0x923f82a4af194f9bui64, 0xab1c5ed5da6d8118ui64,
+ 0xd807aa98a3030242ui64, 0x12835b0145706fbeui64,
+ 0x243185be4ee4b28cui64, 0x550c7dc3d5ffb4e2ui64,
+ 0x72be5d74f27b896fui64, 0x80deb1fe3b1696b1ui64,
+ 0x9bdc06a725c71235ui64, 0xc19bf174cf692694ui64,
+ 0xe49b69c19ef14ad2ui64, 0xefbe4786384f25e3ui64,
+ 0x0fc19dc68b8cd5b5ui64, 0x240ca1cc77ac9c65ui64,
+ 0x2de92c6f592b0275ui64, 0x4a7484aa6ea6e483ui64,
+ 0x5cb0a9dcbd41fbd4ui64, 0x76f988da831153b5ui64,
+ 0x983e5152ee66dfabui64, 0xa831c66d2db43210ui64,
+ 0xb00327c898fb213fui64, 0xbf597fc7beef0ee4ui64,
+ 0xc6e00bf33da88fc2ui64, 0xd5a79147930aa725ui64,
+ 0x06ca6351e003826fui64, 0x142929670a0e6e70ui64,
+ 0x27b70a8546d22ffcui64, 0x2e1b21385c26c926ui64,
+ 0x4d2c6dfc5ac42aedui64, 0x53380d139d95b3dfui64,
+ 0x650a73548baf63deui64, 0x766a0abb3c77b2a8ui64,
+ 0x81c2c92e47edaee6ui64, 0x92722c851482353bui64,
+ 0xa2bfe8a14cf10364ui64, 0xa81a664bbc423001ui64,
+ 0xc24b8b70d0f89791ui64, 0xc76c51a30654be30ui64,
+ 0xd192e819d6ef5218ui64, 0xd69906245565a910ui64,
+ 0xf40e35855771202aui64, 0x106aa07032bbd1b8ui64,
+ 0x19a4c116b8d2d0c8ui64, 0x1e376c085141ab53ui64,
+ 0x2748774cdf8eeb99ui64, 0x34b0bcb5e19b48a8ui64,
+ 0x391c0cb3c5c95a63ui64, 0x4ed8aa4ae3418acbui64,
+ 0x5b9cca4f7763e373ui64, 0x682e6ff3d6b2b8a3ui64,
+ 0x748f82ee5defb2fcui64, 0x78a5636f43172f60ui64,
+ 0x84c87814a1f0ab72ui64, 0x8cc702081a6439ecui64,
+ 0x90befffa23631e28ui64, 0xa4506cebde82bde9ui64,
+ 0xbef9a3f7b2c67915ui64, 0xc67178f2e372532bui64,
+ 0xca273eceea26619cui64, 0xd186b8c721c0c207ui64,
+ 0xeada7dd6cde0eb1eui64, 0xf57d4f7fee6ed178ui64,
+ 0x06f067aa72176fbaui64, 0x0a637dc5a2c898a6ui64,
+ 0x113f9804bef90daeui64, 0x1b710b35131c471bui64,
+ 0x28db77f523047d84ui64, 0x32caab7b40c72493ui64,
+ 0x3c9ebe0a15c9bebcui64, 0x431d67c49c100d4cui64,
+ 0x4cc5d4becb3e42b6ui64, 0x597f299cfc657e2aui64,
+ 0x5fcb6fab3ad6faecui64, 0x6c44198c4a475817ui64
+ };
+ // Initial hash value H for SHA-384
+ const static sha_word64 sha384_initial_hash_value[8] = {
+ 0xcbbb9d5dc1059ed8ui64, 0x629a292a367cd507ui64,
+ 0x9159015a3070dd17ui64, 0x152fecd8f70e5939ui64,
+ 0x67332667ffc00b31ui64, 0x8eb44a8768581511ui64,
+ 0xdb0c2e0d64f98fa7ui64, 0x47b5481dbefa4fa4ui64
+ };
+
+ // Initial hash value H for SHA-512
+ const static sha_word64 sha512_initial_hash_value[8] = {
+ 0x6a09e667f3bcc908ui64, 0xbb67ae8584caa73bui64,
+ 0x3c6ef372fe94f82bui64, 0xa54ff53a5f1d36f1ui64,
+ 0x510e527fade682d1ui64, 0x9b05688c2b3e6c1fui64,
+ 0x1f83d9abfb41bd6bui64, 0x5be0cd19137e2179ui64
+ };
+#else
+ const static sha_word64 K512[80] = {
+ 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
+ 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
+ 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
+ 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
+ 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
+ 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
+ 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
+ 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
+ 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
+ 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
+ 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
+ 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
+ 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
+ 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
+ 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
+ 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
+ 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
+ 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
+ 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
+ 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
+ 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
+ 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
+ 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
+ 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
+ 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
+ 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
+ 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
+ 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
+ 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
+ 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
+ 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
+ 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
+ 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
+ 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
+ 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
+ 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
+ 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
+ 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
+ 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
+ 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
+ };
+ // Initial hash value H for SHA-384
+ const static sha_word64 sha384_initial_hash_value[8] = {
+ 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
+ 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
+ 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
+ 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL
+ };
+
+ // Initial hash value H for SHA-512
+ const static sha_word64 sha512_initial_hash_value[8] = {
+ 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
+ 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
+ 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
+ 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
+ };
+#endif
+
+/*
+ * Constant used by SHA224/256/384/512_End() functions for converting the
+ * digest to a readable hexadecimal character string:
+ */
+static const char *sha_hex_digits = "0123456789abcdef";
+
+
+void sha2::SHA1_Internal_Transform(const sha_word32 *data) {
+ sha_word32 a, b, c, d, e;
+ sha_word32 *state = (sha_word32*)ctx.state;
+ sha_word32 T1, T2, *W1=(sha_word32*)ctx.buffer;
+ int j;
+
+// Initialize registers with the prev. intermediate value
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+ j = 0;
+ do {
+ if (m_boolIsBigEndian) W1[j] = *data++;
+ else REVERSE32(*data++, W1[j]);// Copy data while converting to host byte order
+ T1 = ROTL32(5, a) + Ch(b, c, d) + e + K1_0_TO_19 + W1[j];
+ e = d;
+ d = c;
+ c = ROTL32(30, b);
+ b = a;
+ a = T1;
+ j++;
+ } while (j < 16);
+
+ do {
+ T1 = W1[(j+13)&0x0f] ^ W1[(j+8)&0x0f] ^ W1[(j+2)&0x0f] ^ W1[j&0x0f];
+ if (j < 20) T2 = Ch(b,c,d) + K1_0_TO_19;
+ else if (j < 40) T2 = Parity(b,c,d) + K1_20_TO_39;
+ else if (j < 60) T2 = Maj(b,c,d) + K1_40_TO_59;
+ else T2 = Parity(b,c,d) + K1_60_TO_79;
+ T1 = ROTL32(5, a) + T2 + e + (W1[j&0x0f] = ROTL32(1, T1));
+ e = d;
+ d = c;
+ c = ROTL32(30, b);
+ b = a;
+ a = T1;
+ j++;
+ } while (j < 80);
+
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+}
+
+
+
+///* SHA-256: ********************************************************
+
+void sha2::SHA256_Internal_Transform(const sha_word32* data) {
+ sha_word32 a, b, c, d, e, f, g, h, s0, s1;
+ sha_word32 *state = (sha_word32*)ctx.state;
+ sha_word32 T1, T2, *W256=(sha_word32*)ctx.buffer;;
+ int j;
+
+// Initialize registers with the prev. intermediate value
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+ f = state[5];
+ g = state[6];
+ h = state[7];
+
+ j = 0;
+ do {
+ if (m_boolIsBigEndian) W256[j] = *data++;
+ else REVERSE32(*data++,W256[j]);// Copy data while converting to host byte order
+
+ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
+ T2 = Sigma0_256(a) + Maj(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + T1;
+ d = c;
+ c = b;
+ b = a;
+ a = T1 + T2;
+
+ j++;
+ } while (j < 16);
+
+ do {
+// Part of the message block expansion:
+ s0 = W256[(j+1)&0x0f];
+ s0 = sigma0_256(s0);
+ s1 = W256[(j+14)&0x0f];
+ s1 = sigma1_256(s1);
+
+// Apply the SHA-256 compression function to update a..h
+ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
+ (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
+ T2 = Sigma0_256(a) + Maj(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + T1;
+ d = c;
+ c = b;
+ b = a;
+ a = T1 + T2;
+
+ j++;
+ } while (j < 64);
+
+// Compute the current intermediate hash value
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+ state[5] += f;
+ state[6] += g;
+ state[7] += h;
+}
+
+//** SHA-512: ********************************************************
+
+void sha2::SHA512_Internal_Transform(const sha_word64* data) {
+ sha_word64 a, b, c, d, e, f, g, h, s0, s1;
+ sha_word64 *state = (sha_word64 *)ctx.state;
+ sha_word64 T1, T2, *W512 = (sha_word64*)ctx.buffer;
+ int j;
+
+// Initialize registers with the prev. intermediate value
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+ f = state[5];
+ g = state[6];
+ h = state[7];
+
+ j = 0;
+
+ do {
+
+ if (m_boolIsBigEndian){
+ W512[j] = *data;
+ data++;
+ }else{
+ REVERSE64(*data++, W512[j]);// copy and convert TO host byte order
+ }
+
+ T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
+ T2 = Sigma0_512(a) + Maj(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + T1;
+ d = c;
+ c = b;
+ b = a;
+ a = T1 + T2;
+
+ j++;
+ } while (j < 16);
+
+ do {
+// Part of the message block expansion:
+ s0 = W512[(j+1)&0x0f];
+ s0 = sigma0_512(s0);
+ s1 = W512[(j+14)&0x0f];
+ s1 = sigma1_512(s1);
+
+// Apply the SHA-512 compression function to update a..h
+ T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
+ (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
+ T2 = Sigma0_512(a) + Maj(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + T1;
+ d = c;
+ c = b;
+ b = a;
+ a = T1 + T2;
+
+ j++;
+ } while (j < 80);
+
+// Compute the current intermediate hash value
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+ state[5] += f;
+ state[6] += g;
+ state[7] += h;
+}
+
+
+void sha2::SHA256_Internal_Last(bool isSha1) {
+ sha_word32 usedspace;
+
+ usedspace = (sha_word32)(ctx.bitcount[0] >> 3) % 64;
+ if (usedspace == 0) {
+ MEMSET_BZERO(ctx.buffer, 56);
+ ctx.buffer[0] = 0x80;
+ }else {
+ ctx.buffer[usedspace++] = 0x80;
+ if (usedspace <= 56) {
+ MEMSET_BZERO(&ctx.buffer[usedspace], 56 - usedspace);
+ }else {
+ if (usedspace < 64) {
+ MEMSET_BZERO(&ctx.buffer[usedspace], 64 - usedspace);
+ }
+ if (isSha1) SHA1_Internal_Transform((sha_word32*)ctx.buffer);
+ else SHA256_Internal_Transform((sha_word32*)ctx.buffer);
+ MEMSET_BZERO(ctx.buffer, 56);
+ }
+ }
+
+ if (!m_boolIsBigEndian) REVERSE64(ctx.bitcount[0],ctx.bitcount[0]);
+ *(sha_word64*)&ctx.buffer[56] = ctx.bitcount[0];
+ if (isSha1) SHA1_Internal_Transform((sha_word32*)ctx.buffer);
+ else SHA256_Internal_Transform((sha_word32*)ctx.buffer);
+}
+
+
+void sha2::SHA512_Internal_Last() {
+ sha_word32 usedspace;
+
+ usedspace = (sha_word32)(ctx.bitcount[0] >> 3) % 128;
+ if (usedspace == 0) {
+ MEMSET_BZERO(ctx.buffer, 112);
+ ctx.buffer[0] = 0x80;
+ }else{
+ ctx.buffer[usedspace++] = 0x80;
+ if (usedspace <= 112) {
+ MEMSET_BZERO(&ctx.buffer[usedspace], 112 - usedspace);
+ }else {
+ if (usedspace < 128) {
+ MEMSET_BZERO(&ctx.buffer[usedspace], 128 - usedspace);
+ }
+ SHA512_Internal_Transform((sha_word64*)ctx.buffer);
+ MEMSET_BZERO(ctx.buffer, 112);
+ }
+ usedspace = 0;
+ }
+
+ if (!m_boolIsBigEndian){
+ REVERSE64(ctx.bitcount[0],ctx.bitcount[0]);
+ REVERSE64(ctx.bitcount[1],ctx.bitcount[1]);
+ }
+
+ *(sha_word64*)&ctx.buffer[112] = ctx.bitcount[1];
+ *(sha_word64*)&ctx.buffer[120] = ctx.bitcount[0];
+ SHA512_Internal_Transform((sha_word64*)ctx.buffer);
+}
+
+
+
+void sha2::SHA32bit_Update(const sha_byte *data, size_t len, bool isSha1) {
+ sha_word32 freespace, usedspace;
+
+ if (len<1){return;}// Calling with no data is valid - we do nothing
+
+ usedspace = (sha_word32)(ctx.bitcount[0] >> 3) % 64;
+ if (usedspace > 0) {// Calculate how much free space is available in the buffer
+ freespace = 64 - usedspace;
+ if (len >= freespace) {// Fill the buffer completely and process it
+ MEMCPY_BCOPY(&ctx.buffer[usedspace], data, freespace);
+ ctx.bitcount[0] += freespace << 3;
+ len -= freespace;
+ data += freespace;
+ if (isSha1) SHA1_Internal_Transform((sha_word32 *)ctx.buffer);
+ else SHA256_Internal_Transform((sha_word32 *)ctx.buffer);
+ }else {// The buffer is not yet full
+ MEMCPY_BCOPY(&ctx.buffer[usedspace], data, len);
+ ctx.bitcount[0] += len << 3;
+ return;
+ }
+ }
+ while (len >= 64) {// Process as many complete blocks as we can
+ if (isSha1) SHA1_Internal_Transform((sha_word32*)data);
+ else SHA256_Internal_Transform((sha_word32*)data);
+ ctx.bitcount[0] += 512;
+ len -= 64;
+ data += 64;
+ }
+ if (len > 0) {// There's left-overs, so save 'em
+ MEMCPY_BCOPY(&ctx.buffer, data, len);
+ ctx.bitcount[0] += len << 3;
+ }
+}
+
+
+
+void sha2::SHA64bit_Update(const sha_byte *data, size_t len) {
+ sha_word32 freespace, usedspace;
+
+ if (len < 1){return;}// Calling with no data is valid - we do nothing
+
+ usedspace = (sha_word32)(ctx.bitcount[0] >> 3) % 128;
+ if (usedspace > 0) {// Calculate how much free space is available in the buffer
+ freespace = 128 - usedspace;
+ if (len >= freespace) {// Fill the buffer completely and process it
+ MEMCPY_BCOPY(&ctx.buffer[usedspace], data, freespace);
+ ADDINC128(ctx.bitcount, freespace << 3);
+ len -= freespace;
+ data += freespace;
+ SHA512_Internal_Transform((sha_word64*)ctx.buffer);
+ }else {// The buffer is not yet full
+ MEMCPY_BCOPY(&ctx.buffer[usedspace], data, len);
+ ADDINC128(ctx.bitcount, len << 3);
+ return;
+ }
+ }
+ while (len >= 128) {// Process as many complete blocks as we can
+ SHA512_Internal_Transform((sha_word64*)data);
+ ADDINC128(ctx.bitcount, 1024);
+ len -= 128;
+ data += 128;
+ }
+ if (len > 0) {// There's left-overs, so save 'em
+ MEMCPY_BCOPY(ctx.buffer, data, len);
+ ADDINC128(ctx.bitcount, len << 3);
+ }
+}
+
+
+/*
+ *
+ *
+ *
+ * Public interfaces...
+ *
+ *
+ *
+ */
+
+void sha2::Init(SHA_TYPE type){
+ m_Type = type;
+ m_boolEnded = false;
+ MEMSET_BZERO(&ctx, sizeof(SHA_CTX));
+ switch (m_Type){
+ case enuSHA1 : MEMCPY_BCOPY(ctx.state, sha1_initial_hash_value, sizeof(sha_word32) * 5); break;
+ case enuSHA224 : MEMCPY_BCOPY(ctx.state, sha224_initial_hash_value, sizeof(sha_word32) * 8); break;
+ case enuSHA256 : MEMCPY_BCOPY(ctx.state, sha256_initial_hash_value, sizeof(sha_word32) * 8); break;
+ case enuSHA384 : MEMCPY_BCOPY(ctx.state, sha384_initial_hash_value, sizeof(sha_word64) * 8); break;
+ case enuSHA512 : MEMCPY_BCOPY(ctx.state, sha512_initial_hash_value, sizeof(sha_word64) * 8); break;
+ default : throw std::runtime_error("Invalid SHA_TYPE type!");
+ }
+}
+
+
+void sha2::Update(const sha_byte* data, size_t len){
+ switch (m_Type){
+ case enuSHA1 : SHA32bit_Update(data, len, true); break;
+ case enuSHA224 : SHA32bit_Update(data, len); break;
+ case enuSHA256 : SHA32bit_Update(data, len); break;
+ case enuSHA384 : SHA64bit_Update(data, len); break;
+ case enuSHA512 : SHA64bit_Update(data, len); break;
+ default : throw std::runtime_error("Invalid SHA_TYPE type!");
+ }
+}
+
+
+void sha2::End(){
+ sha_byte *d = m_digest;
+ char *buf = m_chrHexHash;
+ int i, j, diglen, statecnt=8;
+ bool is64bit=false;
+ sha_word32 *state32=(sha_word32 *)ctx.state;
+ sha_word64 *state64=(sha_word64 *)ctx.state;
+
+ switch (m_Type){
+ case enuSHA1 : {
+ SHA256_Internal_Last(true);
+ statecnt = 5;
+ diglen = SHA1_DIGESTC_LENGTH;
+ break;
+ }
+ case enuSHA224 : {
+ SHA256_Internal_Last();
+ diglen = SHA224_DIGESTC_LENGTH;
+ break;
+ }
+ case enuSHA256 : {
+ SHA256_Internal_Last();
+ diglen = SHA256_DIGESTC_LENGTH;
+ break;
+ }
+ case enuSHA384 : {
+ SHA512_Internal_Last();
+ is64bit = true;
+ diglen = SHA384_DIGESTC_LENGTH;
+ break;
+ }
+ case enuSHA512 : {
+ SHA512_Internal_Last();
+ is64bit = true;
+ diglen = SHA512_DIGESTC_LENGTH;
+ break;
+ }
+ default : throw std::runtime_error("Invalid SHA_TYPE type!");
+ }
+ if (m_boolIsBigEndian){
+ MEMCPY_BCOPY(&m_digest, &ctx.state, diglen);
+ }else{
+ sha_byte *dp = m_digest, *ptr;
+ for (i=0; i<statecnt; i++){
+ if (is64bit) ptr = (sha_byte *)&state64[i];
+ else ptr = (sha_byte *)&state32[i];
+ for (j = is64bit ? 7 : 3; j>-1; --j) *dp++ = ptr[j];
+ }
+ }
+
+ for (i=0; i<diglen; i++) {
+ *buf++ = sha_hex_digits[(*d & 0xf0) >> 4];
+ *buf++ = sha_hex_digits[*d & 0x0f];
+ d++;
+ }
+ *buf = (char)0;
+ m_strHash = m_chrHexHash;
+ m_boolEnded = true;
+}
+
+const string &sha2::GetHash(SHA_TYPE type, const sha_byte* data, size_t len){
+ Init(type);
+ Update(data, len);
+ End();
+ return m_strHash;
+}
+
+
+
+const char *sha2::HexHash(){
+ if (!m_boolEnded) throw std::runtime_error("Unfinished execution!");
+ return m_strHash.c_str();
+}
+const string &sha2::StringHash(){
+ if (!m_boolEnded) throw std::runtime_error("Unfinished execution!");
+ return m_strHash;
+}
+const char *sha2::RawHash(int &length){
+ if (!m_boolEnded) throw std::runtime_error("Unfinished execution!");
+ switch (m_Type){
+ case enuSHA1 : length = SHA1_DIGESTC_LENGTH; break;
+ case enuSHA224 : length = SHA224_DIGESTC_LENGTH; break;
+ case enuSHA256 : length = SHA256_DIGESTC_LENGTH; break;
+ case enuSHA384 : length = SHA384_DIGESTC_LENGTH; break;
+ case enuSHA512 : length = SHA512_DIGESTC_LENGTH; break;
+ default : length = 0;
+ }
+ return (const char *)m_digest;
+}
+
diff --git a/src/utils/sha2.h b/src/utils/sha2.h
new file mode 100644
index 00000000..3a0f07b5
--- /dev/null
+++ b/src/utils/sha2.h
@@ -0,0 +1,252 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Additional information about this file can be read below.
+ *
+ * $Id: $
+ */
+
+/*************************************************************
+
+ This program is a C++ implementation of the Secure Hash Algorithm (SHA)
+ that handles the variations from the original 160 bit to 224, 256, 384
+ and 512 bit. The program is intended to be platform independant and
+ has been tested on little-endian (Intel) and big-endian (Sun) machines.
+
+ This program is based on a C version written by Aaron D. Gifford
+ (as of 11/22/2004 his code could be found at http://www.adg.us/computers/sha.html).
+ Attempts to contact him were unsuccessful. I greatly condensed his version
+ and shared as much code and data as I could think of. I also inlined
+ a lot of code that were macros in his version. My version detects
+ endian-ness automatically and adjusts itself accordingly. This program
+ has been tested with Visual C++ versions 6/7 and Dev-C++ on Windows,
+ g++ on Linux and CC on Solaris (g++ on Solaris gave a bus error).
+
+ While I did make half-hearted attempts to optimize as I went along
+ (testing on Wintel), any serious attempt at fast implementation is
+ probably going to need to make use of in-lined assembly which is not
+ very portable.
+
+ The goal of this implementation is ease of use. As much as possible
+ I tried to hide implementation details while making it trivial to change
+ the size of the hash and get the results. The string and charactar
+ array value of the hash is supplied as human-readable hex; the raw value
+ can also be obtained.
+
+ If you use this implementation somewhere I would like to be credited
+ with my work (a link to my page below is fine). I add no license
+ restriction beyond any that is made by the original author. This
+ code comes with no warrenty expressed or implied, use at your own
+ risk!
+
+ Keith Oxenrider
+ koxenrider[at]sol[dash]biotech[dot]com
+ The latest version of this code should be available via the page
+ sol-biotech.com/code.
+
+*************************************************************/
+
+
+
+
+#ifndef _TMW_UTILS_SHA2C_H
+#define _TMW_UTILS_SHA2C_H
+
+#include <string>
+#include <stdexcept>
+
+// NOTE: You may need to define things by hand for your system:
+typedef unsigned char sha_byte; // Exactly 1 byte
+typedef unsigned int sha_word32; // Exactly 4 bytes
+#ifdef WIN32
+ #include <windows.h>
+ typedef ULONG64 sha_word64; // 8-bytes (64-bits)
+#else
+ typedef unsigned long long sha_word64; // 8-bytes (64-bits)
+#endif
+
+// Digest lengths for SHA-1/224/256/384/512
+const sha_word32 SHA1_DIGESTC_LENGTH = 20;
+const sha_word32 SHA1_DIGESTC_STRING_LENGTH = (SHA1_DIGESTC_LENGTH * 2 + 1);
+const sha_word32 SHA224_DIGESTC_LENGTH = 28;
+const sha_word32 SHA224_DIGESTC_STRING_LENGTH = (SHA224_DIGESTC_LENGTH * 2 + 1);
+const sha_word32 SHA256_DIGESTC_LENGTH = 32;
+const sha_word32 SHA256_DIGESTC_STRING_LENGTH = (SHA256_DIGESTC_LENGTH * 2 + 1);
+const sha_word32 SHA384_DIGESTC_LENGTH = 48;
+const sha_word32 SHA384_DIGESTC_STRING_LENGTH = (SHA384_DIGESTC_LENGTH * 2 + 1);
+const sha_word32 SHA512_DIGESTC_LENGTH = 64;
+const sha_word32 SHA512_DIGESTC_STRING_LENGTH = (SHA512_DIGESTC_LENGTH * 2 + 1);
+
+class sha2{
+public:
+ enum SHA_TYPE{
+ enuSHA_NONE,
+ enuSHA1,
+ enuSHA160 = enuSHA1,
+ enuSHA224,
+ enuSHA256,
+ enuSHA384,
+ enuSHA512,
+ enuSHA_LAST //for easier looping during testing
+ };
+
+ sha2(){
+ m_Type = enuSHA_NONE;
+ m_boolIsBigEndian = true;
+ m_boolEnded = false;
+
+ //run-time check for endian-ness
+ unsigned int test = 1;
+ unsigned char *ptr = (unsigned char *)&test;
+ if (ptr[0]) m_boolIsBigEndian = false;
+
+ //these checks here because I wasn't able to figure out how to
+ //check at compile time
+ if (sizeof(sha_byte) != 1) throw std::runtime_error("sha_byte != 1!");
+ if (sizeof(sha_word32) != 4) throw std::runtime_error("sha_word32 != 4!");
+ if (sizeof(sha_word64) != 8) throw std::runtime_error("sha_word64 != 8!");
+
+ memset(m_chrRawHash, 0, SHA512_DIGESTC_LENGTH);
+ memset(m_chrHexHash, 0, SHA512_DIGESTC_STRING_LENGTH);
+ memset(m_digest, 0, SHA512_DIGESTC_LENGTH);
+ };
+
+ SHA_TYPE GetEnumType(){return m_Type;};
+ bool IsBigEndian(){return m_boolIsBigEndian;};
+ const char * GetTypeString(){
+ switch (m_Type){
+ case sha2::enuSHA1 : return "SHA160";
+ case sha2::enuSHA224 : return "SHA224";
+ case sha2::enuSHA256 : return "SHA256";
+ case sha2::enuSHA384 : return "SHA384";
+ case sha2::enuSHA512 : return "SHA512";
+ default : return "Unknown!";
+ }
+ };
+
+//call these three in order if you want to load chunk-by-chunk...
+ void Init(SHA_TYPE type);
+ //these two throw a std::runtime_error if the type is not defined
+ void Update(const sha_byte *data, size_t len);//call as many times as needed
+ void End();
+
+//or call this one if you only have one chunk of data
+ const std::string &GetHash(SHA_TYPE type, const sha_byte* data, size_t len);
+
+//call one of these routines to access the hash
+ //these throw a std::runtime_error if End has not been called
+ const char *HexHash();//NULL terminated
+ const std::string &StringHash();
+ const char *RawHash(int &length);//NO NULL termination! size stored in 'length'
+
+
+private:
+ SHA_TYPE m_Type;
+ std::string m_strHash;
+ bool m_boolEnded, m_boolIsBigEndian;
+ char m_chrRawHash[SHA512_DIGESTC_LENGTH], m_chrHexHash[SHA512_DIGESTC_STRING_LENGTH];
+ sha_byte m_digest[SHA512_DIGESTC_LENGTH];
+
+//these are common buffers for maintaining the hash
+ struct SHA_CTX{
+ sha_byte state[sizeof(sha_word64) * 8];//maximum size
+ sha_word64 bitcount[2];//sha1, 224 and 256 only use the first entry
+ sha_byte buffer[128];
+ }ctx;
+
+
+//** INTERNAL FUNCTION PROTOTYPES ************************************
+ void SHA256_Internal_Last(bool isSha1 = false);
+ void SHA512_Internal_Last();
+
+ void SHA1_Internal_Transform(const sha_word32 *data);
+ void SHA256_Internal_Transform(const sha_word32* data);
+ void SHA512_Internal_Transform(const sha_word64*);
+
+ void SHA32bit_Update(const sha_byte *data, size_t len, bool isSha1=false);
+ void SHA64bit_Update(const sha_byte *data, size_t len);
+
+//macro replacements
+ inline void MEMSET_BZERO(void *p, size_t l){memset(p, 0, l);};
+ inline void MEMCPY_BCOPY(void *d,const void *s, size_t l) {memcpy(d, s, l);};
+
+ //For incrementally adding the unsigned 64-bit integer n to the
+ //unsigned 128-bit integer (represented using a two-element array of
+ //64-bit words):
+ inline void ADDINC128(sha_word64 *w, sha_word32 n) {
+ w[0] += (sha_word64)(n);
+ if (w[0] < (n)) w[1]++;
+ }
+
+ // Shift-right (used in SHA-256, SHA-384, and SHA-512):
+ inline sha_word32 SHR(sha_word32 b,sha_word32 x){return (x >> b);};
+ inline sha_word64 SHR(sha_word64 b,sha_word64 x){return (x >> b);};
+ // 32-bit Rotate-right (used in SHA-256):
+ inline sha_word32 ROTR32(sha_word32 b,sha_word32 x){return ((x >> b) | (x << (32 - b)));};
+ // 64-bit Rotate-right (used in SHA-384 and SHA-512):
+ inline sha_word64 ROTR64(sha_word64 b,sha_word64 x){return ((x >> b) | (x << (64 - b)));};
+ // 32-bit Rotate-left (used in SHA-1):
+ inline sha_word32 ROTL32(sha_word32 b,sha_word32 x){return ((x << b) | (x >> (32 - b)));};
+
+ // Two logical functions used in SHA-1, SHA-254, SHA-256, SHA-384, and SHA-512:
+ inline sha_word32 Ch(sha_word32 x,sha_word32 y,sha_word32 z){return ((x & y) ^ ((~x) & z));};
+ inline sha_word64 Ch(sha_word64 x,sha_word64 y,sha_word64 z){return ((x & y) ^ ((~x) & z));};
+ inline sha_word32 Maj(sha_word32 x,sha_word32 y,sha_word32 z){return ((x & y) ^ (x & z) ^ (y & z));};
+ inline sha_word64 Maj(sha_word64 x,sha_word64 y,sha_word64 z){return ((x & y) ^ (x & z) ^ (y & z));};
+
+ // Function used in SHA-1:
+ inline sha_word32 Parity(sha_word32 x,sha_word32 y,sha_word32 z){return (x ^ y ^ z);};
+
+// Four logical functions used in SHA-256:
+ inline sha_word32 Sigma0_256(sha_word32 x){return (ROTR32(2, x) ^ ROTR32(13, x) ^ ROTR32(22, x));};
+ inline sha_word32 Sigma1_256(sha_word32 x){return (ROTR32(6, x) ^ ROTR32(11, x) ^ ROTR32(25, x));};
+ inline sha_word32 sigma0_256(sha_word32 x){return (ROTR32(7, x) ^ ROTR32(18, x) ^ SHR( 3 , x));};
+ inline sha_word32 sigma1_256(sha_word32 x){return (ROTR32(17,x) ^ ROTR32(19, x) ^ SHR( 10, x));};
+
+// Four of six logical functions used in SHA-384 and SHA-512:
+ inline sha_word64 Sigma0_512(sha_word64 x){return (ROTR64(28, x) ^ ROTR64(34, x) ^ ROTR64(39, x));};
+ inline sha_word64 Sigma1_512(sha_word64 x){return (ROTR64(14, x) ^ ROTR64(18, x) ^ ROTR64(41, x));};
+ inline sha_word64 sigma0_512(sha_word64 x){return (ROTR64( 1, x) ^ ROTR64( 8, x) ^ SHR( 7, x));};
+ inline sha_word64 sigma1_512(sha_word64 x){return (ROTR64(19, x) ^ ROTR64(61, x) ^ SHR( 6, x));};
+
+ inline void REVERSE32(sha_word32 w, sha_word32 &x) {
+ w = (w >> 16) | (w << 16);
+ x = ((w & 0xff00ff00UL) >> 8) | ((w & 0x00ff00ffUL) << 8);
+ }
+ #ifdef _VC6
+ inline void REVERSE64(sha_word64 w, sha_word64 &x) {
+ w = (w >> 32) | (w << 32);
+ w = ((w & 0xff00ff00ff00ff00ui64) >> 8) |
+ ((w & 0x00ff00ff00ff00ffui64) << 8);
+ (x) = ((w & 0xffff0000ffff0000ui64) >> 16) |
+ ((w & 0x0000ffff0000ffffui64) << 16);
+ }
+ #else
+ inline void REVERSE64(sha_word64 w, sha_word64 &x) {
+ w = (w >> 32) | (w << 32);
+ w = ((w & 0xff00ff00ff00ff00ULL) >> 8) |
+ ((w & 0x00ff00ff00ff00ffULL) << 8);
+ (x) = ((w & 0xffff0000ffff0000ULL) >> 16) |
+ ((w & 0x0000ffff0000ffffULL) << 16);
+ }
+ #endif
+
+};//end class sha2
+#endif // __SHA2C_H__