format ELF

public _start

extrn AES_Init
extrn AES_EncryptKeys
extrn AES_DecryptKeys
extrn AES_EncryptBlock
extrn AES_DecryptBlock
extrn AES_SecureMemory

section '.text' executable

_start:
    pusha

	call AES_Init

    push KeyFipsMain
    call AES_EncryptKeys

	mov eax, 4
	mov ebx, 1
	mov ecx, OrigDataMsg
	mov edx, OrigDataMsg.len
	int 0x80

	mov ebx, FipsEncData
	call printdata

    push Outa
    push FipsEncData
    call AES_EncryptBlock

	mov eax, 4
	mov ebx, 1
	mov ecx, EncDataMsg
	mov edx, EncDataMsg.len
	int 0x80

	mov ebx, Outa
	call printdata

    push KeyFipsMain
    call AES_DecryptKeys

    push Outb
    push Outa
    call AES_DecryptBlock

	mov eax, 4
	mov ebx, 1
	mov ecx, DecDataMsg
	mov edx, DecDataMsg.len
	int 0x80

	mov ebx, Outb
	call printdata

    call AES_SecureMemory

	; print new line
	mov eax, 4
	mov ebx, 1
	mov ecx, DecDataMsg
	mov edx, 1
	int 0x80

    popa

	mov eax, 1
	mov ebx, 0
	int 0x80


printdata: ; input: ebx - pointer to data

	mov eax, [ebx]
	mov esi, OutString
	call toString

	mov eax, [ebx+4]
	mov esi, OutString+8
	call toString

	mov eax, [ebx+8]
	mov esi, OutString+16
	call toString

	mov eax, [ebx+12]
	mov esi, OutString+24
	call toString

	mov eax, 4
	mov ebx, 1
	mov ecx, OutString
	mov edx, 32
	int 0x80

ret

toString: ; IN=eax ; OUT=qword[esi] binary converted to ASCII HEX - Madis
		push ebx
        mov      edi,0F0F0F0Fh
        mov      ecx,eax
        mov      ebx,eax
        shr      eax,4
        and      ebx,edi
        and      eax,edi
        mov      ecx,eax
        mov      edx,ebx
        add      ebx,06060606h
        add      eax,06060606h
        shr      ebx,4
        shr      eax,4
        and      ebx,edi
        and      eax,edi
        lea      edx,[ebx*8+edx+30303030h]
        lea      ecx,[eax*8+ecx+30303030h]
        sub      edx,ebx
        sub      ecx,eax
        xchg     ch,dl
        rol      ecx,16
        rol      edx,16
        xchg     ch,dl
        rol      ecx,16
        xchg     cx,dx
        rol      edx,16
        mov      [esi],ecx
        mov      [esi+4],edx
		pop ebx
        ret


section '.data' writeable

KeyFipsTest	db 0x60,0x3d,0xeb,0x10
		db 0x15,0xca,0x71,0xbe
		db 0x2b,0x73,0xae,0xf0
		db 0x85,0x7d,0x77,0x81	 ; key test only
		db 0x1f,0x35,0x2c,0x07
		db 0x3b,0x61,0x08,0xd7
		db 0x2d,0x98,0x10,0xa3
		db 0x09,0x14,0xdf,0xf4

KeyFipsMain	db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
		db 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f  ; main key
		db 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17
		db 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f

FipsEncData	db 0x00,0x11,0x22,0x33
		db 0x44,0x55,0x66,0x77
		db 0x88,0x99,0xaa,0xbb
		db 0xcc,0xdd,0xee,0xff

FipsDecData	db 0x69,0xc4,0xe0,0xd8
		db 0x6a,0x7b,0x04,0x30
		db 0xd8,0xcd,0xb7,0x80
		db 0x70,0xb4,0xc5,0x5a

OrigDataMsg db 'Input Data:     ',0
.len = $ - OrigDataMsg
EncDataMsg db 10,'Encrypted Data: ',0
.len = $ - EncDataMsg
DecDataMsg db 10,'Decrypted Data: ',0
.len = $ - DecDataMsg

Outa		rb 16
Outb		rb 16
OutString   rb 32
