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 64-bit platform. 19; 20; Notes: 21; 22;------------------------------------------------------------------------------ 23 24 DEFAULT REL 25 SECTION .text 26 27;------------------------------------------------------------------------------ 28; Generates a 16 bit random number through RDRAND instruction. 29; Return TRUE if Rand generated successfully, or FALSE if not. 30; 31; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand); 32;------------------------------------------------------------------------------ 33global ASM_PFX(InternalX86RdRand16) 34ASM_PFX(InternalX86RdRand16): 35 ; rdrand ax ; generate a 16 bit RN into eax, 36 ; CF=1 if RN generated ok, otherwise CF=0 37 db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)" 38 jc rn16_ok ; jmp if CF=1 39 xor rax, rax ; reg=0 if CF=0 40 ret ; return with failure status 41rn16_ok: 42 mov [rcx], ax 43 mov rax, 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;------------------------------------------------------------------------------ 52global ASM_PFX(InternalX86RdRand32) 53ASM_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 rax, rax ; reg=0 if CF=0 59 ret ; return with failure status 60rn32_ok: 61 mov [rcx], eax 62 mov rax, 1 63 ret 64 65;------------------------------------------------------------------------------ 66; Generates a 64 bit random number through one RDRAND instruction. 67; Return TRUE if Rand generated successfully, or FALSE if not. 68; 69; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random); 70;------------------------------------------------------------------------------ 71global ASM_PFX(InternalX86RdRand64) 72ASM_PFX(InternalX86RdRand64): 73 ; rdrand rax ; generate a 64 bit RN into rax, 74 ; CF=1 if RN generated ok, otherwise CF=0 75 db 0x48, 0xf, 0xc7, 0xf0 ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)" 76 jc rn64_ok ; jmp if CF=1 77 xor rax, rax ; reg=0 if CF=0 78 ret ; return with failure status 79rn64_ok: 80 mov [rcx], rax 81 mov rax, 1 82 ret 83 84