public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Dandan Bi <dandan.bi@intel.com>
To: edk2-devel@lists.01.org
Cc: Liming Gao <liming.gao@intel.com>,
	Eric Dong <eric.dong@intel.com>,
	Jiewen Yao <jiewen.yao@intel.com>
Subject: [patch 4/4] MdeModulePkg/DriverSample: Use SHA256 algorithm for password encoding
Date: Thu, 17 Nov 2016 10:25:55 +0800	[thread overview]
Message-ID: <1479349555-40684-5-git-send-email-dandan.bi@intel.com> (raw)
In-Reply-To: <1479349555-40684-1-git-send-email-dandan.bi@intel.com>

Use the SHA256 algorithm to hash the password
instead of using XOR operation to encode the password.
And remove the string opcode that echo the password.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
 .../Universal/DriverSampleDxe/DriverSample.c       | 171 ++++++++++++---------
 .../Universal/DriverSampleDxe/DriverSample.h       |   3 +-
 .../Universal/DriverSampleDxe/DriverSampleDxe.inf  |   4 +-
 .../Universal/DriverSampleDxe/NVDataStruc.h        |   7 +-
 MdeModulePkg/Universal/DriverSampleDxe/Vfr.vfr     |   8 -
 5 files changed, 112 insertions(+), 81 deletions(-)

diff --git a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
index 3c494e3..380226d 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
+++ b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
@@ -259,43 +259,88 @@ InternalStopMonitor(
     }
   }
   return EFI_SUCCESS;
 }
 
-
 /**
-  Encode the password using a simple algorithm.
+  Generate Salt value.
 
-  @param Password The string to be encoded.
-  @param MaxSize  The size of the string.
+  @param[in, out]   SaltValue           Points to the salt buffer, 32 bytes
 
 **/
 VOID
-EncodePassword (
-  IN  CHAR16                      *Password,
-  IN  UINTN                       MaxSize
+GenSalt (
+  IN OUT UINT8  *SaltValue
   )
 {
-  UINTN   Index;
-  UINTN   Loop;
-  CHAR16  *Buffer;
-  CHAR16  *Key;
-
-  Key     = L"MAR10648567";
-  Buffer  = AllocateZeroPool (MaxSize);
-  ASSERT (Buffer != NULL);
-
-  for (Index = 0; Key[Index] != 0; Index++) {
-    for (Loop = 0; Loop < (UINT8) (MaxSize / 2); Loop++) {
-      Buffer[Loop] = (CHAR16) (Password[Loop] ^ Key[Index]);
-    }
+  RandomSeed (NULL, 0);
+  RandomBytes (SaltValue, PASSWORD_SALT_SIZE);
+}
+
+/**
+  Hash the data.
+
+  @param[in]   Buffer         Points to the data buffer, 32 bytes
+  @param[in]   SaltValue      Points to the salt buffer, 32 bytes
+  @param[out]  EncodedDate    Points to the hashed result
+
+  @retval      TRUE           Hash the data successfully.
+  @retval      FALSE          Failed to hash the data.
+
+**/
+BOOLEAN
+HashPassword(
+  IN      UINT8               *Buffer,
+  IN      UINT8               *SaltValue,
+  OUT     UINT8               *EncodedDate
+  )
+{
+  BOOLEAN                     Status;
+  UINTN                       HashSize;
+  VOID                        *Hash;
+  VOID                        *HashData;
+
+  Hash      = NULL;
+  HashData  = NULL;
+  Status    = FALSE;
+
+  HashSize = Sha256GetContextSize ();
+  Hash     = AllocateZeroPool (HashSize);
+  ASSERT (Hash != NULL);
+  if (Hash == NULL) {
+    goto Done;
+  }
+
+  Status = Sha256Init (Hash);
+  if (!Status) {
+    goto Done;
+  }
+
+  HashData = AllocateZeroPool (PASSWORD_SALT_SIZE + 32);
+  ASSERT (HashData != NULL);
+  if (HashData == NULL) {
+    goto Done;
+  }
+
+  CopyMem (HashData, SaltValue, PASSWORD_SALT_SIZE);
+  CopyMem ((CHAR8*)HashData + PASSWORD_SALT_SIZE, Buffer, 32);
+
+  Status = Sha256Update (Hash, HashData, PASSWORD_SALT_SIZE + 32);
+  if (!Status) {
+    goto Done;
   }
 
-  CopyMem (Password, Buffer, MaxSize);
+  Status = Sha256Final (Hash, EncodedDate);
 
-  FreePool (Buffer);
-  return ;
+Done:
+  if (Hash != NULL) {
+    FreePool (Hash);
+  }
+  if (HashData != NULL) {
+    FreePool (HashData);
+  }
+  return Status;
 }
 
 /**
   Validate the user's password.
 
@@ -314,12 +359,14 @@ ValidatePassword (
   EFI_STATUS                      Status;
   UINTN                           Index;
   UINTN                           BufferSize;
   UINTN                           PasswordMaxSize;
   CHAR16                          *Password;
-  CHAR16                          *EncodedPassword;
   BOOLEAN                         OldPassword;
+  CHAR8                           AsciiPassword[32 + 1];
+  UINT8                           HashedPassword[PASSWORD_SHA256_SIZE];
+  BOOLEAN                         EncodedOk;
 
   //
   // Get encoded password first
   //
   BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
@@ -340,11 +387,11 @@ ValidatePassword (
   OldPassword = FALSE;
   PasswordMaxSize = sizeof (PrivateData->Configuration.WhatIsThePassword2);
   //
   // Check whether we have any old password set
   //
-  for (Index = 0; Index < PasswordMaxSize / sizeof (UINT16); Index++) {
+  for (Index = 0; Index < PasswordMaxSize / sizeof (UINT8); Index++) {
     if (PrivateData->Configuration.WhatIsThePassword2[Index] != 0) {
       OldPassword = TRUE;
       break;
     }
   }
@@ -360,33 +407,34 @@ ValidatePassword (
   //
   Password = HiiGetString (PrivateData->HiiHandle[0], StringId, NULL);
   if (Password == NULL) {
     return EFI_NOT_READY;
   }
-  if (StrSize (Password) > PasswordMaxSize) {
+  if ((StrLen (Password) > 32) || (Password[0] == 0)) {
     FreePool (Password);
     return EFI_NOT_READY;
   }
 
   //
-  // Validate old password
+  // Validate the input password
   //
-  EncodedPassword = AllocateZeroPool (PasswordMaxSize);
-  ASSERT (EncodedPassword != NULL);
-  StrnCpyS (EncodedPassword, PasswordMaxSize / sizeof (CHAR16), Password, StrLen (Password));
-  EncodePassword (EncodedPassword, StrLen (EncodedPassword) * sizeof (CHAR16));
-  if (CompareMem (EncodedPassword, PrivateData->Configuration.WhatIsThePassword2, PasswordMaxSize) != 0) {
+  ZeroMem (AsciiPassword, sizeof (AsciiPassword));
+  UnicodeStrToAsciiStrS (Password, AsciiPassword, sizeof (AsciiPassword));
+  ZeroMem (HashedPassword, sizeof (HashedPassword));
+
+  EncodedOk = HashPassword ((UINT8 *)AsciiPassword, PrivateData->Configuration.PasswordSalt, HashedPassword);
+  ASSERT (EncodedOk);
+  if (CompareMem (HashedPassword, PrivateData->Configuration.WhatIsThePassword2, PASSWORD_SHA256_SIZE) != 0) {
     //
     // Old password mismatch, return EFI_NOT_READY to prompt for error message
     //
     Status = EFI_NOT_READY;
   } else {
     Status = EFI_SUCCESS;
   }
-
+  ZeroMem (Password, StrSize (Password));
   FreePool (Password);
-  FreePool (EncodedPassword);
 
   return Status;
 }
 
 /**
@@ -404,15 +452,16 @@ SetPassword (
   IN DRIVER_SAMPLE_PRIVATE_DATA      *PrivateData,
   IN EFI_STRING_ID                   StringId
   )
 {
   EFI_STATUS                      Status;
-  CHAR16                          *Password;
+  UINT8                           *HashedPassword;
   CHAR16                          *TempPassword;
-  UINTN                           PasswordSize;
-  DRIVER_SAMPLE_CONFIGURATION     *Configuration;
   UINTN                           BufferSize;
+  UINT8                           *PasswordSalt;
+  CHAR8                           AsciiPassword[32 + 1];
+  BOOLEAN                         EncodedOk;
 
   //
   // Get Buffer Storage data from EFI variable
   //
   BufferSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);
@@ -425,61 +474,45 @@ SetPassword (
                   );
   if (EFI_ERROR (Status)) {
     return Status;
   }
 
+  HashedPassword = PrivateData->Configuration.WhatIsThePassword2;
+  ZeroMem (HashedPassword, sizeof (HashedPassword));
+  PasswordSalt = PrivateData->Configuration.PasswordSalt;
+  ZeroMem (PasswordSalt, sizeof (PasswordSalt));
+
   //
   // Get user input password
   //
-  Password = PrivateData->Configuration.WhatIsThePassword2;
-  PasswordSize = sizeof (PrivateData->Configuration.WhatIsThePassword2);
-  ZeroMem (Password, PasswordSize);
-
   TempPassword = HiiGetString (PrivateData->HiiHandle[0], StringId, NULL);
   if (TempPassword == NULL) {
     return EFI_NOT_READY;
   }
-  if (StrSize (TempPassword) > PasswordSize) {
+  if (StrLen(TempPassword) > 32) {
     FreePool (TempPassword);
     return EFI_NOT_READY;
   }
-  StrnCpyS (Password, PasswordSize / sizeof (CHAR16), TempPassword, StrLen (TempPassword));
-  FreePool (TempPassword);
 
   //
-  // Retrieve uncommitted data from Browser
+  // Generate Salt value.
   //
-  Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));
-  ASSERT (Configuration != NULL);
-  if (HiiGetBrowserData (&gDriverSampleFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {
-    //
-    // Update password's clear text in the screen
-    //
-    CopyMem (Configuration->PasswordClearText, Password, StrSize (Password));
-
-    //
-    // Update uncommitted data of Browser
-    //
-    HiiSetBrowserData (
-       &gDriverSampleFormSetGuid,
-       VariableName,
-       sizeof (DRIVER_SAMPLE_CONFIGURATION),
-       (UINT8 *) Configuration,
-       NULL
-       );
-  }
+  GenSalt (PasswordSalt);
 
   //
-  // Free Configuration Buffer
+  // Hash password.
   //
-  FreePool (Configuration);
-
+  ZeroMem (AsciiPassword, sizeof (AsciiPassword));
+  UnicodeStrToAsciiStrS (TempPassword, AsciiPassword, sizeof (AsciiPassword));
+  EncodedOk = HashPassword ((UINT8 *) AsciiPassword, PasswordSalt, HashedPassword);
+  ASSERT (EncodedOk);
+  ZeroMem (TempPassword, StrSize (TempPassword));
+  FreePool (TempPassword);
 
   //
-  // Set password
+  // Set the password.
   //
-  EncodePassword (Password, StrLen (Password) * 2);
   Status = gRT->SetVariable(
                   VariableName,
                   &gDriverSampleFormSetGuid,
                   EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                   sizeof (DRIVER_SAMPLE_CONFIGURATION),
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.h b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.h
index 97dee9c..32e61eb 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.h
+++ b/MdeModulePkg/Universal/DriverSampleDxe/DriverSample.h
@@ -1,8 +1,8 @@
 /** @file
 
-Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
 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
 
@@ -44,10 +44,11 @@ Revision History
 #include <Library/MemoryAllocationLib.h>
 #include <Library/HiiLib.h>
 #include <Library/DevicePathLib.h>
 #include <Library/PrintLib.h>
 #include <Library/UefiLib.h>
+#include <Library/BaseCryptLib.h>
 
 #include "NVDataStruc.h"
 
 //
 // This is the generated IFR binary data for each formset defined in VFR.
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/DriverSampleDxe.inf b/MdeModulePkg/Universal/DriverSampleDxe/DriverSampleDxe.inf
index 4233e63..144ac17 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/DriverSampleDxe.inf
+++ b/MdeModulePkg/Universal/DriverSampleDxe/DriverSampleDxe.inf
@@ -2,11 +2,11 @@
 # This is a sample HII driver. 
 #
 # This driver shows how HII protocol, VFR and UNI files are used to create a HII 
 # driver which can be dipslayed and configured by a UEFI HII Form Browser.
 #
-# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
 #
 #  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
@@ -43,14 +43,16 @@ [Sources]
   Vfr.vfr
 
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
+  CryptoPkg/CryptoPkg.dec
 
 
 [LibraryClasses]
   BaseLib
+  BaseCryptLib
   MemoryAllocationLib
   UefiBootServicesTableLib
   UefiDriverEntryPoint
   UefiRuntimeServicesTableLib
   BaseMemoryLib
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
index 17b4d99..32d5491 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
+++ b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
@@ -29,17 +29,20 @@ Revision History:
 #include <Guid/HiiFormMapMethodGuid.h>
 #include <Guid/DriverSampleHii.h>
 #include <Guid/ZeroGuid.h>
 
 #define CONFIGURATION_VARSTORE_ID    0x1234
+#define PASSWORD_SALT_SIZE 32
+#define PASSWORD_SHA256_SIZE 32
+
 
 #pragma pack(1)
 typedef struct {
   UINT16  WhatIsThePassword[20];
-  UINT16  WhatIsThePassword2[20];
+  UINT8   WhatIsThePassword2[32];
+  UINT8   PasswordSalt[PASSWORD_SALT_SIZE];
   UINT16  MyStringData[40];
-  UINT16  PasswordClearText[20];
   UINT16  SomethingHiddenForHtml;
   UINT8   HowOldAreYouInYearsManual;
   UINT16  HowTallAreYouManual;
   UINT8   HowOldAreYouInYears;
   UINT16  HowTallAreYou;
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/Vfr.vfr b/MdeModulePkg/Universal/DriverSampleDxe/Vfr.vfr
index 6e7b96b..922f2f9 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/Vfr.vfr
+++ b/MdeModulePkg/Universal/DriverSampleDxe/Vfr.vfr
@@ -466,18 +466,10 @@ formset
               help     = STRING_TOKEN(STR_PASSWORD_HELP),
               minsize  = 6,
               maxsize  = 20,
     endpassword;
 
-    string    varid    = MyIfrNVData.PasswordClearText,
-              prompt   = STRING_TOKEN(STR_MY_STRING_PROMPT),
-              help     = STRING_TOKEN(STR_MY_STRING_HELP),
-              minsize  = 6,
-              maxsize  = 0x14,
-              default  = STRING_TOKEN(STR_MY_STRING_DEFAULT),
-    endstring;
-
     //
     // Interactive password, validate via ConfigAccess.Callback()
     //
     password  varid    = MyIfrNVData.WhatIsThePassword2,
               prompt   = STRING_TOKEN(STR_PASSWORD_CALLBACK_PROMPT),
-- 
1.9.5.msysgit.1



  parent reply	other threads:[~2016-11-17  2:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-17  2:25 [patch 0/4] MdeModulePkg: Use SHA256 algorithm for password encoding Dandan Bi
2016-11-17  2:25 ` [patch 1/4] MdeModulePkg: Add BaseCryptLib/OpensslLib/IntrinsicLib Dandan Bi
2016-11-17  2:25 ` [patch 2/4] Nt32Pkg: " Dandan Bi
2016-11-17  2:34   ` Ni, Ruiyu
2016-11-17  2:25 ` [patch 3/4] EmulatorPkg: " Dandan Bi
2016-11-17  2:25 ` Dandan Bi [this message]
2016-11-17  3:28 ` [patch 0/4] MdeModulePkg: Use SHA256 algorithm for password encoding Gao, Liming
2016-11-17  3:40   ` Bi, Dandan

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=1479349555-40684-5-git-send-email-dandan.bi@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