Page 1 of 1

Posted: Wed Jan 29, 2003 4:02 am
by BackupUser
Restored from previous forum. Originally posted by NightShade.


So, after a few graveyard shift nightmare runs where i bumped into a new PB bug, and found a few damn limitations, the thing is here. Runs ok in demo, but cant find NUM3 so he can compile it. Heck, might as well let u all have it, its no big thing anyway :) If you bump into a bug let me know. Without further ado, here is the code.




;
; Crypt/Decrypt procedures v0.1b. Remember kids, it DOES ALTER the ;incoming data, so keep a copy at hand if u need the original.
;


Procedure XSX_Crypt(*DatablockPointer,*Keyblockpointer)

;Encrypt Tier 1 - XOR datablock with keyblock
PokeL(*DatablockPointer,PeekL(*DatablockPointer+0)!PeekL(*Keyblockpointer+0))
PokeL(*DatablockPointer+4,PeekL(*DatablockPointer+4)!PeekL(*Keyblockpointer+4))

;Encrypt Tier 2 - Byteswap based on keyblock data

For counter.b=0 To 7
tvar.w=PeekB(*Keyblockpointer+counter.b)+127
tvar.w=tvar.w/32
Select tvar.w
Case 0
;Swap 1st And 8th byte
tmp.b=PeekB(*DatablockPointer+0)
PokeB(*DatablockPointer+0,PeekB(*DatablockPointer+7))
PokeB(*DatablockPointer+7,tmp.b)
Case 1
;Swap 2nd and 7th byte
tmp.b=PeekB(*DatablockPointer+1)
PokeB(*DatablockPointer+1,PeekB(*DatablockPointer+6))
PokeB(*DatablockPointer+6,tmp.b)
Case 2
;Swap 3rd and 6th byte
tmp.b=PeekB(*DatablockPointer+2)
PokeB(*DatablockPointer+2,PeekB(*DatablockPointer+5))
PokeB(*DatablockPointer+5,tmp.b)
Case 3
;Swap 4th and 5th byte
tmp.b=PeekB(*DatablockPointer+3)
PokeB(*DatablockPointer+3,PeekB(*DatablockPointer+4))
PokeB(*DatablockPointer+4,tmp.b)
Case 4
;Swap 1st and 2nd byte
tmp.b=PeekB(*DatablockPointer+0)
PokeB(*DatablockPointer+0,PeekB(*DatablockPointer+1))
PokeB(*DatablockPointer+1,tmp.b)
Case 5
;Swap 3rd and 4th byte
tmp.b=PeekB(*DatablockPointer+2)
PokeB(*DatablockPointer+2,PeekB(*DatablockPointer+3))
PokeB(*DatablockPointer+3,tmp.b)
Case 6
;Swap 5th and 6th byte
tmp.b=PeekB(*DatablockPointer+4)
PokeB(*DatablockPointer+4,PeekB(*DatablockPointer+5))
PokeB(*DatablockPointer+5,tmp.b)
Case 7
;Swap 7th and 8th byte
tmp.b=PeekB(*DatablockPointer+6)
PokeB(*DatablockPointer+6,PeekB(*DatablockPointer+7))
PokeB(*DatablockPointer+7,tmp.b)
EndSelect
Next

;Encrypt Tier 3 - XOR datablock with keyblock
PokeL(*DatablockPointer,PeekL(*DatablockPointer)!PeekL(*Keyblockpointer))
PokeL(*DatablockPointer+4,PeekL(*DatablockPointer+4)!PeekL(*Keyblockpointer+4))

EndProcedure

Procedure XSX_Decrypt(*DatablockPointer,*Keyblockpointer)

;Decrypt Tier 3 - XOR datablock with keyblock
PokeL(*DatablockPointer,PeekL(*DatablockPointer)!PeekL(*Keyblockpointer))
PokeL(*DatablockPointer+4,PeekL(*DatablockPointer+4)!PeekL(*Keyblockpointer+4))

;Decrypt Tier 2 - Byteswap based on keyblock data

For counter.b=7 To 0 Step -1
tvar.w=PeekB(*Keyblockpointer+counter.b)+127
tvar.w=tvar.w/32
Select tvar.w
Case 0
;Swap 1st And 8th byte
tmp.b=PeekB(*DatablockPointer+0)
PokeB(*DatablockPointer+0,PeekB(*DatablockPointer+7))
PokeB(*DatablockPointer+7,tmp.b)
Case 1
;Swap 2nd and 7th byte
tmp.b=PeekB(*DatablockPointer+1)
PokeB(*DatablockPointer+1,PeekB(*DatablockPointer+6))
PokeB(*DatablockPointer+6,tmp.b)
Case 2
;Swap 3rd and 6th byte
tmp.b=PeekB(*DatablockPointer+2)
PokeB(*DatablockPointer+2,PeekB(*DatablockPointer+5))
PokeB(*DatablockPointer+5,tmp.b)
Case 3
;Swap 4th and 5th byte
tmp.b=PeekB(*DatablockPointer+3)
PokeB(*DatablockPointer+3,PeekB(*DatablockPointer+4))
PokeB(*DatablockPointer+4,tmp.b)
Case 4
;Swap 1st and 2nd byte
tmp.b=PeekB(*DatablockPointer+0)
PokeB(*DatablockPointer+0,PeekB(*DatablockPointer+1))
PokeB(*DatablockPointer+1,tmp.b)
Case 5
;Swap 3rd And 4th byte
tmp.b=PeekB(*DatablockPointer+2)
PokeB(*DatablockPointer+2,PeekB(*DatablockPointer+3))
PokeB(*DatablockPointer+3,tmp.b)
Case 6
;Swap 5th And 6th byte
tmp.b=PeekB(*DatablockPointer+4)
PokeB(*DatablockPointer+4,PeekB(*DatablockPointer+5))
PokeB(*DatablockPointer+5,tmp.b)
Case 7
;Swap 7th and 8th byte
tmp.b=PeekB(*DatablockPointer+6)
PokeB(*DatablockPointer+6,PeekB(*DatablockPointer+7))
PokeB(*DatablockPointer+7,tmp.b)
EndSelect
Next

;Decrypt Tier 1 - XOR datablock with keyblock
PokeL(*DatablockPointer,PeekL(*DatablockPointer)!PeekL(*Keyblockpointer))
PokeL(*DatablockPointer+4,PeekL(*DatablockPointer+4)!PeekL(*Keyblockpointer+4))

EndProcedure

;
; Test Encryption for data integrity
;

Dim XSX_Datablock_Mirror.b(8)
Dim XSX_Datablock.b(8)
Dim XSX_Keyblock.b(8)

*DatablockPointer=@XSX_Datablock.b(0)
*Keyblockpointer=@XSX_Keyblock.b(0)

For testcount.l=0 To 9999

For c.l=0 To 7
XSX_Datablock.b(c.l)=Random(255)
XSX_Datablock_Mirror.b(c.l)=XSX_Datablock.b(c.l)
XSX_Keyblock.b(c.l)=Random(255)
Next

XSX_Crypt(*DatablockPointer,*Keyblockpointer)
XSX_Decrypt(*DatablockPointer,*Keyblockpointer)

For c.l=0 To 7
If XSX_Datablock_Mirror.b(c.l)XSX_Datablock.b(c.l)
result=MessageRequester("WARNING!!!","Encryption failed", #PB_MessageRequester_Ok )
EndIf
Next

Next

;
; Benchmark the algo
;

Dim XSX_BigDatablock.b(4*1024*1024)
Dim XSX_BigDatablock_Mirror.b(4*1024*1024)
Dim XSX_Keyblock.b(8)
*Keyblockpointer=@XSX_Keyblock.b(0)


*BigDatablockPointer=@XSX_BigDatablock.b(0)

;
; Setup Datablock
;

For c.l=0 To (4*1024*1024)-1
XSX_BigDatablock.b(c.l)=Random(255)
XSX_BigDatablock_Mirror.b(c.l)=XSX_BigDatablock.b(c.l)
Next

;
; Setup KeyBlock
;

For c.l=0 To 7
XSX_Keyblock.b(c.l)=Random(255)
Next

time1=Date()
;
; Crypt file
;
For c.l=0 To (4*1024*1024)-1 Step 8
*BigDatablockPointer=@XSX_BigDatablock.b(c.l)
XSX_Crypt(*BigDatablockPointer,*Keyblockpointer)
Next
time2=Date()
For c.l=0 To (4*1024*1024)-1 Step 8
*BigDatablockPointer=@XSX_BigDatablock.b(c.l)
XSX_Decrypt(*BigDatablockPointer,*Keyblockpointer)
Next
time3.l=Date()

crypttime.l=time2.l-time1.l
decrypttime.l=time3.l-time2.l

result=MessageRequester("Benchmark","Crypttime: "+Str(crypttime)+"seconds at "+Str((4*1024*1024)/crypttime)+" bytes per second.", #PB_MessageRequester_Ok )
Delay(50)
result=MessageRequester("Benchmark","Decrypttime: "+Str(decrypttime)+"seconds at "+Str((4*1024*1024)/decrypttime)+" bytes per second.", #PB_MessageRequester_Ok )





p.s. I know code is a bit sloppy, could be made more compact, more organized, even maybe faster, but hey, its 0.1b u know? 0.2b will have tier 2 grow, but only after im sure this is working.

p.s.s Thx for the new demo Fred. Date() came just in time. But it would be REALLY nice to have some low-level timer. Seconds are just WAAAAY tooo big :)

Peace 2u all.

Posted: Wed Jan 29, 2003 5:19 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.

> But it would be REALLY nice to have some low-level timer.

Dunno if this works in the Demo, but you could give it a try:
StartTimer(1,ms,@procedure())
Opens Timer 1 and calls the procedure every ms millisonds.

cya,
...Danilo
(registered PureBasic user)

Posted: Wed Jan 29, 2003 6:20 am
by BackupUser
Restored from previous forum. Originally posted by NightShade.

StartTimer() is not in docs..... Besides, it looks like somekind of OnTimer(callproc). What i need is to get accurate time :) Like in CPU ticks or something similar.

Posted: Wed Jan 29, 2003 6:35 am
by BackupUser
Restored from previous forum. Originally posted by TheBeck.

GetTickCount()

MS since boot

Posted: Thu Jan 30, 2003 12:51 am
by BackupUser
Restored from previous forum. Originally posted by NightShade.

thx for the tip Beck. Woudl like it more accurate, but i can live with milisecs :) Tests show crypt is fast enough. After getting it compiled by Num3, it also showed that Debugger is like brake to teh metal. 7.5x slowdown over exe.
Ah well, all is fine, even if debugger speed was final speed. Unless u can upstream at 20Mbits/s :) Too bad i cant delay for less than 1ms.... w/o delay it grabs all the cpu it can, and with delay(1) it grind to an halt.

p.s. Come to think of it, it will only encrypt/decrypt packets as they get sent/received, so, it will spike the CPU load, but wont hog it.