summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/CMakeLists.txt6
-rw-r--r--src/Makefile.am6
-rw-r--r--src/net/accountserver/account.cpp14
-rw-r--r--src/net/accountserver/accountserver.cpp7
-rw-r--r--src/utils/encryption.cpp75
-rw-r--r--src/utils/sha2.cpp675
-rw-r--r--src/utils/sha2.h224
-rw-r--r--src/utils/sha256.cpp276
-rw-r--r--src/utils/sha256.h (renamed from src/utils/encryption.h)82
9 files changed, 326 insertions, 1039 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9ddd64d0..2fb3b458 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -301,13 +301,11 @@ SET(SRCS
utils/base64.cpp
utils/base64.h
utils/dtor.h
- utils/encryption.h
- utils/encryption.cpp
utils/fastsqrt.h
utils/gettext.h
utils/minmax.h
- utils/sha2.h
- utils/sha2.cpp
+ utils/sha256.h
+ utils/sha256.cpp
utils/strprintf.cpp
utils/strprintf.h
utils/tostring.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 046a518b..9d739685 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -255,13 +255,11 @@ tmw_SOURCES = gui/widgets/dropdown.cpp \
utils/base64.cpp \
utils/base64.h \
utils/dtor.h \
- utils/encryption.h \
- utils/encryption.cpp \
utils/fastsqrt.h \
utils/gettext.h \
utils/minmax.h \
- utils/sha2.h \
- utils/sha2.cpp \
+ utils/sha256.h \
+ utils/sha256.cpp \
utils/strprintf.h \
utils/strprintf.cpp \
utils/tostring.h \
diff --git a/src/net/accountserver/account.cpp b/src/net/accountserver/account.cpp
index bb0214d3..2d8065b5 100644
--- a/src/net/accountserver/account.cpp
+++ b/src/net/accountserver/account.cpp
@@ -24,7 +24,6 @@
#include "account.h"
#include <string>
-#include "../../utils/encryption.h"
#include "internal.h"
@@ -32,6 +31,8 @@
#include "../messageout.h"
#include "../protocol.h"
+#include "../../utils/sha256.h"
+
void Net::AccountServer::Account::createCharacter(
const std::string &name, char hairStyle, char hairColor, char gender,
short strength, short agility, short vitality,
@@ -77,14 +78,13 @@ void Net::AccountServer::Account::unregister(const std::string &username,
MessageOut msg(PAMSG_UNREGISTER);
msg.writeString(username);
- msg.writeString(Encryption::GetSHA2Hash(
- std::string (username + password)));
+ msg.writeString(sha256(username + password));
Net::AccountServer::connection->send(msg);
}
void Net::AccountServer::Account::changeEmail(const std::string &username,
- const std::string &email)
+ const std::string &email)
{
MessageOut msg(PAMSG_EMAIL_CHANGE);
@@ -103,10 +103,8 @@ void Net::AccountServer::Account::changePassword(
MessageOut msg(PAMSG_PASSWORD_CHANGE);
// Change password using SHA2 encryption
- msg.writeString(Encryption::GetSHA2Hash(
- std::string (username + oldPassword)));
- msg.writeString(Encryption::GetSHA2Hash(
- std::string (username + newPassword)));
+ msg.writeString(sha256(username + oldPassword));
+ msg.writeString(sha256(username + newPassword));
Net::AccountServer::connection->send(msg);
}
diff --git a/src/net/accountserver/accountserver.cpp b/src/net/accountserver/accountserver.cpp
index a641ab47..db94563b 100644
--- a/src/net/accountserver/accountserver.cpp
+++ b/src/net/accountserver/accountserver.cpp
@@ -24,7 +24,6 @@
#include "accountserver.h"
#include <string>
-#include "../../utils/encryption.h"
#include "internal.h"
@@ -32,6 +31,8 @@
#include "../messageout.h"
#include "../protocol.h"
+#include "../../utils/sha256.h"
+
void Net::AccountServer::login(Net::Connection *connection, int version,
const std::string &username, const std::string &password)
{
@@ -41,9 +42,7 @@ void Net::AccountServer::login(Net::Connection *connection, int version,
msg.writeInt32(version);
msg.writeString(username);
- // The password is hashed
- msg.writeString(Encryption::GetSHA2Hash(
- std::string (username + password)));
+ msg.writeString(sha256(username + password));
Net::AccountServer::connection->send(msg);
}
diff --git a/src/utils/encryption.cpp b/src/utils/encryption.cpp
deleted file mode 100644
index e3483e07..00000000
--- a/src/utils/encryption.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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/sha2.cpp b/src/utils/sha2.cpp
deleted file mode 100644
index b2bf9c1e..00000000
--- a/src/utils/sha2.cpp
+++ /dev/null
@@ -1,675 +0,0 @@
-/*************************************************************
-
- 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
deleted file mode 100644
index 79f6b862..00000000
--- a/src/utils/sha2.h
+++ /dev/null
@@ -1,224 +0,0 @@
-/*************************************************************
-
- 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__
diff --git a/src/utils/sha256.cpp b/src/utils/sha256.cpp
new file mode 100644
index 00000000..0cedcc89
--- /dev/null
+++ b/src/utils/sha256.cpp
@@ -0,0 +1,276 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file has been slighly modified as 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$
+ */
+
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+/* m_sha256 - Based on m_opersha256 written by Special <john@yarbbles.com>
+ * Modified and improved by Craig Edwards, December 2006.
+ *
+ *
+ * FIPS 180-2 SHA-224/256/384/512 implementation
+ * Last update: 05/23/2005
+ * Issue date: 04/30/2005
+ *
+ * Copyright (C) 2005 Olivier Gay <olivier.gay@a3.epfl.ch>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "sha256.h"
+
+#ifdef HAS_STDINT
+#include <stdint.h>
+#endif
+#ifndef HAS_STDINT
+typedef unsigned int uint32_t;
+#endif
+
+#define SHA256_BLOCK_SIZE (512 / 8)
+
+/** An sha 256 context, used by original m_opersha256 */
+class SHA256Context
+{
+ public:
+ unsigned int tot_len;
+ unsigned int len;
+ unsigned char block[2 * SHA256_BLOCK_SIZE];
+ uint32_t h[8];
+};
+
+#define SHA256_DIGEST_SIZE (256 / 8)
+
+#define SHFR(x, n) (x >> n)
+#define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
+#define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
+#define CH(x, y, z) ((x & y) ^ (~x & z))
+#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
+
+#define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
+#define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
+#define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3))
+#define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10))
+
+#define UNPACK32(x, str) \
+{ \
+ *((str) + 3) = (uint8_t) ((x) ); \
+ *((str) + 2) = (uint8_t) ((x) >> 8); \
+ *((str) + 1) = (uint8_t) ((x) >> 16); \
+ *((str) + 0) = (uint8_t) ((x) >> 24); \
+}
+
+#define PACK32(str, x) \
+{ \
+ *(x) = ((uint32_t) *((str) + 3) ) \
+ | ((uint32_t) *((str) + 2) << 8) \
+ | ((uint32_t) *((str) + 1) << 16) \
+ | ((uint32_t) *((str) + 0) << 24); \
+}
+
+/* Macros used for loops unrolling */
+
+#define SHA256_SCR(i) \
+{ \
+ w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \
+ + SHA256_F3(w[i - 15]) + w[i - 16]; \
+}
+
+const unsigned int sha256_h0[8] =
+{
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
+};
+
+uint32_t sha256_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
+};
+
+void SHA256Init(SHA256Context *ctx)
+{
+ for (int i = 0; i < 8; i++)
+ ctx->h[i] = sha256_h0[i];
+ ctx->len = 0;
+ ctx->tot_len = 0;
+}
+
+void SHA256Transform(SHA256Context *ctx,
+ unsigned char *message,
+ unsigned int block_nb)
+{
+ uint32_t w[64];
+ uint32_t wv[8];
+ unsigned char *sub_block;
+ for (unsigned int i = 1; i <= block_nb; i++)
+ {
+ int j;
+ sub_block = message + ((i - 1) << 6);
+
+ for (j = 0; j < 16; j++)
+ PACK32(&sub_block[j << 2], &w[j]);
+ for (j = 16; j < 64; j++)
+ SHA256_SCR(j);
+ for (j = 0; j < 8; j++)
+ wv[j] = ctx->h[j];
+ for (j = 0; j < 64; j++)
+ {
+ uint32_t t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j];
+ uint32_t t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
+ wv[7] = wv[6];
+ wv[6] = wv[5];
+ wv[5] = wv[4];
+ wv[4] = wv[3] + t1;
+ wv[3] = wv[2];
+ wv[2] = wv[1];
+ wv[1] = wv[0];
+ wv[0] = t1 + t2;
+ }
+ for (j = 0; j < 8; j++)
+ ctx->h[j] += wv[j];
+ }
+}
+
+void SHA256Update(SHA256Context *ctx,
+ unsigned char *message,
+ unsigned int len)
+{
+ /*
+ * XXX here be dragons!
+ * After many hours of pouring over this, I think I've found the problem.
+ * When Special created our module from the reference one, he used:
+ *
+ * unsigned int rem_len = SHA256_BLOCK_SIZE - ctx->len;
+ *
+ * instead of the reference's version of:
+ *
+ * unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len;
+ * unsigned int rem_len = len < tmp_len ? len : tmp_len;
+ *
+ * I've changed back to the reference version of this code, and it seems to work with no errors.
+ * So I'm inclined to believe this was the problem..
+ * -- w00t (January 06, 2008)
+ */
+ unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len;
+ unsigned int rem_len = len < tmp_len ? len : tmp_len;
+
+ memcpy(&ctx->block[ctx->len], message, rem_len);
+ if (ctx->len + len < SHA256_BLOCK_SIZE)
+ {
+ ctx->len += len;
+ return;
+ }
+ unsigned int new_len = len - rem_len;
+ unsigned int block_nb = new_len / SHA256_BLOCK_SIZE;
+ unsigned char *shifted_message = message + rem_len;
+ SHA256Transform(ctx, ctx->block, 1);
+ SHA256Transform(ctx, shifted_message, block_nb);
+ rem_len = new_len % SHA256_BLOCK_SIZE;
+ memcpy(ctx->block, &shifted_message[block_nb << 6],rem_len);
+ ctx->len = rem_len;
+ ctx->tot_len += (block_nb + 1) << 6;
+}
+
+void SHA256Final(SHA256Context *ctx, unsigned char *digest)
+{
+ unsigned int block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) < (ctx->len % SHA256_BLOCK_SIZE)));
+ unsigned int len_b = (ctx->tot_len + ctx->len) << 3;
+ unsigned int pm_len = block_nb << 6;
+ memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
+ ctx->block[ctx->len] = 0x80;
+ UNPACK32(len_b, ctx->block + pm_len - 4);
+ SHA256Transform(ctx, ctx->block, block_nb);
+ for (int i = 0 ; i < 8; i++)
+ UNPACK32(ctx->h[i], &digest[i << 2]);
+}
+
+std::string SHA256Hash(const char *src, int len)
+{
+ // Generate the hash
+ unsigned char bytehash[SHA256_DIGEST_SIZE];
+ SHA256Context ctx;
+ SHA256Init(&ctx);
+ SHA256Update(&ctx, (unsigned char *)src, (unsigned int)len);
+ SHA256Final(&ctx, bytehash);
+ // Convert it to hex
+ const char* hxc = "0123456789abcdef";
+ std::string hash = "";
+ for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
+ {
+ hash += hxc[bytehash[i] / 16];
+ hash += hxc[bytehash[i] % 16];
+ }
+ return hash;
+}
+
+std::string sha256(const std::string& string)
+{
+ return SHA256Hash(string.c_str(), string.length());
+}
diff --git a/src/utils/encryption.h b/src/utils/sha256.h
index 79d69dc4..c03efc0c 100644
--- a/src/utils/encryption.h
+++ b/src/utils/sha256.h
@@ -1,45 +1,37 @@
-/*
- * 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 {
-
-const sha2::SHA_TYPE shaType = sha2::enuSHA256; /**< Set the encryption strength */
-
-/** 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
-
+/*
+ * The Mana World
+ * Copyright 2007 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_SHA256_H_
+#define _TMW_UTILS_SHA256_H_
+
+#include <string>
+
+/**
+ * Returns the SHA-256 hash for the given string.
+ *
+ * @param string the string to create the SHA-256 hash for
+ * @return the SHA-256 hash for the given string.
+ */
+std::string sha256(const std::string& string);
+
+#endif // _TMW_UTILS_SHA256_H_