• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2   Pseudorandom Number Generator Wrapper Implementation over OpenSSL.
3 
4 Copyright (c) 2010 - 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 #include "InternalCryptLib.h"
16 #include <openssl/rand.h>
17 #include <openssl/evp.h>
18 
19 //
20 // Default seed for UEFI Crypto Library
21 //
22 CONST UINT8  DefaultSeed[] = "UEFI Crypto Library default seed";
23 
24 /**
25   Sets up the seed value for the pseudorandom number generator.
26 
27   This function sets up the seed value for the pseudorandom number generator.
28   If Seed is not NULL, then the seed passed in is used.
29   If Seed is NULL, then default seed is used.
30 
31   @param[in]  Seed      Pointer to seed value.
32                         If NULL, default seed is used.
33   @param[in]  SeedSize  Size of seed value.
34                         If Seed is NULL, this parameter is ignored.
35 
36   @retval TRUE   Pseudorandom number generator has enough entropy for random generation.
37   @retval FALSE  Pseudorandom number generator does not have enough entropy for random generation.
38 
39 **/
40 BOOLEAN
41 EFIAPI
RandomSeed(IN CONST UINT8 * Seed OPTIONAL,IN UINTN SeedSize)42 RandomSeed (
43   IN  CONST  UINT8  *Seed  OPTIONAL,
44   IN  UINTN         SeedSize
45   )
46 {
47   if (SeedSize > INT_MAX) {
48     return FALSE;
49   }
50 
51   //
52   // The software PRNG implementation built in OpenSSL depends on message digest algorithm.
53   // Make sure SHA-1 digest algorithm is available here.
54   //
55   if (EVP_add_digest (EVP_sha1 ()) == 0) {
56     return FALSE;
57   }
58 
59   //
60   // Seed the pseudorandom number generator with user-supplied value.
61   // NOTE: A cryptographic PRNG must be seeded with unpredictable data.
62   //
63   if (Seed != NULL) {
64     RAND_seed (Seed, (UINT32) SeedSize);
65   } else {
66     RAND_seed (DefaultSeed, sizeof (DefaultSeed));
67   }
68 
69   if (RAND_status () == 1) {
70     return TRUE;
71   }
72 
73   return FALSE;
74 }
75 
76 /**
77   Generates a pseudorandom byte stream of the specified size.
78 
79   If Output is NULL, then return FALSE.
80 
81   @param[out]  Output  Pointer to buffer to receive random value.
82   @param[in]   Size    Size of random bytes to generate.
83 
84   @retval TRUE   Pseudorandom byte stream generated successfully.
85   @retval FALSE  Pseudorandom number generator fails to generate due to lack of entropy.
86 
87 **/
88 BOOLEAN
89 EFIAPI
RandomBytes(OUT UINT8 * Output,IN UINTN Size)90 RandomBytes (
91   OUT  UINT8  *Output,
92   IN   UINTN  Size
93   )
94 {
95   //
96   // Check input parameters.
97   //
98   if (Output == NULL || Size > INT_MAX) {
99     return FALSE;
100   }
101 
102   //
103   // Generate random data.
104   //
105   if (RAND_bytes (Output, (UINT32) Size) != 1) {
106     return FALSE;
107   }
108 
109   return TRUE;
110 }
111