2012-05-10 09:58:02 +02:00
|
|
|
/** @file
|
|
|
|
Pseudorandom Number Generator Wrapper Implementation over OpenSSL.
|
|
|
|
|
2013-11-19 03:57:19 +01:00
|
|
|
Copyright (c) 2012 - 2013, Intel Corporation. All rights reserved.<BR>
|
2012-05-10 09:58:02 +02:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "InternalCryptLib.h"
|
|
|
|
#include <openssl/rand.h>
|
2013-11-19 03:57:19 +01:00
|
|
|
#include <openssl/evp.h>
|
2012-05-10 09:58:02 +02:00
|
|
|
#include <Library/PrintLib.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sets up the seed value for the pseudorandom number generator.
|
|
|
|
|
|
|
|
This function sets up the seed value for the pseudorandom number generator.
|
|
|
|
If Seed is not NULL, then the seed passed in is used.
|
|
|
|
If Seed is NULL, then default seed is used.
|
|
|
|
|
|
|
|
@param[in] Seed Pointer to seed value.
|
|
|
|
If NULL, default seed is used.
|
|
|
|
@param[in] SeedSize Size of seed value.
|
|
|
|
If Seed is NULL, this parameter is ignored.
|
|
|
|
|
|
|
|
@retval TRUE Pseudorandom number generator has enough entropy for random generation.
|
|
|
|
@retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
|
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
RandomSeed (
|
|
|
|
IN CONST UINT8 *Seed OPTIONAL,
|
|
|
|
IN UINTN SeedSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CHAR8 DefaultSeed[128];
|
|
|
|
|
2012-08-02 04:49:24 +02:00
|
|
|
if (SeedSize > INT_MAX) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-11-19 03:57:19 +01:00
|
|
|
//
|
|
|
|
// The software PRNG implementation built in OpenSSL depends on message digest algorithm.
|
|
|
|
// Make sure SHA-1 digest algorithm is available here.
|
|
|
|
//
|
|
|
|
if (EVP_add_digest (EVP_sha1 ()) == 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-05-10 09:58:02 +02:00
|
|
|
//
|
|
|
|
// Seed the pseudorandom number generator with user-supplied value.
|
|
|
|
// NOTE: A cryptographic PRNG must be seeded with unpredictable data.
|
|
|
|
//
|
|
|
|
if (Seed != NULL) {
|
|
|
|
RAND_seed (Seed, (UINT32) SeedSize);
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Retrieve current time.
|
|
|
|
//
|
|
|
|
AsciiSPrint (
|
|
|
|
DefaultSeed,
|
|
|
|
sizeof (DefaultSeed),
|
|
|
|
"UEFI Crypto Library default seed (%ld)",
|
|
|
|
AsmReadTsc ()
|
|
|
|
);
|
|
|
|
|
|
|
|
RAND_seed (DefaultSeed, sizeof (DefaultSeed));
|
|
|
|
}
|
|
|
|
|
2013-05-29 07:30:13 +02:00
|
|
|
if (RAND_status () == 1) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2012-05-10 09:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generates a pseudorandom byte stream of the specified size.
|
|
|
|
|
|
|
|
If Output is NULL, then return FALSE.
|
|
|
|
|
|
|
|
@param[out] Output Pointer to buffer to receive random value.
|
|
|
|
@param[in] Size Size of randome bytes to generate.
|
|
|
|
|
|
|
|
@retval TRUE Pseudorandom byte stream generated successfully.
|
|
|
|
@retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
|
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
RandomBytes (
|
|
|
|
OUT UINT8 *Output,
|
|
|
|
IN UINTN Size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Check input parameters.
|
|
|
|
//
|
2012-08-02 04:49:24 +02:00
|
|
|
if (Output == NULL || Size > INT_MAX) {
|
2012-05-10 09:58:02 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Generate random data.
|
|
|
|
//
|
|
|
|
if (RAND_bytes (Output, (UINT32) Size) != 1) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|