2024-11-21

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

TEA

Algorithm creator(s)

David Wheeler, Roger Needham


PB author(s)

Torsten Rienow, Michael Ritter


Description

A Feistel cipher which uses operations from mixed (orthogonal) algebraic groups, encrypting 64 bits at a time using a 128-bit key. Although not considered secure, it is one of the fastest and most efficient cryptographic algorithms in existence.


Note

The code below is Torsten Rienow's PB and PB-ASM ports.


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23946-tea-inlined?t=23321


See also


Source Code

Download source code file tea.bas (Right-click -> "Save as ...")

'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

Mirror provided by Knuth Konrad