Browse Source

Remove all the Digest stuff from pnUtCrypt.

Darryl Pogue 13 years ago
parent
commit
39e8dd1b3c
  1. 55
      Sources/Plasma/NucleusLib/pnEncryption/plChallengeHash.cpp
  2. 2
      Sources/Plasma/NucleusLib/pnEncryption/plChallengeHash.h
  3. 128
      Sources/Plasma/NucleusLib/pnUtils/pnUtCrypt.cpp
  4. 35
      Sources/Plasma/NucleusLib/pnUtils/pnUtCrypt.h

55
Sources/Plasma/NucleusLib/pnEncryption/plChallengeHash.cpp

@ -45,11 +45,54 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
ShaDigest fSeed;
void CryptCreateRandomSeed(size_t length, uint8_t* data) {
void CryptCreateRandomSeed(size_t length, uint8_t* data)
{
uint32_t seedIdx = 0;
uint32_t dataIdx = 0;
uint32_t cur = 0;
uint32_t end = max(length, sizeof(ShaDigest));
// Combine seed with input data
for (; cur < end; cur++) {
fSeed[seedIdx] ^= data[dataIdx];
if (++seedIdx >= sizeof(ShaDigest))
seedIdx = 0;
if (++dataIdx >= length)
dataIdx = 0;
}
((uint32_t*)fSeed)[2] ^= (uint32_t)&length;
((uint32_t*)fSeed)[3] ^= (uint32_t)length;
((uint32_t*)fSeed)[4] ^= (uint32_t)data;
// Hash seed
plSHAChecksum sum(sizeof(ShaDigest), (uint8_t*)fSeed);
ShaDigest digest;
memcpy(digest, sum->GetValue(), sizeof(ShaDigest));
seedIdx = 0;
dataIdx = 0;
cur = 0;
// Update output with contents of digest
for (; cur < end; cur++) {
data[dataIdx] ^= digest[seedIdx];
if (++seedIdx >= sizeof(ShaDigest))
seedIdx = 0;
if (++dataIdx >= length)
dataIdx = 0;
}
// Combine seed with digest
for (size_t i = 0; i < sizeof(ShaDigest); i++) {
fSeed[i] ^= digest[i];
}
}
void CryptHashPassword(const plString& username, const plString& password, ShaDigest dest) {
void CryptHashPassword(const plString& username, const plString& password, ShaDigest dest)
{
/* This should be unnecessary once plString has ToLower() */
wchar_t* w_name = (wchar_t*)_TEMP_CONVERT_TO_WCHAR_T(username);
StrLower(w_name);
@ -62,7 +105,8 @@ void CryptHashPassword(const plString& username, const plString& password, ShaDi
memcpy(dest, sum.GetValue(), sizeof(ShaDigest));
}
void CryptHashPasswordChallenge(uint32_t clientChallenge, uint32_t serverChallenge, ShaDigest namePassHash, ShaDigest challengeHash) {
void CryptHashPasswordChallenge(uint32_t clientChallenge, uint32_t serverChallenge, ShaDigest namePassHash, ShaDigest challengeHash)
{
plSHAChecksum sum;
sum.Start();
@ -73,6 +117,3 @@ void CryptHashPasswordChallenge(uint32_t clientChallenge, uint32_t serverChallen
memcpy(challengeHash, sum.GetValue(), sizeof(ShaDigest));
}
void CryptCreateFastWeakChallenge(uint32_t* challenge, uint32_t val1, uint32_t val2) {
}

2
Sources/Plasma/NucleusLib/pnEncryption/plChallengeHash.h

@ -52,6 +52,4 @@ void CryptHashPassword(const plString& username, const plString& password, ShaDi
void CryptHashPasswordChallenge(uint32_t clientChallenge, uint32_t serverChallenge, ShaDigest namePassHash, ShaDigest challengeHash);
void CryptCreateFastWeakChallenge(uint32_t* challenge, uint32_t val1, uint32_t val2);
#endif //PL_CHALLENGE_HASH_H

128
Sources/Plasma/NucleusLib/pnUtils/pnUtCrypt.cpp

@ -49,7 +49,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "pnUtStr.h"
#include "pnUtTime.h"
#include <openssl/sha.h>
#include <openssl/rc4.h>
/*****************************************************************************
@ -72,8 +71,6 @@ struct CryptKey {
namespace Crypt {
ShaDigest s_shaSeed;
/*****************************************************************************
*
@ -81,26 +78,6 @@ ShaDigest s_shaSeed;
*
***/
//============================================================================
void ShaProcess (
void * dest,
unsigned sourceCount,
const unsigned sourceBytes[],
const void * sourcePtrs[]
) {
// initialize digest
SHA_CTX sha;
SHA_Init(&sha);
// hash data streams
for (unsigned index = 0; index < sourceCount; ++index)
SHA_Update(&sha, sourcePtrs[index], sourceBytes[index]);
// complete hashing
SHA_Final((unsigned char *)dest, &sha);
}
/*****************************************************************************
*
* RC4
@ -144,39 +121,6 @@ static void Rc4Codec (
*
***/
//============================================================================
void CryptDigest (
ECryptAlgorithm algorithm,
void * dest, // must be sized to the algorithm's digest size
const unsigned sourceBytes,
const void * sourceData
) {
CryptDigest(
algorithm,
dest,
1,
&sourceBytes,
&sourceData
);
}
//============================================================================
void CryptDigest (
ECryptAlgorithm algorithm,
void * dest, // must be sized to the algorithm's digest size
unsigned sourceCount,
const unsigned sourceBytes[], // [sourceCount]
const void * sourcePtrs[] // [sourceCount]
) {
switch (algorithm) {
case kCryptSha:
ShaProcess(dest, sourceCount, sourceBytes, sourcePtrs);
break;
DEFAULT_FATAL(algorithm);
}
}
//============================================================================
CryptKey * CryptKeyCreate (
ECryptAlgorithm algorithm,
@ -231,78 +175,6 @@ unsigned CryptKeyGetBlockSize (
}
}
//============================================================================
void CryptCreateRandomSeed (
unsigned bytes,
uint8_t * data
) {
COMPILER_ASSERT(SHA_DIGEST_LENGTH == 20);
// Combine seed with input data
{
unsigned seedIndex = 0;
unsigned dataIndex = 0;
unsigned cur = 0;
unsigned end = max(bytes, sizeof(s_shaSeed));
for (; cur < end; ++cur) {
s_shaSeed[seedIndex] ^= data[dataIndex];
if (++seedIndex >= sizeof(s_shaSeed))
seedIndex = 0;
if (++dataIndex >= bytes)
dataIndex = 0;
}
((uint32_t*)s_shaSeed)[2] ^= (uint32_t) &bytes;
((uint32_t*)s_shaSeed)[3] ^= (uint32_t) bytes;
((uint32_t*)s_shaSeed)[4] ^= (uint32_t) data;
}
// Hash seed
ShaDigest digest;
CryptDigest(kCryptSha, &digest, sizeof(s_shaSeed), s_shaSeed);
// Update output with contents of digest
{
unsigned src = 0;
unsigned dst = 0;
unsigned cur = 0;
unsigned end = max(bytes, sizeof(digest));
for (; cur < end; ++cur) {
data[dst] ^= ((const uint8_t *) &digest)[src];
if (++src >= sizeof(digest))
src = 0;
if (++dst >= bytes)
dst = 0;
}
}
// Combine seed with digest
for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
s_shaSeed[i] ^= digest[i];
}
}
//============================================================================
void CryptCreateFastWeakChallenge (
unsigned * challenge,
unsigned val1,
unsigned val2
) {
s_shaSeed.data[0] ^= TimeGetMs(); // looping time
s_shaSeed.data[0] ^= _rotl(s_shaSeed.data[0], 1);
s_shaSeed.data[0] ^= (unsigned) TimeGetTime(); // global time
s_shaSeed.data[0] ^= _rotl(s_shaSeed.data[0], 1);
s_shaSeed.data[0] ^= *challenge; // unknown
s_shaSeed.data[0] ^= _rotl(s_shaSeed.data[0], 1);
s_shaSeed.data[0] ^= (unsigned) challenge; // variable address
s_shaSeed.data[0] ^= _rotl(s_shaSeed.data[0], 1);
s_shaSeed.data[0] ^= val1;
s_shaSeed.data[0] ^= _rotl(s_shaSeed.data[0], 1);
s_shaSeed.data[0] ^= val2;
*challenge = s_shaSeed.data[0];
}
//============================================================================
void CryptEncrypt (
CryptKey * key,

35
Sources/Plasma/NucleusLib/pnUtils/pnUtCrypt.h

@ -61,35 +61,12 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
struct CryptKey;
enum ECryptAlgorithm {
kCryptSha,
kCryptRc4,
kCryptRsa,
kNumCryptAlgorithms
};
/*****************************************************************************
*
* Digest functions
*
***/
void CryptDigest (
ECryptAlgorithm algorithm,
void * dest, // must be sized to the algorithm's digest size
const unsigned sourceBytes,
const void * sourceData
);
void CryptDigest (
ECryptAlgorithm algorithm,
void * dest, // must be sized to the algorithm's digest size
unsigned sourceCount,
const unsigned sourceBytes[], // [sourceCount]
const void * sourcePtrs[] // [sourceCount]
);
/*****************************************************************************
*
* Key generation
@ -119,18 +96,6 @@ unsigned CryptKeyGetBlockSize (
CryptKey * key
);
void CryptCreateRandomSeed (
unsigned bytes,
uint8_t * data
);
void CryptCreateFastWeakChallenge (
unsigned * challenge,
unsigned val1,
unsigned val2
);
/*****************************************************************************
*
* Encryption and Decryption

Loading…
Cancel
Save