1 ;------------------------------------------------------------------------------ 2 ; 3 ; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR> 4 ; This program and the accompanying materials 5 ; are licensed and made available under the terms and conditions of the BSD License 6 ; which accompanies this distribution. The full text of the license may be found at 7 ; http://opensource.org/licenses/bsd-license.php. 8 ; 9 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 ; 12 ; Module Name: 13 ; 14 ; RdRand.nasm 15 ; 16 ; Abstract: 17 ; 18 ; Generates random number through CPU RdRand instruction under 32-bit platform. 19 ; 20 ; Notes: 21 ; 22 ;------------------------------------------------------------------------------ 23 24 SECTION .text 25 26 ;------------------------------------------------------------------------------ 27 ; Generates a 16 bit random number through RDRAND instruction. 28 ; Return TRUE if Rand generated successfully, or FALSE if not. 29 ; 30 ; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand); 31 ;------------------------------------------------------------------------------ 32 global ASM_PFX(InternalX86RdRand16) 33 ASM_PFX(InternalX86RdRand16): 34 ; rdrand ax ; generate a 16 bit RN into ax 35 ; CF=1 if RN generated ok, otherwise CF=0 36 db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)" 37 jc rn16_ok ; jmp if CF=1 38 xor eax, eax ; reg=0 if CF=0 39 ret ; return with failure status 40 rn16_ok: 41 mov edx, dword [esp + 4] 42 mov [edx], ax 43 mov eax, 1 44 ret 45 46 ;------------------------------------------------------------------------------ 47 ; Generates a 32 bit random number through RDRAND instruction. 48 ; Return TRUE if Rand generated successfully, or FALSE if not. 49 ; 50 ; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand); 51 ;------------------------------------------------------------------------------ 52 global ASM_PFX(InternalX86RdRand32) 53 ASM_PFX(InternalX86RdRand32): 54 ; rdrand eax ; generate a 32 bit RN into eax 55 ; CF=1 if RN generated ok, otherwise CF=0 56 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)" 57 jc rn32_ok ; jmp if CF=1 58 xor eax, eax ; reg=0 if CF=0 59 ret ; return with failure status 60 rn32_ok: 61 mov edx, dword [esp + 4] 62 mov [edx], eax 63 mov eax, 1 64 ret 65 66 ;------------------------------------------------------------------------------ 67 ; Generates a 64 bit random number through RDRAND instruction. 68 ; Return TRUE if Rand generated successfully, or FALSE if not. 69 ; 70 ; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand); 71 ;------------------------------------------------------------------------------ 72 global ASM_PFX(InternalX86RdRand64) 73 ASM_PFX(InternalX86RdRand64): 74 ; rdrand eax ; generate a 32 bit RN into eax 75 ; CF=1 if RN generated ok, otherwise CF=0 76 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)" 77 jnc rn64_ret ; jmp if CF=0 78 mov edx, dword [esp + 4] 79 mov [edx], eax 80 81 db 0xf, 0xc7, 0xf0 ; generate another 32 bit RN 82 jnc rn64_ret ; jmp if CF=0 83 mov [edx + 4], eax 84 85 mov eax, 1 86 ret 87 rn64_ret: 88 xor eax, eax 89 ret ; return with failure status 90 91