public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Xiaoyu Lu" <xiaoyux.lu@intel.com>
To: devel@edk2.groups.io
Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>,
	Jian J Wang <jian.j.wang@intel.com>, Ting Ye <ting.ye@intel.com>
Subject: [PATCH v5 7/9] CryptoPkg/OpensslLib: Add functions for upgrading OpenSSL1_1_1b
Date: Wed, 29 May 2019 06:40:36 -0400	[thread overview]
Message-ID: <1559126438-9211-8-git-send-email-xiaoyux.lu@intel.com> (raw)
In-Reply-To: <1559126438-9211-1-git-send-email-xiaoyux.lu@intel.com>

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1089

* From OpenSSL_1_1_0i(97c0959f27b294fe1eb10b547145ebef2524b896) to
  OpenSSL_1_1_1b(50eaac9f3337667259de725451f201e784599687), OpenSSL
  updated DRBG / RAND to request nonce and additional low entropy
  randomness from system(line 229 openssl/CHANGES).

  Since OpenSSL_1_1_1b doesn't fully implement rand pool functions
  for UEFI. We must provide a method to implenet these method.
  TSC is used as first entropy source if it's availabe otherwise
  fallback to TimerLib. But we are not sure the amount of randomness
  they provide. If you really care about the security, one choice is
  overrided it with hardware generator.

  Add rand_pool.c to implement these functions required by OpenSSL
    rand_pool_acquire_entropy
    rand_pool_add_nonce_data
    rand_pool_add_additional_data
    rand_pool_init
    rand_pool_cleanup
    rand_pool_keep_random_devices_open

  And add rand_pool_noise.* for getting entropy noise from different
  architecture.

* We don't need ossl_store functions. We exclude relative files
  through process_files.pl. And ossl_store_cleanup_int was first
  added in crypto/init.c OpenSSL_1_1_1(71a5516d).
  So add a new file(ossl_store.c) to implement ossl_store_cleanup_int
  function.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Ting Ye <ting.ye@intel.com>
Signed-off-by: Xiaoyu Lu <xiaoyux.lu@intel.com>
Tested-by: Gary Lin <glin@suse.com>
---
 CryptoPkg/Library/OpensslLib/rand_pool_noise.h     |  29 ++
 CryptoPkg/Library/OpensslLib/ossl_store.c          |  17 ++
 CryptoPkg/Library/OpensslLib/rand_pool.c           | 316 +++++++++++++++++++++
 CryptoPkg/Library/OpensslLib/rand_pool_noise.c     |  29 ++
 CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c |  43 +++
 5 files changed, 434 insertions(+)
 create mode 100644 CryptoPkg/Library/OpensslLib/rand_pool_noise.h
 create mode 100644 CryptoPkg/Library/OpensslLib/ossl_store.c
 create mode 100644 CryptoPkg/Library/OpensslLib/rand_pool.c
 create mode 100644 CryptoPkg/Library/OpensslLib/rand_pool_noise.c
 create mode 100644 CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c

diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h b/CryptoPkg/Library/OpensslLib/rand_pool_noise.h
new file mode 100644
index 000000000000..75acc686a9f1
--- /dev/null
+++ b/CryptoPkg/Library/OpensslLib/rand_pool_noise.h
@@ -0,0 +1,29 @@
+/** @file
+  Provide rand noise source.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef __RAND_POOL_NOISE_H__
+#define __RAND_POOL_NOISE_H__
+
+#include <Uefi/UefiBaseType.h>
+
+/**
+   Get 64-bit noise source.
+
+   @param[out] Rand         Buffer pointer to store 64-bit noise source
+
+   @retval TRUE             Get randomness successfully.
+   @retval FALSE            Failed to generate
+**/
+BOOLEAN
+EFIAPI
+GetRandomNoise64 (
+  OUT UINT64         *Rand
+  );
+
+
+#endif // __RAND_POOL_NOISE_H__
diff --git a/CryptoPkg/Library/OpensslLib/ossl_store.c b/CryptoPkg/Library/OpensslLib/ossl_store.c
new file mode 100644
index 000000000000..29e1506048e3
--- /dev/null
+++ b/CryptoPkg/Library/OpensslLib/ossl_store.c
@@ -0,0 +1,17 @@
+/** @file
+  Dummy implement ossl_store(Store retrieval functions) for UEFI.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+/*
+ * This function is cleanup ossl store.
+ *
+ * Dummy Implement for UEFI
+ */
+void ossl_store_cleanup_int(void)
+{
+}
+
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool.c b/CryptoPkg/Library/OpensslLib/rand_pool.c
new file mode 100644
index 000000000000..9d2a4ad13823
--- /dev/null
+++ b/CryptoPkg/Library/OpensslLib/rand_pool.c
@@ -0,0 +1,316 @@
+/** @file
+  OpenSSL_1_1_1b doesn't implement rand_pool_* functions for UEFI.
+  The file implement these functions.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "internal/rand_int.h"
+#include <openssl/aes.h>
+
+#include <Uefi.h>
+#include <Library/TimerLib.h>
+
+#include "rand_pool_noise.h"
+
+/**
+  Get some randomness from low-order bits of GetPerformanceCounter results.
+  And combine them to the 64-bit value
+
+  @param[out] Rand    Buffer pointer to store the 64-bit random value.
+
+  @retval TRUE        Random number generated successfully.
+  @retval FALSE       Failed to generate.
+**/
+STATIC
+BOOLEAN
+EFIAPI
+GetRandNoise64FromPerformanceCounter(
+  OUT UINT64      *Rand
+  )
+{
+  UINT32 Index;
+  UINT32 *RandPtr;
+
+  if (NULL == Rand) {
+    return FALSE;
+  }
+
+  RandPtr = (UINT32 *) Rand;
+
+  for (Index = 0; Index < 2; Index ++) {
+    *RandPtr = (UINT32) (GetPerformanceCounter () & 0xFF);
+    MicroSecondDelay (10);
+    RandPtr++;
+  }
+
+  return TRUE;
+}
+
+/**
+  Calls RandomNumber64 to fill
+  a buffer of arbitrary size with random bytes.
+
+  @param[in]   Length        Size of the buffer, in bytes,  to fill with.
+  @param[out]  RandBuffer    Pointer to the buffer to store the random result.
+
+  @retval EFI_SUCCESS        Random bytes generation succeeded.
+  @retval EFI_NOT_READY      Failed to request random bytes.
+
+**/
+STATIC
+BOOLEAN
+EFIAPI
+RandGetBytes (
+  IN UINTN         Length,
+  OUT UINT8        *RandBuffer
+  )
+{
+  BOOLEAN     Ret;
+  UINT64      TempRand;
+
+  Ret = FALSE;
+
+  while (Length > 0) {
+    //
+    // Get random noise from platform.
+    // If it failed, fallback to PerformanceCounter
+    // If you really care about security, you must override
+    // GetRandomNoise64FromPlatform.
+    //
+    Ret = GetRandomNoise64 (&TempRand);
+    if (Ret == FALSE) {
+      Ret = GetRandNoise64FromPerformanceCounter (&TempRand);
+    }
+    if (!Ret) {
+      return Ret;
+    }
+    if (Length >= sizeof (TempRand)) {
+      *((UINT64*) RandBuffer) = TempRand;
+      RandBuffer += sizeof (UINT64);
+      Length -= sizeof (TempRand);
+    } else {
+      CopyMem (RandBuffer, &TempRand, Length);
+      Length = 0;
+    }
+  }
+
+  return Ret;
+}
+
+/**
+  Creates a 128bit random value that is fully forward and backward prediction resistant,
+  suitable for seeding a NIST SP800-90 Compliant.
+  This function takes multiple random numbers from PerformanceCounter to ensure reseeding
+  and performs AES-CBC-MAC over the data to compute the seed value.
+
+  @param[out]  SeedBuffer    Pointer to a 128bit buffer to store the random seed.
+
+  @retval TRUE        Random seed generation succeeded.
+  @retval FALSE      Failed to request random bytes.
+
+**/
+STATIC
+BOOLEAN
+EFIAPI
+RandGetSeed128 (
+  OUT UINT8        *SeedBuffer
+  )
+{
+  BOOLEAN     Ret;
+  UINT8       RandByte[16];
+  UINT8       Key[16];
+  UINT8       Ffv[16];
+  UINT8       Xored[16];
+  UINT32      Index;
+  UINT32      Index2;
+  AES_KEY     AESKey;
+
+  //
+  // Chose an arbitary key and zero the feed_forward_value (FFV)
+  //
+  for (Index = 0; Index < 16; Index++) {
+    Key[Index] = (UINT8) Index;
+    Ffv[Index] = 0;
+  }
+
+  AES_set_encrypt_key (Key, 16 * 8, &AESKey);
+
+  //
+  // Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 bit value
+  // The 10us gaps will ensure multiple reseeds within the system time with a large
+  // design margin.
+  //
+  for (Index = 0; Index < 32; Index++) {
+    MicroSecondDelay (10);
+    Ret = RandGetBytes (16, RandByte);
+    if (!Ret) {
+      return Ret;
+    }
+
+    //
+    // Perform XOR operations on two 128-bit value.
+    //
+    for (Index2 = 0; Index2 < 16; Index2++) {
+      Xored[Index2] = RandByte[Index2] ^ Ffv[Index2];
+    }
+
+    AES_encrypt (Xored, Ffv, &AESKey);
+  }
+
+  for (Index = 0; Index < 16; Index++) {
+    SeedBuffer[Index] = Ffv[Index];
+  }
+
+  return Ret;
+}
+
+/**
+  Generate high-quality entropy source.
+
+  @param[in]   Length        Size of the buffer, in bytes, to fill with.
+  @param[out]  Entropy       Pointer to the buffer to store the entropy data.
+
+  @retval EFI_SUCCESS        Entropy generation succeeded.
+  @retval EFI_NOT_READY      Failed to request random data.
+
+**/
+STATIC
+BOOLEAN
+EFIAPI
+RandGenerateEntropy (
+  IN UINTN         Length,
+  OUT UINT8        *Entropy
+  )
+{
+  BOOLEAN     Ret;
+  UINTN       BlockCount;
+  UINT8       Seed[16];
+  UINT8       *Ptr;
+
+  BlockCount = Length / 16;
+  Ptr        = (UINT8 *) Entropy;
+
+  //
+  // Generate high-quality seed for DRBG Entropy
+  //
+  while (BlockCount > 0) {
+    Ret = RandGetSeed128 (Seed);
+    if (!Ret) {
+      return Ret;
+    }
+    CopyMem (Ptr, Seed, 16);
+
+    BlockCount--;
+    Ptr = Ptr + 16;
+  }
+
+  //
+  // Populate the remained data as request.
+  //
+  Ret = RandGetSeed128 (Seed);
+  if (!Ret) {
+    return Ret;
+  }
+  CopyMem (Ptr, Seed, (Length % 16));
+
+  return Ret;
+}
+
+/*
+ * Add random bytes to the pool to acquire requested amount of entropy
+ *
+ * This function is platform specific and tries to acquire the requested
+ * amount of entropy by polling platform specific entropy sources.
+ *
+ * This is OpenSSL required interface.
+ */
+size_t rand_pool_acquire_entropy(RAND_POOL *pool)
+{
+  BOOLEAN  Ret;
+  size_t bytes_needed;
+  unsigned char * buffer;
+
+  bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
+  if (bytes_needed > 0) {
+    buffer = rand_pool_add_begin(pool, bytes_needed);
+
+    if (buffer != NULL) {
+      Ret = RandGenerateEntropy(bytes_needed, buffer);
+      if (FALSE == Ret) {
+        rand_pool_add_end(pool, 0, 0);
+      } else {
+        rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
+      }
+    }
+  }
+
+  return rand_pool_entropy_available(pool);
+}
+
+/*
+ * Implementation for UEFI
+ *
+ * This is OpenSSL required interface.
+ */
+int rand_pool_add_nonce_data(RAND_POOL *pool)
+{
+  struct {
+    UINT64  Rand;
+    UINT64  TimerValue;
+  } data = { 0 };
+
+  RandGetBytes(8, (UINT8 *)&(data.Rand));
+  data.TimerValue = GetPerformanceCounter();
+
+  return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);
+}
+
+/*
+ * Implementation for UEFI
+ *
+ * This is OpenSSL required interface.
+ */
+int rand_pool_add_additional_data(RAND_POOL *pool)
+{
+  struct {
+    UINT64  Rand;
+    UINT64  TimerValue;
+  } data = { 0 };
+
+  RandGetBytes(8, (UINT8 *)&(data.Rand));
+  data.TimerValue = GetPerformanceCounter();
+
+  return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);
+}
+
+/*
+ * Dummy Implememtation for UEFI
+ *
+ * This is OpenSSL required interface.
+ */
+int rand_pool_init(void)
+{
+  return 1;
+}
+
+/*
+ * Dummy Implememtation for UEFI
+ *
+ * This is OpenSSL required interface.
+ */
+void rand_pool_cleanup(void)
+{
+}
+
+/*
+ * Dummy Implememtation for UEFI
+ *
+ * This is OpenSSL required interface.
+ */
+void rand_pool_keep_random_devices_open(int keep)
+{
+}
+
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c b/CryptoPkg/Library/OpensslLib/rand_pool_noise.c
new file mode 100644
index 000000000000..c16ed8b45496
--- /dev/null
+++ b/CryptoPkg/Library/OpensslLib/rand_pool_noise.c
@@ -0,0 +1,29 @@
+/** @file
+  Provide rand noise source.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+
+/**
+  Get 64-bit noise source
+
+  @param[out] Rand         Buffer pointer to store 64-bit noise source
+
+  @retval FALSE            Failed to generate
+**/
+BOOLEAN
+EFIAPI
+GetRandomNoise64 (
+  OUT UINT64         *Rand
+  )
+{
+  //
+  // Return FALSE will fallback to use PerformaceCounter to
+  // generate noise.
+  //
+  return FALSE;
+}
diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c b/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c
new file mode 100644
index 000000000000..4158106231fd
--- /dev/null
+++ b/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c
@@ -0,0 +1,43 @@
+/** @file
+  Provide rand noise source.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/TimerLib.h>
+
+/**
+  Get 64-bit noise source
+
+  @param[out] Rand         Buffer pointer to store 64-bit noise source
+
+  @retval TRUE             Get randomness successfully.
+  @retval FALSE            Failed to generate
+**/
+BOOLEAN
+EFIAPI
+GetRandomNoise64 (
+  OUT UINT64         *Rand
+  )
+{
+  UINT32 Index;
+  UINT32 *RandPtr;
+
+  if (NULL == Rand) {
+    return FALSE;
+  }
+
+  RandPtr = (UINT32 *)Rand;
+
+  for (Index = 0; Index < 2; Index ++) {
+    *RandPtr = (UINT32) ((AsmReadTsc ()) & 0xFF);
+    RandPtr++;
+    MicroSecondDelay (10);
+  }
+
+  return TRUE;
+}
-- 
2.7.4


  parent reply	other threads:[~2019-05-29 10:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-29 10:40 [PATCH v5 0/9] Crypto: Upgrade OpenSSL to 1.1.1b Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 1/9] CryptoPkg/OpensslLib: Modify process_files.pl for upgrading OpenSSL Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 2/9] CryptoPkg/OpensslLib: Exclude unnecessary files in process_files.pl Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 3/9] CryptoPkg/IntrinsicLib: Fix possible unresolved external symbol issue Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 4/9] CryptoPkg/OpensslLib: Prepare for upgrading OpenSSL Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 5/9] CryptoPkg/OpensslLib: Fix cross-build problem for AARCH64 Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 6/9] CryptoPkg: Fix possible build problem with Clang Xiaoyu Lu
2019-05-29 10:40 ` Xiaoyu Lu [this message]
2019-05-29 10:40 ` [PATCH v5 8/9] CryptoPkg: Upgrade OpenSSL to 1.1.1b Xiaoyu Lu
2019-05-29 10:40 ` [PATCH v5 9/9] CryptoPkg/BaseCryptLib: Make HMAC_CTX size backward compatible Xiaoyu Lu
2019-05-29 15:39 ` [edk2-devel] [PATCH v5 0/9] Crypto: Upgrade OpenSSL to 1.1.1b Wang, Jian J
2019-06-03  0:48 ` Wang, Jian J
2019-06-03 11:45   ` Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1559126438-9211-8-git-send-email-xiaoyux.lu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox