Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
Created February 7, 2012 18:11
Show Gist options
  • Save maxdeliso/1761045 to your computer and use it in GitHub Desktop.
Save maxdeliso/1761045 to your computer and use it in GitHub Desktop.
Some 64 bit linux assembly for detecting and using specialized AES instructions.
A program designed to test operating system capabilities on 64 bit CPUs.
Currently supported operating systems: Linux, Mac OS X
In order to build, you need nasm: the netwide assembler.
$ make -f makefile.{your os}
Max DeLiso <maxdeliso@gmail.com>
bits 64
;; exports
global aes_feature_check
section .text
aes_feature_check:
;; preconditions
; none
;; postconditions
; returns 0 on success, -1 or -2 on failure
mov eax, 1
cpuid ; feature bits returned int ecx/edx
and ecx, 01a000000h
cmp ecx, 01a000000h
jne .fail ; check OSXSAVE AVX and AESNI
mov ecx, 0
xgetbv ; value returned in eax
and eax, 06h
cmp eax, 06h
jne .fail2 ; check for XMM and YMM
xor rax, rax ; return 0 for failure
ret
.fail:
mov rax, -1 ; return -1 for no OSXSAVE AVX and AESNI
ret
.fail2:
mov rax, -2 ; return -2 for missing XMM and YMM
ret
bits 64
;; exports
global exit
;; imports
; none
section .text
exit: ; rbx is return code from caller
mov rax, 60
syscall
bits 64
;; exports
global exit
;; imports
; none
section .text
exit: ; rbx is return code from caller,
; but mac os x's abi expects it in rdi,
; so copy it over
mov rax, 0x2000001
mov rdi, rbx
syscall
bits 64
;; exports
global exit
;; imports
; none
section .text
exit:
xor rcx, rcx
xor rdx, rdx
mov rax, 0x29
syscall ;TODO: fix this, it sould terminate the caller but doesn't
ret ; as a fallback
LD=ld
RM=rm
PROBE=probe
NASM=nasm
AFLAGS=-felf64
LDFLAGS =-e probe -pie --gc-sections --print-gc-sections
LDFLAGS+=-dynamic-linker /lib64/ld-linux-x86-64.so.2 # replace with your dynamic linker path
LDFLAGS+=-z execstack -z initfirst -z relro -z now
MODULES=probe.o cpuid.o print_linux.o exit_linux.o
OBJDIR=obj
STRIP=strip
.PHONY: clean
probe: $(MODULES) makefile.linux
$(LD) $(LDFLAGS) $(MODULES) -o $(PROBE)
# $(STRIP) $(PROBE)
%.o : %.asm
$(NASM) $(AFLAGS) -o $@ $<
clean:
$(RM) -f $(PROBE) $(MODULES) *~
LD=ld
RM=rm
PROBE=probe
NASM=nasm
AFLAGS=-fmacho64
LDFLAGS=-e probe -macosx_version_min 10.6
MODULES=probe.o cpuid.o print_macosx.o exit_macosx.o
OBJDIR=obj
STRIP=strip
.PHONY: clean
probe: $(MODULES)
$(LD) $(LDFLAGS) $(MODULES) -o $(PROBE)
# $(STRIP) $(PROBE)
%.o : %.asm
$(NASM) $(AFLAGS) -o $@ $<
clean:
$(RM) -f $(PROBE) $(MODULES) *~
LD=link
RM=del
PROBE=probe
NASM=nasm
AFLAGS=-fwin64
LDFLAGS=/entry:probe /subsystem:console /release /nologo /tsaware\
/dynamicbase /errorReport:none /nxcompat /opt:ref /out:probe.exe
MODULES=probe.obj cpuid.obj print_win64.obj exit_win64.obj
probe: $(MODULES) makefile.win64
$(LD) $(LDFLAGS) $(MODULES)
.asm.obj:
$(NASM) -o $@ $(AFLAGS) $<
clean:
$(RM) $(PROBE).exe $(MODULES) *~
bits 64
;;exports
global print
;;imports
;none
section .text
print:
;; preconditions
; rsi contains pointer to string
; rdx contains number of bytes to write
;; postconditions
; none
mov rax, 1
mov rdi, 1
syscall
ret
bits 64
;;exports
global print
;;imports
;none
section .text
print:
;; preconditions
; rsi contains pointer to string
; rdx contains number of bytes to write
;; postconditions
; some stdio action
mov rax, 0x2000004 ; UNIX class + SYS_write
mov rdi, 1 ; write to stdout
syscall ; trap (rdx already contains length)
ret
bits 64
;;exports
global print
;;imports
;none
section .text
print:
;; preconditions
; rsi contains pointer to string
; rdx contains number of bytes to write
;; postconditions
; some stdio action
;mov rax, 0x2000004 ; UNIX class + SYS_write
;mov rdi, 1 ; write to stdout
;syscall ; trap (rdx already contains length)
ret
bits 64
;;exports
global probe ;entry point
;;imports
extern print ;; in (rsi, rdx) = (string address, string length)
extern exit ;; in (rbx) = (return code)
extern aes_feature_check
section .text
probe:
push rbp
mov rbp, rsp ; build main stack frame
mov rsi, probeMsg
mov rdx, probeMsgLen
call print ; display startup message
call aes_feature_check
cmp rax, rax
jnz .done
nop ; TODO: some cpu accelerated cipherage
.done:
pop rbp ; cleanup main stack frame
xor rdi, rdi ; return 0 for success
call exit
ret ; added for windows case as fallback
section .rodata
probeMsg: db 'pr0be', 10, 0 ; hopefully this is 1337 enough
probeMsgLen: equ $-probeMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment