Server Help

Bot Questions - subspace encryption

emileej - Fri Aug 20, 2004 3:59 pm
Post subject: subspace encryption
Hi! I'm working on a new core and since I am not very experienced when it comes to encryption I decided to ask Snrrrub if I could use his encryption routines. He said "yeah sure" and I implemented them. However I think I might have added a bug and I was wonderin if anyone in here could find it since I (as mentioned before) am not very good in encryption.
Code: Show/Hide
//Conversion routine
void CSSConnection::WriteUint32to8(Uint32 data,Uint8 *target,int len){
   for(Uint32 cnt=0;cnt<sizeof(Uint32) && cnt<len;cnt++)
       target[cnt]=(Uint8)((data >> (cnt * 8)) & 0xFF);
}

//Encryption routines - by Snrrrup
void CSSConnection::InitEncryption(Uint32 key){
   Log("InitEncryption");

Sint32 temp = 0;
encCypherKey = key;

for(int cnt = 0; cnt < 520; cnt += 2) // Each "block" is 2 bytes and the keystream size is 520 bytes
{
temp = (Uint32)((Uint64)((Uint64)key * (Uint64)0x834E0B5F) >> 48);
temp += (temp >> 31);
key = ((key % 0x1F31D) * 16807) - (temp * 2836) + 123;
if((Sint32)key < 0)
  key += 0x7FFFFFFF;
*((Uint16 *)(encKeyStream + cnt)) = (Uint16)key;
}

encryptionEnabled=true;
}

void CSSConnection::Encrypt(char *data,int len){
   Log("Encrypt");
if(!encryptionEnabled)
return;

int StartPos = 1;
Uint32 Encrypted = encCypherKey;

if(!data[0]) //If Byte at Offset 0 is 0x00
StartPos++;

for(Uint32 Count = StartPos; Count < len; Count += 4)
{
Encrypted ^= *(Uint32 *)(encKeyStream + (Count - StartPos)) ^ (Uint32)(data+Count);
WriteUint32to8(Encrypted,(Uint8*)(data+Count),len-Count);
}
}

void CSSConnection::Decrypt(char *data,int len){
   Log("Decrypt");
if(!encryptionEnabled)
return;

Uint32 Decrypted = encCypherKey;
int StartPos = 1;

if(!data[0]) //If Byte at Offset 0 is 0x00
StartPos++;

for(Uint32 Count = StartPos; Count < len; Count += 4)
{
Uint32 Encrypted = (Uint32)(data+Count);
Decrypted ^= *(Uint32 *)(encKeyStream + (Count - StartPos)) ^ Encrypted;
WriteUint32to8(Decrypted,(Uint8*)(data+Count),len-Count);
Decrypted = Encrypted;
}
}

emileej - Fri Aug 20, 2004 7:21 pm
Post subject: argh!
Ok I'm this close to toss that code ouy the window... I'm really stuck here! Please help - whatever you have of hints, ideas or questions will be helpfull.
myke - Fri Aug 20, 2004 7:58 pm
Post subject:
you haven't described your problem so people really can't help you too much...is it a problem compiling? a problem with the encryption not encrypting or decrypting?
emileej - Fri Aug 20, 2004 8:55 pm
Post subject:
myke wrote:
you haven't described your problem so people really can't help you too much...is it a problem compiling? a problem with the encryption not encrypting or decrypting?
It compiles fine. I know that it is not decrypting properly since for instance my incomming reliable packets have insanely large IDs. I suspect it also doesnt encrypt ptoperly since I, when I send a reliable packet, get a reliable reply with a wrong id and no response on the content of the reliable packet.
ExplodyThingy - Fri Aug 20, 2004 9:21 pm
Post subject:
Why not just gank them stright from logic bot? I believe he uses Snrrrubs work there for the packet wrappers encryptions, checksums, and settings structs.
Mr Ekted - Fri Aug 20, 2004 9:54 pm
Post subject:
One of the requirements for coding is being able to debug. If you compile and run stuff and it doesn't work, you can't simply just throw it out to the public and say "it's broken, fix it". Take it step by step, narrow down where the problem is. For example, capture an encryption key and a packet from some other source, then see if your code results in the same encrypted packet, etc.
emileej - Sat Aug 21, 2004 3:15 am
Post subject:
splody << I am - I asked Snrrrub for permission, got it and integrated his routines.

Mr. Ekted << I guess I deserved that one, but the point of me posting the source here in stead is that since it is coded by Snrrrub originaly then I though that this community was very familiar with it and could easier see any major screwups than I.

But as Ekted logicly suggests - are there anyone out there who could provide me with a log displaying client encryption key, server encryption key and all packets in a session encrypted aswell as decrypted?
Mr Ekted - Sat Aug 21, 2004 4:21 am
Post subject:
There is only 1 key, set by the client. The server just compliments it, and sends it back.
emileej - Sat Aug 21, 2004 4:52 am
Post subject:
kk. I stand corrected.
Cyan~Fire - Sat Aug 21, 2004 10:06 am
Post subject:
Seeing as you've just ripped this from Snrrrubspace, I'm guessing you don't really know how it works. I'd suggest analyzing the code according to the known encryption, and then try to think your way through to where it's going wrong.

Also, you didn't give us the class CSSConnection declaration. That would be nice, too.
emileej - Sat Aug 21, 2004 10:57 am
Post subject:
I read the source in LogicBot and thought I'd save some time if Snrrrub would let me use it. Unfortunatly I introduced some bug which noone seems to be able to help me with.
Too bad that I wasted time on this one, but since nobody can help I guess Ill just have have to reinvent the wheel.
These are the encryption related declareations from CSSConnection:
Code: Show/Hide
            //Encryption
             Uint32 encClientKey,encCypherKey;
             Uint8 encKeyStream[520];
             bool encryptionEnabled;

            //Encryption routines - by Snrrrup
            void InitEncryption(Uint32 key);//FIXME
            void Encrypt(char *data,int len);//FIXME
            void Decrypt(char *data,int len);//FIXME

Mr Ekted - Sat Aug 21, 2004 2:56 pm
Post subject:
We can't help without a larger context. You are probably using the functions incorrectly. We need to see all your code. Obviously Snrrrub's code works.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group