PowerBASIC Forums
  Source Code
  TEA Inlined

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   TEA Inlined
Torsten Rienow
Member
posted March 16, 2002 09:32 AM     Click Here to See the Profile for Torsten Rienow     Edit/Delete Message   Reply w/Quote
If you want to know more about TEA then visit http://vader.brad.ac.uk/tea/tea.shtml

P.S.: do not forget to click on HOME at the end of Page above


'TEA.BAS
#Compile Exe
#Register None

Function encipherASM(ByVal v As Dword Ptr, ByVal w As Dword Ptr, ByVal k As Dword Ptr, ByVal nLoops As Long) As Dword

Dim y As Dword
Dim z As Dword

y = @v[0]
z = @v[1]

!MOV EDI, nLoops
!MOV EDX, k
!MOV EAX, z
!MOV ESI, 0

eLoop:
!MOV EBX, EAX
!SHL EAX, 4
!SHR EBX, 5
!XOR EAX, EBX
!ADD EAX, z
!MOV ECX, ESI
!AND ECX, 3
!MOV ECX, [EDX + ECX * 4]
!ADD ECX, ESI
!XOR EAX, ECX
!ADD EAX, y
!MOV y, EAX

!ADD ESI, &H9E3779B9???

!MOV EBX, EAX
!SHL EAX, 4
!SHR EBX, 5
!XOR EAX, EBX
!ADD EAX, y
!MOV ECX, ESI
!SHR ECX, 11
!AND ECX, 3
!MOV ECX, [EDX + ECX * 4]
!ADD ECX, ESI
!XOR EAX, ECX
!ADD EAX, z
!MOV z, EAX
!DEC EDI
!JNZ eLoop

@w[0] = y
@w[1] = z

End Function

Sub decipherASM(ByVal v As Dword Ptr, ByVal w As Dword Ptr, ByVal k As Dword Ptr, ByVal nLoops As Long)

Dim y As Dword
Dim z As Dword

y = @v[0]
z = @v[1]

!MOV EDI, nLoops
!MOV EAX, &H9E3779B9???
!MUL EDI
!MOV ESI, EAX
!MOV EDX, k
!MOV EBX, y

eLoop:
!MOV EAX, EBX
!SHL EAX, 4
!SHR EBX, 5
!XOR EAX, EBX
!ADD EAX, y
!MOV ECX, ESI
!SHR ECX, 11
!AND ECX, 3
!MOV ECX, [EDX + ECX * 4]
!ADD ECX, ESI
!XOR EAX, ECX
!MOV EBX, z
!SUB EBX, EAX
!MOV z, EBX

!SUB ESI, &H9E3779B9???

!MOV EAX, EBX
!SHL EAX, 4
!SHR EBX, 5
!XOR EAX, EBX
!ADD EAX, z
!MOV ECX, ESI
!AND ECX, 3
!MOV ECX, [EDX + ECX * 4]
!ADD ECX, ESI
!XOR EAX, ECX
!MOV EBX, y
!SUB EBX, EAX
!MOV y, EBX
!DEC EDI
!JNZ eLoop

@w[0] = y
@w[1] = z

End Sub

Function encipher(ByVal v As Dword Ptr, ByVal w As Dword Ptr, ByVal k As Dword Ptr, ByVal nLoops As Long) As Dword

Register y As Dword
Register z As Dword
Register sum As Dword
Register n As Dword

Dim ts1 As Dword
Dim ts2 As Dword
Dim ts3 As Dword

y = @v[0]
z = @v[1]
sum = 0
n = nLoops

While n > 0
ts1 = z
Shift Left ts1, 4
ts2 = z
Shift Right ts2, 5
y = y + ((ts1 Xor ts2) + z Xor sum + @k[sum And 3])
sum = sum + &H9E3779B9???
ts1 = y
Shift Left ts1, 4
ts2 = y
Shift Right ts2, 5
ts3 = sum
Shift Right ts3, 11
z = z + ((ts1 Xor ts2) + y Xor sum + @k[ts3 And 3])
n = n - 1
Wend

@w[0] = y
@w[1] = z

End Function

Sub decipher(ByVal v As Dword Ptr, ByVal w As Dword Ptr, ByVal k As Dword Ptr, ByVal nLoops As Long)

Register y As Dword
Register z As Dword
Register sum As Dword
Register n As Dword

Dim Delta As Dword
Dim ts1 As Dword
Dim ts2 As Dword
Dim ts3 As Dword

Delta = &H9E3779B9???
y = @v[0]
z = @v[1]

n = nLoops
sum = n * delta

While n > 0
ts1 = y
Shift Left ts1, 4
ts2 = y
Shift Right ts2, 5
ts3 = sum
Shift Right ts3, 11
z = z - ((ts1 Xor ts2) + y Xor sum + @k[ts3 And 3])
sum = sum - delta
ts1 = z
Shift Left ts1, 4
ts2 = z
Shift Right ts2, 5
y = y - ((ts1 Xor ts2) + z Xor sum + @k[sum And 3])
n = n - 1
Wend

@w[0] = y
@w[1] = z

End Sub


Function PbMain

Dim v As String * 8
Dim w As String * 8
Dim k As String * 16

k = Chr$(0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6)

'Test Power Basic Version
v = "TEA TEST"
Call encipher(ByVal VarPtr(v), ByVal VarPtr(w), ByVal VarPtr(k), 32)
v = w
Call decipher(ByVal VarPtr(v), ByVal VarPtr(w), ByVal VarPtr(k), 32)
MsgBox w , 0, "Powerbasic Language"

'Test Inline Asm Version
v = "TEA TEST"
Call encipherASM(ByVal VarPtr(v), ByVal VarPtr(w), ByVal VarPtr(k), 32)
v = w
Call decipherASM(ByVal VarPtr(v), ByVal VarPtr(w), ByVal VarPtr(k), 32)
MsgBox w , 0, "Inline Assembler"


End Function


------------------

IP: Logged

Peter Redei
Member
posted March 17, 2002 09:06 AM     Click Here to See the Profile for Peter Redei     Edit/Delete Message   Reply w/Quote
Torsten,
It seems to me both the assembly and the PB code handle only max. 8 characters at a time? So, how to use is for a longer text?

Regards,

Peter Redei

------------------

IP: Logged

Torsten Rienow
Member
posted March 21, 2002 06:24 AM     Click Here to See the Profile for Torsten Rienow     Edit/Delete Message   Reply w/Quote
Paul,

please go to
http://www.powerbasic.com/support/forums/Forum6/HTML/002371.html

i'answer there.

regards,

Torsten

------------------

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c