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