Hello, all! I was having a problem trying to find asm-example to generate random bytes via winapi. Here is my version, maybe someone will find it useful. This example will show message box with random hex string.
format PE GUI
include 'win32ax.inc'
; MSDN Random Example:
; https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx
.data
LENGTH equ 10 ; number of random BYTES
buffer rb 255 ; random hex string
hcrypt dd ?
ascii db '0123456789ABCDEF'
RESERVED equ NULL
PROV_RSA_FULL equ 1 ; values from wincrypt.h:
CRYPT_VERIFYCONTEXT equ 0xF0000000 ; http://sourceforge.net/u/earnie/winapi/winapi/ci/Initial/tree/include/wincrypt.h
.code
start:
; wincrypto funcitons are from Advapi32.dll
invoke CryptAcquireContext, hcrypt, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT
invoke CryptGenRandom, [hcrypt], LENGTH, buffer
invoke CryptReleaseContext, [hcrypt], RESERVED
; now buffer contains LENGTH random BYTES
; we need to transform it to HEX string
; gonna fill buffer with hex characters backwards
mov edi, LENGTH * 2 ; edi is index of hex string
mov esi, LENGTH ; esi is index of random generated byte
mov [buffer + edi], 0 ; terminate our hex string
mov eax, 0 ; eax is index of ascii character
bin2hex:
mov al, [buffer + esi - 1] ; copy random byte to AL
shr al, 4 ; get last 4 bits of the byte
mov cl, [ascii + eax] ; copy corresponding ascii character to its place
mov [buffer + edi - 1], cl ;
dec edi
mov al, [buffer + esi - 1]
shl al, 4 ; get first 4 bits of the byte
shr al, 4 ;
mov cl, [ascii + eax]
mov [buffer + edi - 1], cl ; copy ascii character to its place
dec edi
dec esi
jnz bin2hex ; repeat until index of random generated byte is ZERO
invoke MessageBox, 0, buffer, 'Random Hex', MB_OK
finish:
invoke ExitProcess, 0
.end start