13cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
23cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
39ec9a7a59769927ad1f5662f78a52031cb9cd758Qin Long; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
43cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; This program and the accompanying materials
53cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; are licensed and made available under the terms and conditions of the BSD License
63cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; which accompanies this distribution.  The full text of the license may be found at
73cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; http://opensource.org/licenses/bsd-license.php.
83cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
93cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
103cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
113cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
123cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; Module Name:
133cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
143cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;   RdRand.asm
153cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
163cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; Abstract:
173cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
183cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;   Generates random number through CPU RdRand instruction under 32-bit platform.
193cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
203cfc7813bb7e904c4a940d638facc10be5e22645Qin Long; Notes:
213cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
223cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
233cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
243cfc7813bb7e904c4a940d638facc10be5e22645Qin Long.686P
253cfc7813bb7e904c4a940d638facc10be5e22645Qin Long.model flat, C
263cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
273cfc7813bb7e904c4a940d638facc10be5e22645Qin Long.code
283cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
293cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
303cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Generates a 16 bit random number through RDRAND instruction.
313cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Return TRUE if Rand generated successfully, or FALSE if not.
323cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
339ec9a7a59769927ad1f5662f78a52031cb9cd758Qin Long;  BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
343cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
359ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand16  PROC
363cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ; rdrand   ax                  ; generate a 16 bit RN into ax
373cfc7813bb7e904c4a940d638facc10be5e22645Qin Long                                   ; CF=1 if RN generated ok, otherwise CF=0
383cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    db     0fh, 0c7h, 0f0h         ; rdrand r16: "0f c7 /6  ModRM:r/m(w)"
393cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    jc     rn16_ok                 ; jmp if CF=1
403cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    xor    eax, eax                ; reg=0 if CF=0
413cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret                            ; return with failure status
423cfc7813bb7e904c4a940d638facc10be5e22645Qin Longrn16_ok:
433cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    edx, dword ptr [esp + 4]
443cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    [edx], ax
453cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    eax,  1
463cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret
479ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand16 ENDP
483cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
493cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
503cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Generates a 32 bit random number through RDRAND instruction.
513cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Return TRUE if Rand generated successfully, or FALSE if not.
523cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
539ec9a7a59769927ad1f5662f78a52031cb9cd758Qin Long;  BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
543cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
559ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand32  PROC
563cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ; rdrand   eax                 ; generate a 32 bit RN into eax
573cfc7813bb7e904c4a940d638facc10be5e22645Qin Long                                   ; CF=1 if RN generated ok, otherwise CF=0
583cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    db     0fh, 0c7h, 0f0h         ; rdrand r32: "0f c7 /6  ModRM:r/m(w)"
593cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    jc     rn32_ok                 ; jmp if CF=1
603cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    xor    eax, eax                ; reg=0 if CF=0
613cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret                            ; return with failure status
623cfc7813bb7e904c4a940d638facc10be5e22645Qin Longrn32_ok:
633cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    edx, dword ptr [esp + 4]
643cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    [edx], eax
653cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    eax,  1
663cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret
679ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand32 ENDP
683cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
693cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
703cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Generates a 64 bit random number through RDRAND instruction.
713cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;  Return TRUE if Rand generated successfully, or FALSE if not.
723cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;
739ec9a7a59769927ad1f5662f78a52031cb9cd758Qin Long;  BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
743cfc7813bb7e904c4a940d638facc10be5e22645Qin Long;------------------------------------------------------------------------------
759ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand64  PROC
763cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ; rdrand   eax                 ; generate a 32 bit RN into eax
773cfc7813bb7e904c4a940d638facc10be5e22645Qin Long                                   ; CF=1 if RN generated ok, otherwise CF=0
783cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    db     0fh, 0c7h, 0f0h         ; rdrand r32: "0f c7 /6  ModRM:r/m(w)"
793cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    jnc    rn64_ret                ; jmp if CF=0
803cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    edx, dword ptr [esp + 4]
813cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    [edx], eax
823cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
833cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    db     0fh, 0c7h, 0f0h         ; generate another 32 bit RN
843cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    jnc    rn64_ret                ; jmp if CF=0
853cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    [edx + 4], eax
863cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
873cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    mov    eax,  1
883cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret
893cfc7813bb7e904c4a940d638facc10be5e22645Qin Longrn64_ret:
903cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    xor    eax, eax
913cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    ret                            ; return with failure status
929ec9a7a59769927ad1f5662f78a52031cb9cd758Qin LongInternalX86RdRand64 ENDP
933cfc7813bb7e904c4a940d638facc10be5e22645Qin Long
943cfc7813bb7e904c4a940d638facc10be5e22645Qin Long    END
95