]>
Commit | Line | Data |
---|---|---|
55745c24 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 32-bit platform.\r | |
19 | ;\r | |
20 | ; Notes:\r | |
21 | ;\r | |
22 | ;------------------------------------------------------------------------------\r | |
23 | \r | |
24 | SECTION .text\r | |
25 | \r | |
26 | ;------------------------------------------------------------------------------\r | |
27 | ; Generates a 16 bit random number through RDRAND instruction.\r | |
28 | ; Return TRUE if Rand generated successfully, or FALSE if not.\r | |
29 | ;\r | |
30 | ; BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);\r | |
31 | ;------------------------------------------------------------------------------\r | |
32 | global ASM_PFX(AsmRdRand16)\r | |
33 | ASM_PFX(AsmRdRand16):\r | |
34 | ; rdrand ax ; generate a 16 bit RN into ax\r | |
35 | ; CF=1 if RN generated ok, otherwise CF=0\r | |
36 | db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"\r | |
37 | jc rn16_ok ; jmp if CF=1\r | |
38 | xor eax, eax ; reg=0 if CF=0\r | |
39 | ret ; return with failure status\r | |
40 | rn16_ok:\r | |
41 | mov edx, dword [esp + 4]\r | |
42 | mov [edx], ax\r | |
43 | mov eax, 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 eax, eax ; reg=0 if CF=0\r | |
59 | ret ; return with failure status\r | |
60 | rn32_ok:\r | |
61 | mov edx, dword [esp + 4]\r | |
62 | mov [edx], eax\r | |
63 | mov eax, 1\r | |
64 | ret\r | |
65 | \r | |
66 | ;------------------------------------------------------------------------------\r | |
67 | ; Generates a 64 bit random number through RDRAND instruction.\r | |
68 | ; Return TRUE if Rand generated successfully, or FALSE if not.\r | |
69 | ;\r | |
70 | ; BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);\r | |
71 | ;------------------------------------------------------------------------------\r | |
72 | global ASM_PFX(AsmRdRand64)\r | |
73 | ASM_PFX(AsmRdRand64):\r | |
74 | ; rdrand eax ; generate a 32 bit RN into eax\r | |
75 | ; CF=1 if RN generated ok, otherwise CF=0\r | |
76 | db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r | |
77 | jnc rn64_ret ; jmp if CF=0\r | |
78 | mov edx, dword [esp + 4]\r | |
79 | mov [edx], eax\r | |
80 | \r | |
81 | db 0xf, 0xc7, 0xf0 ; generate another 32 bit RN\r | |
82 | jnc rn64_ret ; jmp if CF=0\r | |
83 | mov [edx + 4], eax\r | |
84 | \r | |
85 | mov eax, 1\r | |
86 | ret\r | |
87 | rn64_ret:\r | |
88 | xor eax, eax\r | |
89 | ret ; return with failure status\r | |
90 | \r |