From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 4614181F49 for ; Wed, 16 Nov 2016 18:26:19 -0800 (PST) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP; 16 Nov 2016 18:26:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,651,1473145200"; d="scan'208";a="32319493" Received: from shwdeopenpsi114.ccr.corp.intel.com ([10.239.157.135]) by fmsmga006.fm.intel.com with ESMTP; 16 Nov 2016 18:26:23 -0800 From: Dandan Bi To: edk2-devel@lists.01.org Cc: Liming Gao , Eric Dong , Jiewen Yao Date: Thu, 17 Nov 2016 10:25:55 +0800 Message-Id: <1479349555-40684-5-git-send-email-dandan.bi@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.1 In-Reply-To: <1479349555-40684-1-git-send-email-dandan.bi@intel.com> References: <1479349555-40684-1-git-send-email-dandan.bi@intel.com> Subject: [patch 4/4] MdeModulePkg/DriverSample: Use SHA256 algorithm for password encoding X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Nov 2016 02:26:19 -0000 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 Cc: Eric Dong Cc: Jiewen Yao Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Dandan Bi --- .../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.
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.
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 #include #include #include #include +#include #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.
+# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.
# # 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 #include #include #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