Assembly Language for Beginners

(nextflipdebug2) #1

8.6. “QR9”: RUBIK’S CUBE INSPIRED AMATEUR CRYPTO-ALGORITHM


.text:00541369 mov esi, [esp+2Ch+Str]
; reset all lowest 6 bits
.text:0054136D and esi, 0FFFFFFC0h
; align size to 64-byte border
.text:00541370 add esi, 40h


Allocate buffer with aligned size:


.text:00541373 push esi ; Size
.text:00541374 call _malloc


Call memset(), e.g., clear the allocated buffer^17.


.text:00541379 mov ecx, esi
.text:0054137B mov ebx, eax ; allocated buffer pointer -> to EBX
.text:0054137D mov edx, ecx
.text:0054137F xor eax, eax
.text:00541381 mov edi, ebx
.text:00541383 push ebp ; File
.text:00541384 shr ecx, 2
.text:00541387 rep stosd
.text:00541389 mov ecx, edx
.text:0054138B push 1 ; Count
.text:0054138D and ecx, 3
.text:00541390 rep stosb ; memset (buffer, 0, aligned_size)


Read file via the standard C functionfread().


.text:00541392 mov eax, [esp+38h+Str]
.text:00541396 push eax ; ElementSize
.text:00541397 push ebx ; DstBuf
.text:00541398 call _fread ; read file
.text:0054139D push ebp ; File
.text:0054139E call _fclose


Callcrypt(). This function takes a buffer, buffer size (aligned) and a password string.


.text:005413A3 mov ecx, [esp+44h+password]
.text:005413A7 push ecx ; password
.text:005413A8 push esi ; aligned size
.text:005413A9 push ebx ; buffer
.text:005413AA call crypt ; do crypt


Create the output file. By the way, the developer forgot to check if it has been created correctly! The file
opening result is being checked, though.


.text:005413AF mov edx, [esp+50h+Filename]
.text:005413B3 add esp, 40h
.text:005413B6 push offset aWb ; "wb"
.text:005413BB push edx ; Filename
.text:005413BC call _fopen
.text:005413C1 mov edi, eax


The newly created file handle is in theEDIregister now. Write signature “QR9”.


.text:005413C3 push edi ; File
.text:005413C4 push 1 ; Count
.text:005413C6 push 3 ; Size
.text:005413C8 push offset aQr9 ; "QR9"
.text:005413CD call _fwrite ; write file signature


Write the actual file size (not aligned):


.text:005413D2 push edi ; File
.text:005413D3 push 1 ; Count
.text:005413D5 lea eax, [esp+30h+Str]
.text:005413D9 push 4 ; Size


(^17) malloc() + memset() could be replaced by calloc()

Free download pdf