]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/IA32/GccRdRand.c
SecurityPkg: Integrate new RngLib into RngDxe
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / IA32 / GccRdRand.c
1 /** @file
2 RDRAND Support Routines for GCC environment.
3
4 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 /**
16 Generates a 16-bit random number through RDRAND instruction.
17
18 @param[out] Rand Buffer pointer to store the random result.
19
20 @retval TRUE RDRAND call was successful.
21 @retval FALSE Failed attempts to call RDRAND.
22
23 **/
24 BOOLEAN
25 EFIAPI
26 RdRand16Step (
27 OUT UINT16 *Rand
28 )
29 {
30 UINT8 Carry;
31
32 //
33 // Uses byte code for RDRAND instruction,
34 // in case that GCC version has no direct support on RDRAND assembly.
35 //
36 __asm__ __volatile__ (
37 ".byte 0x66; .byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"
38 :"=a" (*Rand),
39 "=qm" (Carry)
40 );
41
42 return (BOOLEAN) Carry;
43 }
44
45 /**
46 Generates a 32-bit random number through RDRAND instruction.
47
48 @param[out] Rand Buffer pointer to store the random result.
49
50 @retval TRUE RDRAND call was successful.
51 @retval FALSE Failed attempts to call RDRAND.
52
53 **/
54 BOOLEAN
55 EFIAPI
56 RdRand32Step (
57 OUT UINT32 *Rand
58 )
59 {
60 UINT8 Carry;
61
62 __asm__ __volatile__ (
63 ".byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"
64 :"=a" (*Rand),
65 "=qm" (Carry)
66 );
67
68 return (BOOLEAN) Carry;
69 }