• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.asm
15;
16; Abstract:
17;
18;   Generates random number through CPU RdRand instruction under 64-bit platform.
19;
20; Notes:
21;
22;------------------------------------------------------------------------------
23
24    .code
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;------------------------------------------------------------------------------
32InternalX86RdRand16  PROC
33    ; rdrand   ax                  ; generate a 16 bit RN into eax,
34                                   ; CF=1 if RN generated ok, otherwise CF=0
35    db     0fh, 0c7h, 0f0h         ; rdrand r16: "0f c7 /6  ModRM:r/m(w)"
36    jc     rn16_ok                 ; jmp if CF=1
37    xor    rax, rax                ; reg=0 if CF=0
38    ret                            ; return with failure status
39rn16_ok:
40    mov    [rcx], ax
41    mov    rax,  1
42    ret
43InternalX86RdRand16 ENDP
44
45;------------------------------------------------------------------------------
46;  Generates a 32 bit random number through RDRAND instruction.
47;  Return TRUE if Rand generated successfully, or FALSE if not.
48;
49;  BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
50;------------------------------------------------------------------------------
51InternalX86RdRand32  PROC
52    ; rdrand   eax                 ; generate a 32 bit RN into eax,
53                                   ; CF=1 if RN generated ok, otherwise CF=0
54    db     0fh, 0c7h, 0f0h         ; rdrand r32: "0f c7 /6  ModRM:r/m(w)"
55    jc     rn32_ok                 ; jmp if CF=1
56    xor    rax, rax                ; reg=0 if CF=0
57    ret                            ; return with failure status
58rn32_ok:
59    mov    [rcx], eax
60    mov    rax,  1
61    ret
62InternalX86RdRand32 ENDP
63
64;------------------------------------------------------------------------------
65;  Generates a 64 bit random number through one RDRAND instruction.
66;  Return TRUE if Rand generated successfully, or FALSE if not.
67;
68;  BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random);
69;------------------------------------------------------------------------------
70InternalX86RdRand64  PROC
71    ; rdrand   rax                 ; generate a 64 bit RN into rax,
72                                   ; CF=1 if RN generated ok, otherwise CF=0
73    db     048h, 0fh, 0c7h, 0f0h   ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"
74    jc     rn64_ok                 ; jmp if CF=1
75    xor    rax, rax                ; reg=0 if CF=0
76    ret                            ; return with failure status
77rn64_ok:
78    mov    [rcx], rax
79    mov    rax, 1
80    ret
81InternalX86RdRand64 ENDP
82
83    END
84