Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

TEA Inlined

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • TEA Inlined

    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
    Code:
    '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

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

  • #2
    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

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

    Comment


    • #3
      paul,

      please go to
      http://www.powerbasic.com/support/pb...ad.php?t=20244

      i'answer there.

      regards,

      torsten

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

      Comment

      Working...
      X