EnderAlice

RDRAND and RDSEED sample

Jul 6th, 2015
422
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; The MIT License (MIT)
  2. ;
  3. ; Copyright © 2015 alice
  4. ;
  5. ; Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. ; this software and associated documentation files (the "Software"), to deal in
  7. ; the Software without restriction, including without limitation the rights to
  8. ; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. ; the Software, and to permit persons to whom the Software is furnished to do so,
  10. ; subject to the following conditions:
  11. ;
  12. ; The above copyright notice and this permission notice shall be included in all
  13. ; copies or substantial portions of the Software.
  14. ;
  15. ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. ; FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. ; COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. ; IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. ; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. TITLE RAND_ASM.ASM
  23. ; ml64 /c rand_asm.asm
  24. EXTERN __imp_rand:NEAR
  25.  
  26. .CODE
  27. hw_rand PROC
  28.     call check_rdrand
  29.     test al, al
  30.     jnz rdrand_supported
  31.     call qword ptr [__imp_rand]
  32.     ret
  33. rdrand_supported:
  34.     rdrand rax
  35.     jnc hw_rand
  36.     ret
  37. hw_rand ENDP
  38.  
  39. hw_srand PROC
  40.     call check_rdseed
  41.     test al, al
  42.     jnz rdseed_supported
  43.     call qword ptr [__imp_rand]
  44.     ret
  45. rdseed_supported:
  46.     rdseed rcx
  47.     jnc hw_srand
  48.     mov rax, rcx
  49.     ret
  50. hw_srand ENDP
  51.  
  52. check_hw_rng PROC
  53.     push r14
  54.     xor r14, r14
  55.     call check_rdrand
  56.     test al, al
  57.     jz no_rdrand
  58.     or r14b, 001h
  59. no_rdrand:
  60.     call check_rdseed
  61.     test al, al
  62.     jz no_rdseed
  63.     or r14b, 002h
  64. no_rdseed:
  65.     mov al, r14b
  66.     pop r14
  67.     ret
  68. check_hw_rng ENDP
  69.  
  70. check_rdrand PROC
  71.     push rbx
  72.     mov eax, 001h
  73.     cpuid
  74.     xor eax, eax
  75.     test ecx, 040000000h ; 30
  76.     jz rdrand_not_supported
  77.     inc al
  78. rdrand_not_supported:
  79.     pop rbx
  80.     ret
  81. check_rdrand ENDP
  82.  
  83. check_rdseed PROC
  84.     push rbx
  85.     mov eax, 007h
  86.     xor ecx, ecx
  87.     cpuid
  88.     xor eax, eax
  89.     test ebx, 000040000h ; 18
  90.     jz rdseed_not_supported
  91.     inc al
  92. rdseed_not_supported:
  93.     pop rbx
  94.     ret
  95. check_rdseed ENDP
  96. END
RAW Paste Data