Search code examples
assemblyencryptioninputx86-16tasm

Assembly 8086 TASM - TEA Algorithm - How to split 8-byte variable into two different variables of 4 bytes?


I want to write the TEA algorithm in assembly 8086 (TASM), and I am stuck in the first steps of splitting the plaintext (block) variable into 2 variables and the key into 4 variables (k1, k2, k3, k4).
I know the limit of the plaintext (block) is 64 bits (8 bytes)
and the limit of the key is 128 bits (16 bytes).

The plaintext variable is stored in the data section:

plainText db 8 dup('$')

and these are the two variables of the plaintext:

p1 db 4 dup('$')
p2 db 4 dup('$')

The key variable is stored like this:

key db 16 dup("$")

and the 4 split keys:

k0 db ?
k1 db ?
k2 db ?
k3 db ?

This is my current encryption section for taking the input from the user:

proc encryptionSelection
    print plainTextPrompt
    inputString [plainText] ; Block Limit: 64 bits -> 8 bytes

    call printLine
    print p1
    call printLine
    print p2

    print keyPrompt
    inputString [key] ; Key Limit:  128 bits -> 16 bytes

    ; Encryption Logic Here...
    ret
endp encryptionSelection
macro inputString storage
    pusha
    mov ah, 0ah
    lea dx, storage
    int 21h
    popa
endm inputString
macro print value
    pusha
    mov ah, 09h
    mov dx, offset value
    int 21h
    popa

endm print

I tried many ways and googled a lot, but I couldn't find any solutions.


Solution

  • and the 4 split keys:

    k0 db ?  
    k1 db ?  
    k2 db ?  
    k3 db ?
    

    The key has 16 bytes, so split in 4, I would expect these to be dwords rather than bytes!

    plainText db 8 dup('$')
    p1 db 4 dup('$')
    p2 db 4 dup('$')
    

    Your inputString macro is using the DOS.BufferedInput function 0Ah, and you are not supplying the correct buffer to DOS! See How buffered input works.
    The correct definition for plainText would be:

    plainText db 9, 0, 9 dup('$')
    p1 db 4 dup('$')
    p2 db 4 dup('$')
    

    One way to split the inputted 8 bytes is to copy the following way:

    print plainTextPrompt
    inputString [plainText] ; Block Limit: 64 bits -> 8 bytes
    mov  di, offset p1
    mov  si, offset plainText+2
    mov  cx, 8
    rep movsb
    

    But the most efficient way would be to reorganize the buffers:

    plainText db 9, 0
    p1        dd 0      ;;
    p2        dd 0      ; Together 9 bytes of inputbuffer
              db 0      ;;
              db 0      ; 1 extra byte for word-alignment
    key       db 17, 0
    k0        dd 0      ;;
    k1        dd 0      ;;;
    k3        dd 0      ; Together 17 bytes of inputbuffer
    k4        dd 0      ;;;
              db 0      ;;
              db 0      ; 1 extra byte for word-alignment
    

    Then no copying is required and you can easily access your separate variables.