public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16  0:59 ` [PATCH v3 1/3] " Zurcher, Christopher J
@ 2020-09-16 11:06   ` Laszlo Ersek
  2020-09-16 11:44     ` Yao, Jiewen
  0 siblings, 1 reply; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 11:06 UTC (permalink / raw)
  To: devel, christopher.j.zurcher; +Cc: Jiewen Yao, Jian J Wang, Xiaoyu Lu

Hello Christopher,

On 09/16/20 02:59, Zurcher, Christopher J wrote:
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
> 
> The EVP interface should be used in place of discrete digest function
> calls.
> 
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
> ---
>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257 ++++++++++++++++++++
>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128 ++++++++++
>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128 ++++++++++
>  9 files changed, 647 insertions(+)

I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
is necessary. (If I understand correctly, that file was missing from
your v2 posting.)

But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
superfluous. This file is never referenced in the INF files.

The point of this file would be to allow *some* of the Base / Pei /
Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
only stub implementations). But this isn't what's happening -- all of
the Base / Pei / Runtime / Smm instances are getting the real deal
("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").

(1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
should be dropped. Only the Null instance of the library needs null
versions of the new functions. The Base / Pei / Runtime / Smm instances
don't.

> 
> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> index 4aae2aba95..3968f29412 100644
> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> @@ -50,6 +50,7 @@
>    Pk/CryptAuthenticode.c
>    Pk/CryptTs.c
>    Pem/CryptPem.c
> +  Evp/CryptEvpMd.c
>  
>    SysCall/CrtWrapper.c
>    SysCall/TimerWrapper.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> index dc28e3a11d..d0b91716d0 100644
> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> @@ -57,6 +57,7 @@
>    Pk/CryptTsNull.c
>    Pem/CryptPemNull.c
>    Rand/CryptRandNull.c
> +  Evp/CryptEvpMd.c
>  
>    SysCall/CrtWrapper.c
>    SysCall/ConstantTimeClock.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> index 5005beed02..9f3accd35b 100644
> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> @@ -56,6 +56,7 @@
>    Pk/CryptAuthenticodeNull.c
>    Pk/CryptTsNull.c
>    Pem/CryptPem.c
> +  Evp/CryptEvpMd.c
>  
>    SysCall/CrtWrapper.c
>    SysCall/TimerWrapper.c
> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> index 91ec3e03bf..420623cdc6 100644
> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> @@ -54,6 +54,7 @@
>    Pk/CryptAuthenticodeNull.c
>    Pk/CryptTsNull.c
>    Pem/CryptPem.c
> +  Evp/CryptEvpMd.c
>  
>    SysCall/CrtWrapper.c
>    SysCall/ConstantTimeClock.c
> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> index 689af4fedd..542ac2e2e1 100644
> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> @@ -50,6 +50,7 @@
>    Pk/CryptTsNull.c
>    Pem/CryptPemNull.c
>    Rand/CryptRandNull.c
> +  Evp/CryptEvpMdNull.c
>  
>  [Packages]
>    MdePkg/MdePkg.dec
> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h b/CryptoPkg/Include/Library/BaseCryptLib.h
> index ae9bde9e37..5e1b408b54 100644
> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> @@ -1012,6 +1012,135 @@ HmacSha256Final (
>    OUT     UINT8  *HmacValue
>    );
>  
> +//=====================================================================================
> +//    EVP (Envelope) Primitive
> +//=====================================================================================
> +
> +/**
> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD use.
> +
> +  If DigestName is NULL, then return FALSE.
> +
> +  @param[in]    DigestName    Pointer to the digest name as a NULL-terminated ASCII string.
> +                              Valid digest names are:
> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> +                              SM3
> +
> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and initialized.
> +           If DigestName is invalid, returns NULL.
> +           If the allocations fails, returns NULL.
> +           If initialization fails, returns NULL.
> +
> +**/
> +VOID *
> +EFIAPI
> +EvpMdInit (
> +  IN  CONST CHAR8   *DigestName
> +  );
> +
> +/**
> +  Makes a copy of an existing EVP_MD context.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If NewEvpMdContext is NULL, then return FALSE.
> +
> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> +
> +  @retval TRUE   EVP_MD context copy succeeded.
> +  @retval FALSE  EVP_MD context copy failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdDuplicate (
> +  IN  CONST VOID    *EvpMdContext,
> +  OUT VOID          *NewEvpMdContext
> +  );
> +
> +/**
> +  Digests the input data and updates EVP_MD context.
> +
> +  This function performs EVP digest on a data buffer of the specified size.
> +  It can be called multiple times to compute the digest of long or discontinuous data streams.
> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and should not
> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If Data is NULL and DataSize is not zero, return FALSE.
> +
> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> +  @param[in]       Data               Pointer to the buffer containing the data to be digested.
> +  @param[in]       DataSize           Size of Data buffer in bytes.
> +
> +  @retval TRUE   EVP data digest succeeded.
> +  @retval FALSE  EVP data digest failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdUpdate (
> +  IN OUT  VOID        *EvpMdContext,
> +  IN      CONST VOID  *Data,
> +  IN      UINTN       DataSize
> +  );
> +
> +/**
> +  Completes computation of the EVP digest value.
> +  Releases the specified EVP_MD_CTX context.
> +
> +  This function completes EVP hash computation and retrieves the digest value into
> +  the specified memory. After this function has been called, the EVP context cannot
> +  be used again.
> +  EVP context should be already correctly initialized by EvpMdInit(), and should
> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is undefined.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If DigestValue is NULL, free the Context then return FALSE.
> +
> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest value.

(2) Please extend the comment on Digest with the following:

  The caller is responsible for providing enough storage for the digest
  algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
  will suffice for storing the digest regardless of the algorithm chosen
  in EvpMdInit().

(EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
advertise it to consumers in edk2.)

> +
> +  @retval TRUE   EVP digest computation succeeded.
> +  @retval FALSE  EVP digest computation failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdFinal (
> +  IN OUT  VOID   *EvpMdContext,
> +  OUT     UINT8  *DigestValue

(3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
deliberately permitted (for just freeing the context).

> +  );
> +
> +/**
> +  Computes the message digest of an input data buffer.
> +
> +  This function performs the message digest of a given data buffer, and places
> +  the digest value into the specified memory.
> +
> +  If DigestName is NULL, return FALSE.
> +  If Data is NULL and DataSize is not zero, return FALSE.
> +  If HashValue is NULL, return FALSE.
> +
> +  @param[in]    DigestName    Pointer to the digest name.
> +  @param[in]    Data          Pointer to the buffer containing the data to be hashed.
> +  @param[in]    DataSize      Size of Data buffer in bytes.
> +  @param[out]   HashValue     Pointer to a buffer that receives the digest value.
> +
> +  @retval TRUE   Digest computation succeeded.
> +  @retval FALSE  Digest computation failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdHashAll (
> +  IN  CONST CHAR8   *DigestName,
> +  IN  CONST VOID    *Data,
> +  IN  UINTN         DataSize,
> +  OUT UINT8         *HashValue
> +  );
> +
>  //=====================================================================================
>  //    Symmetric Cryptography Primitive
>  //=====================================================================================
> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> new file mode 100644
> index 0000000000..b2770a9186
> --- /dev/null
> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> @@ -0,0 +1,257 @@
> +/** @file
> +  EVP MD Wrapper Implementation for OpenSSL.
> +
> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "InternalCryptLib.h"
> +#include <openssl/evp.h>
> +
> +/**
> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD use.
> +
> +  If DigestName is NULL, then return FALSE.
> +
> +  @param[in]    DigestName    Pointer to the digest name as a NULL-terminated ASCII string.
> +                              Valid digest names are:
> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> +                              SM3
> +
> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and initialized.
> +           If DigestName is invalid, returns NULL.
> +           If the allocations fails, returns NULL.
> +           If initialization fails, returns NULL.
> +
> +**/
> +VOID *
> +EFIAPI
> +EvpMdInit (
> +  IN  CONST CHAR8   *DigestName
> +  )
> +{
> +  EVP_MD    *Digest;
> +  VOID      *EvpMdContext;
> +
> +  //
> +  // Check input parameters.
> +  //
> +  if (DigestName == NULL) {
> +    return NULL;
> +  }
> +
> +  //
> +  // Allocate EVP_MD_CTX Context
> +  //
> +  EvpMdContext = EVP_MD_CTX_new ();
> +  if (EvpMdContext == NULL) {
> +    return NULL;
> +  }
> +
> +  Digest = EVP_get_digestbyname (DigestName);

I think this may not compile with gcc (and correctly so). The pointer
returned by EVP_get_digestbyname() is const-qualified, but with the
assignment, we're throwing away the const-ness.

(4) Please const-qualify the "Digest" local pointer.

> +  if (Digest == NULL) {
> +    return NULL;
> +  }

(5) This is a memory leak I believe; "EvpMdContext" is leaked.

For keeping the control flow simple, consider moving
EVP_get_digestbyname() above EVP_MD_CTX_new().

> +
> +  //
> +  // Initialize Context
> +  //
> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
> +    EVP_MD_CTX_free (EvpMdContext);
> +    return NULL;
> +  }
> +
> +  return EvpMdContext;
> +}
> +
> +/**
> +  Makes a copy of an existing EVP_MD context.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If NewEvpMdContext is NULL, then return FALSE.
> +
> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> +
> +  @retval TRUE   EVP_MD context copy succeeded.
> +  @retval FALSE  EVP_MD context copy failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdDuplicate (
> +  IN  CONST VOID    *EvpMdContext,
> +  OUT VOID          *NewEvpMdContext
> +  )
> +{
> +  //
> +  // Check input parameters.
> +  //
> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
> +    return FALSE;
> +  }
> +
> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
> +    return FALSE;
> +  }
> +
> +  return TRUE;
> +}

(6) Can you please confirm that the caller is supposed to initialize
"NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?

> +
> +/**
> +  Digests the input data and updates EVP_MD context.
> +
> +  This function performs EVP digest on a data buffer of the specified size.
> +  It can be called multiple times to compute the digest of long or discontinuous data streams.
> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and should not
> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If Data is NULL and DataSize is not zero, return FALSE.
> +
> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> +  @param[in]       Data               Pointer to the buffer containing the data to be digested.
> +  @param[in]       DataSize           Size of Data buffer in bytes.
> +
> +  @retval TRUE   EVP data digest succeeded.
> +  @retval FALSE  EVP data digest failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdUpdate (
> +  IN OUT  VOID        *EvpMdContext,
> +  IN      CONST VOID  *Data,
> +  IN      UINTN       DataSize
> +  )
> +{
> +  //
> +  // Check input parameters.
> +  //
> +  if (EvpMdContext == NULL) {
> +    return FALSE;
> +  }
> +
> +  //
> +  // Check invalid parameters, in case only DataLength was checked in OpenSSL
> +  //
> +  if (Data == NULL && DataSize != 0) {
> +    return FALSE;
> +  }
> +
> +  //
> +  // OpenSSL EVP digest update
> +  //
> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
> +    return FALSE;
> +  }
> +
> +  return TRUE;
> +}
> +
> +/**
> +  Completes computation of the EVP digest value.
> +  Releases the specified EVP_MD_CTX context.
> +
> +  This function completes EVP hash computation and retrieves the digest value into
> +  the specified memory. After this function has been called, the EVP context cannot
> +  be used again.
> +  EVP context should be already correctly initialized by EvpMdInit(), and should
> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is undefined.
> +
> +  If EvpMdContext is NULL, then return FALSE.
> +  If DigestValue is NULL, free the Context then return FALSE.
> +
> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest value.
> +
> +  @retval TRUE   EVP digest computation succeeded.
> +  @retval FALSE  EVP digest computation failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdFinal (
> +  IN OUT  VOID   *EvpMdContext,
> +  OUT     UINT8  *DigestValue
> +  )
> +{
> +  UINT32    Length;
> +  BOOLEAN   ReturnValue;
> +
> +  ReturnValue = TRUE;
> +
> +  //
> +  // Check input parameters.
> +  //
> +  if (EvpMdContext == NULL) {
> +    return FALSE;
> +  }
> +  if (DigestValue == NULL) {
> +    EVP_MD_CTX_free (EvpMdContext);
> +    return FALSE;
> +  }
> +
> +  //
> +  // OpenSSL EVP digest finalization
> +  //
> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
> +    ReturnValue = FALSE;
> +  }


(7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
deals fine with the third parameter being NULL, according to the docs
(and the code).


> +
> +  //
> +  // Free OpenSSL EVP_MD_CTX Context
> +  //
> +  EVP_MD_CTX_free (EvpMdContext);
> +
> +  return ReturnValue;
> +}
> +
> +/**
> +  Computes the message digest of an input data buffer.
> +
> +  This function performs the message digest of a given data buffer, and places
> +  the digest value into the specified memory.
> +
> +  If DigestName is NULL, return FALSE.
> +  If Data is NULL and DataSize is not zero, return FALSE.
> +  If HashValue is NULL, return FALSE.
> +
> +  @param[in]    DigestName    Pointer to the digest name.
> +  @param[in]    Data          Pointer to the buffer containing the data to be hashed.
> +  @param[in]    DataSize      Size of Data buffer in bytes.
> +  @param[out]   HashValue     Pointer to a buffer that receives the digest value.
> +
> +  @retval TRUE   Digest computation succeeded.
> +  @retval FALSE  Digest computation failed.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdHashAll (
> +  IN  CONST CHAR8   *DigestName,
> +  IN  CONST VOID    *Data,
> +  IN  UINTN         DataSize,
> +  OUT UINT8         *HashValue
> +  )
> +{
> +  BOOLEAN   Result;
> +  VOID      *EvpMdContext;
> +
> +  EvpMdContext = EvpMdInit (DigestName);
> +  if (EvpMdContext == NULL) {
> +    return FALSE;
> +  }
> +
> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
> +  if (Result == FALSE) {

(8) Style: please write (!Result).


> +    EvpMdFinal (EvpMdContext, NULL);
> +    return FALSE;
> +  }
> +
> +  Result = EvpMdFinal (EvpMdContext, HashValue);
> +
> +  return Result;
> +}
> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> new file mode 100644
> index 0000000000..038f63801f
> --- /dev/null
> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> @@ -0,0 +1,128 @@
> +/** @file
> +  EVP MD Wrapper Null Library.
> +
> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "InternalCryptLib.h"
> +
> +/**
> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD use.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]    DigestName    Pointer to the digest name as a NULL-terminated ASCII string.
> +                              Valid digest names are:
> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> +                              SM3
> +
> +  @return NULL  This interface is not supported.
> +
> +**/
> +VOID *
> +EFIAPI
> +EvpMdInit (
> +  IN  CONST CHAR8   *DigestName
> +  )
> +{
> +  ASSERT (FALSE);
> +  return NULL;
> +}
> +
> +/**
> +  Makes a copy of an existing EVP_MD context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdDuplicate (
> +  IN  CONST VOID    *EvpMdContext,
> +  OUT VOID          *NewEvpMdContext
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Digests the input data and updates EVP_MD context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> +  @param[in]       Data               Pointer to the buffer containing the data to be digested.
> +  @param[in]       DataSize           Size of Data buffer in bytes.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdUpdate (
> +  IN OUT  VOID        *EvpMdContext,
> +  IN      CONST VOID  *Data,
> +  IN      UINTN       DataSize
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Completes computation of the EVP digest value.
> +  Releases the specified EVP_MD_CTX context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest value.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdFinal (
> +  IN OUT  VOID   *EvpMdContext,
> +  OUT     UINT8  *DigestValue
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Computes the message digest of an input data buffer.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]    DigestName    Pointer to the digest name.
> +  @param[in]    Data          Pointer to the buffer containing the data to be hashed.
> +  @param[in]    DataSize      Size of Data buffer in bytes.
> +  @param[out]   HashValue     Pointer to a buffer that receives the digest value.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdHashAll (
> +  IN  CONST CHAR8   *DigestName,
> +  IN  CONST VOID    *Data,
> +  IN  UINTN         DataSize,
> +  OUT UINT8         *HashValue
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> new file mode 100644
> index 0000000000..038f63801f
> --- /dev/null
> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> @@ -0,0 +1,128 @@
> +/** @file
> +  EVP MD Wrapper Null Library.
> +
> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "InternalCryptLib.h"
> +
> +/**
> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD use.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]    DigestName    Pointer to the digest name as a NULL-terminated ASCII string.
> +                              Valid digest names are:
> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> +                              SM3
> +
> +  @return NULL  This interface is not supported.
> +
> +**/
> +VOID *
> +EFIAPI
> +EvpMdInit (
> +  IN  CONST CHAR8   *DigestName
> +  )
> +{
> +  ASSERT (FALSE);
> +  return NULL;
> +}
> +
> +/**
> +  Makes a copy of an existing EVP_MD context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdDuplicate (
> +  IN  CONST VOID    *EvpMdContext,
> +  OUT VOID          *NewEvpMdContext
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Digests the input data and updates EVP_MD context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> +  @param[in]       Data               Pointer to the buffer containing the data to be digested.
> +  @param[in]       DataSize           Size of Data buffer in bytes.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdUpdate (
> +  IN OUT  VOID        *EvpMdContext,
> +  IN      CONST VOID  *Data,
> +  IN      UINTN       DataSize
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Completes computation of the EVP digest value.
> +  Releases the specified EVP_MD_CTX context.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest value.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdFinal (
> +  IN OUT  VOID   *EvpMdContext,
> +  OUT     UINT8  *DigestValue
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> +
> +/**
> +  Computes the message digest of an input data buffer.
> +
> +  Return FALSE to indicate this interface is not supported.
> +
> +  @param[in]    DigestName    Pointer to the digest name.
> +  @param[in]    Data          Pointer to the buffer containing the data to be hashed.
> +  @param[in]    DataSize      Size of Data buffer in bytes.
> +  @param[out]   HashValue     Pointer to a buffer that receives the digest value.
> +
> +  @retval FALSE  This interface is not supported.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +EvpMdHashAll (
> +  IN  CONST CHAR8   *DigestName,
> +  IN  CONST VOID    *Data,
> +  IN  UINTN         DataSize,
> +  OUT UINT8         *HashValue
> +  )
> +{
> +  ASSERT (FALSE);
> +  return FALSE;
> +}
> 

Thanks,
Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 11:06   ` [edk2-devel] " Laszlo Ersek
@ 2020-09-16 11:44     ` Yao, Jiewen
  2020-09-16 13:35       ` Laszlo Ersek
  0 siblings, 1 reply; 13+ messages in thread
From: Yao, Jiewen @ 2020-09-16 11:44 UTC (permalink / raw)
  To: Laszlo Ersek, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

Hi Laszlo
I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
If we need follow this, then we need add OPTIONAL to almost all pointer, which is unnecessary.

> +BOOLEAN
> +EFIAPI
> +EvpMdFinal (
> +  IN OUT  VOID   *EvpMdContext,
> +  OUT     UINT8  *DigestValue

(3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
deliberately permitted (for just freeing the context).




> -----Original Message-----
> From: Laszlo Ersek <lersek@redhat.com>
> Sent: Wednesday, September 16, 2020 7:07 PM
> To: devel@edk2.groups.io; Zurcher, Christopher J
> <christopher.j.zurcher@intel.com>
> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J <jian.j.wang@intel.com>;
> Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> 
> Hello Christopher,
> 
> On 09/16/20 02:59, Zurcher, Christopher J wrote:
> > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
> >
> > The EVP interface should be used in place of discrete digest function
> > calls.
> >
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
> > Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
> > ---
> >  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
> >  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
> >  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
> >  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
> >  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
> >  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
> >  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
> ++++++++++++++++++++
> >  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128 ++++++++++
> >  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128 ++++++++++
> >  9 files changed, 647 insertions(+)
> 
> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
> is necessary. (If I understand correctly, that file was missing from
> your v2 posting.)
> 
> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
> superfluous. This file is never referenced in the INF files.
> 
> The point of this file would be to allow *some* of the Base / Pei /
> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
> only stub implementations). But this isn't what's happening -- all of
> the Base / Pei / Runtime / Smm instances are getting the real deal
> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
> 
> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
> should be dropped. Only the Null instance of the library needs null
> versions of the new functions. The Base / Pei / Runtime / Smm instances
> don't.
> 
> >
> > diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > index 4aae2aba95..3968f29412 100644
> > --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > @@ -50,6 +50,7 @@
> >    Pk/CryptAuthenticode.c
> >    Pk/CryptTs.c
> >    Pem/CryptPem.c
> > +  Evp/CryptEvpMd.c
> >
> >    SysCall/CrtWrapper.c
> >    SysCall/TimerWrapper.c
> > diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > index dc28e3a11d..d0b91716d0 100644
> > --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > @@ -57,6 +57,7 @@
> >    Pk/CryptTsNull.c
> >    Pem/CryptPemNull.c
> >    Rand/CryptRandNull.c
> > +  Evp/CryptEvpMd.c
> >
> >    SysCall/CrtWrapper.c
> >    SysCall/ConstantTimeClock.c
> > diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > index 5005beed02..9f3accd35b 100644
> > --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > @@ -56,6 +56,7 @@
> >    Pk/CryptAuthenticodeNull.c
> >    Pk/CryptTsNull.c
> >    Pem/CryptPem.c
> > +  Evp/CryptEvpMd.c
> >
> >    SysCall/CrtWrapper.c
> >    SysCall/TimerWrapper.c
> > diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > index 91ec3e03bf..420623cdc6 100644
> > --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > @@ -54,6 +54,7 @@
> >    Pk/CryptAuthenticodeNull.c
> >    Pk/CryptTsNull.c
> >    Pem/CryptPem.c
> > +  Evp/CryptEvpMd.c
> >
> >    SysCall/CrtWrapper.c
> >    SysCall/ConstantTimeClock.c
> > diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > index 689af4fedd..542ac2e2e1 100644
> > --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > @@ -50,6 +50,7 @@
> >    Pk/CryptTsNull.c
> >    Pem/CryptPemNull.c
> >    Rand/CryptRandNull.c
> > +  Evp/CryptEvpMdNull.c
> >
> >  [Packages]
> >    MdePkg/MdePkg.dec
> > diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
> b/CryptoPkg/Include/Library/BaseCryptLib.h
> > index ae9bde9e37..5e1b408b54 100644
> > --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> > +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> > @@ -1012,6 +1012,135 @@ HmacSha256Final (
> >    OUT     UINT8  *HmacValue
> >    );
> >
> >
> +//==============================================================
> =======================
> > +//    EVP (Envelope) Primitive
> >
> +//==============================================================
> =======================
> > +
> > +/**
> > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> use.
> > +
> > +  If DigestName is NULL, then return FALSE.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> terminated ASCII string.
> > +                              Valid digest names are:
> > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > +                              SM3
> > +
> > +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> initialized.
> > +           If DigestName is invalid, returns NULL.
> > +           If the allocations fails, returns NULL.
> > +           If initialization fails, returns NULL.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +EvpMdInit (
> > +  IN  CONST CHAR8   *DigestName
> > +  );
> > +
> > +/**
> > +  Makes a copy of an existing EVP_MD context.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If NewEvpMdContext is NULL, then return FALSE.
> > +
> > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > +
> > +  @retval TRUE   EVP_MD context copy succeeded.
> > +  @retval FALSE  EVP_MD context copy failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdDuplicate (
> > +  IN  CONST VOID    *EvpMdContext,
> > +  OUT VOID          *NewEvpMdContext
> > +  );
> > +
> > +/**
> > +  Digests the input data and updates EVP_MD context.
> > +
> > +  This function performs EVP digest on a data buffer of the specified size.
> > +  It can be called multiple times to compute the digest of long or
> discontinuous data streams.
> > +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
> should not
> > +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If Data is NULL and DataSize is not zero, return FALSE.
> > +
> > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > +  @param[in]       Data               Pointer to the buffer containing the data to be
> digested.
> > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > +
> > +  @retval TRUE   EVP data digest succeeded.
> > +  @retval FALSE  EVP data digest failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdUpdate (
> > +  IN OUT  VOID        *EvpMdContext,
> > +  IN      CONST VOID  *Data,
> > +  IN      UINTN       DataSize
> > +  );
> > +
> > +/**
> > +  Completes computation of the EVP digest value.
> > +  Releases the specified EVP_MD_CTX context.
> > +
> > +  This function completes EVP hash computation and retrieves the digest
> value into
> > +  the specified memory. After this function has been called, the EVP context
> cannot
> > +  be used again.
> > +  EVP context should be already correctly initialized by EvpMdInit(), and
> should
> > +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> undefined.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If DigestValue is NULL, free the Context then return FALSE.
> > +
> > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
> value.
> 
> (2) Please extend the comment on Digest with the following:
> 
>   The caller is responsible for providing enough storage for the digest
>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
>   will suffice for storing the digest regardless of the algorithm chosen
>   in EvpMdInit().
> 
> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
> advertise it to consumers in edk2.)
> 
> > +
> > +  @retval TRUE   EVP digest computation succeeded.
> > +  @retval FALSE  EVP digest computation failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdFinal (
> > +  IN OUT  VOID   *EvpMdContext,
> > +  OUT     UINT8  *DigestValue
> 
> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> deliberately permitted (for just freeing the context).
> 
> > +  );
> > +
> > +/**
> > +  Computes the message digest of an input data buffer.
> > +
> > +  This function performs the message digest of a given data buffer, and places
> > +  the digest value into the specified memory.
> > +
> > +  If DigestName is NULL, return FALSE.
> > +  If Data is NULL and DataSize is not zero, return FALSE.
> > +  If HashValue is NULL, return FALSE.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name.
> > +  @param[in]    Data          Pointer to the buffer containing the data to be
> hashed.
> > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> value.
> > +
> > +  @retval TRUE   Digest computation succeeded.
> > +  @retval FALSE  Digest computation failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdHashAll (
> > +  IN  CONST CHAR8   *DigestName,
> > +  IN  CONST VOID    *Data,
> > +  IN  UINTN         DataSize,
> > +  OUT UINT8         *HashValue
> > +  );
> > +
> >
> //===============================================================
> ======================
> >  //    Symmetric Cryptography Primitive
> >
> //===============================================================
> ======================
> > diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> > new file mode 100644
> > index 0000000000..b2770a9186
> > --- /dev/null
> > +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> > @@ -0,0 +1,257 @@
> > +/** @file
> > +  EVP MD Wrapper Implementation for OpenSSL.
> > +
> > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "InternalCryptLib.h"
> > +#include <openssl/evp.h>
> > +
> > +/**
> > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> use.
> > +
> > +  If DigestName is NULL, then return FALSE.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> terminated ASCII string.
> > +                              Valid digest names are:
> > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > +                              SM3
> > +
> > +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> initialized.
> > +           If DigestName is invalid, returns NULL.
> > +           If the allocations fails, returns NULL.
> > +           If initialization fails, returns NULL.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +EvpMdInit (
> > +  IN  CONST CHAR8   *DigestName
> > +  )
> > +{
> > +  EVP_MD    *Digest;
> > +  VOID      *EvpMdContext;
> > +
> > +  //
> > +  // Check input parameters.
> > +  //
> > +  if (DigestName == NULL) {
> > +    return NULL;
> > +  }
> > +
> > +  //
> > +  // Allocate EVP_MD_CTX Context
> > +  //
> > +  EvpMdContext = EVP_MD_CTX_new ();
> > +  if (EvpMdContext == NULL) {
> > +    return NULL;
> > +  }
> > +
> > +  Digest = EVP_get_digestbyname (DigestName);
> 
> I think this may not compile with gcc (and correctly so). The pointer
> returned by EVP_get_digestbyname() is const-qualified, but with the
> assignment, we're throwing away the const-ness.
> 
> (4) Please const-qualify the "Digest" local pointer.
> 
> > +  if (Digest == NULL) {
> > +    return NULL;
> > +  }
> 
> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
> 
> For keeping the control flow simple, consider moving
> EVP_get_digestbyname() above EVP_MD_CTX_new().
> 
> > +
> > +  //
> > +  // Initialize Context
> > +  //
> > +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
> > +    EVP_MD_CTX_free (EvpMdContext);
> > +    return NULL;
> > +  }
> > +
> > +  return EvpMdContext;
> > +}
> > +
> > +/**
> > +  Makes a copy of an existing EVP_MD context.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If NewEvpMdContext is NULL, then return FALSE.
> > +
> > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > +
> > +  @retval TRUE   EVP_MD context copy succeeded.
> > +  @retval FALSE  EVP_MD context copy failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdDuplicate (
> > +  IN  CONST VOID    *EvpMdContext,
> > +  OUT VOID          *NewEvpMdContext
> > +  )
> > +{
> > +  //
> > +  // Check input parameters.
> > +  //
> > +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
> > +    return FALSE;
> > +  }
> > +
> > +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
> > +    return FALSE;
> > +  }
> > +
> > +  return TRUE;
> > +}
> 
> (6) Can you please confirm that the caller is supposed to initialize
> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
> 
> > +
> > +/**
> > +  Digests the input data and updates EVP_MD context.
> > +
> > +  This function performs EVP digest on a data buffer of the specified size.
> > +  It can be called multiple times to compute the digest of long or
> discontinuous data streams.
> > +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
> should not
> > +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If Data is NULL and DataSize is not zero, return FALSE.
> > +
> > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > +  @param[in]       Data               Pointer to the buffer containing the data to be
> digested.
> > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > +
> > +  @retval TRUE   EVP data digest succeeded.
> > +  @retval FALSE  EVP data digest failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdUpdate (
> > +  IN OUT  VOID        *EvpMdContext,
> > +  IN      CONST VOID  *Data,
> > +  IN      UINTN       DataSize
> > +  )
> > +{
> > +  //
> > +  // Check input parameters.
> > +  //
> > +  if (EvpMdContext == NULL) {
> > +    return FALSE;
> > +  }
> > +
> > +  //
> > +  // Check invalid parameters, in case only DataLength was checked in
> OpenSSL
> > +  //
> > +  if (Data == NULL && DataSize != 0) {
> > +    return FALSE;
> > +  }
> > +
> > +  //
> > +  // OpenSSL EVP digest update
> > +  //
> > +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
> > +    return FALSE;
> > +  }
> > +
> > +  return TRUE;
> > +}
> > +
> > +/**
> > +  Completes computation of the EVP digest value.
> > +  Releases the specified EVP_MD_CTX context.
> > +
> > +  This function completes EVP hash computation and retrieves the digest
> value into
> > +  the specified memory. After this function has been called, the EVP context
> cannot
> > +  be used again.
> > +  EVP context should be already correctly initialized by EvpMdInit(), and
> should
> > +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> undefined.
> > +
> > +  If EvpMdContext is NULL, then return FALSE.
> > +  If DigestValue is NULL, free the Context then return FALSE.
> > +
> > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
> value.
> > +
> > +  @retval TRUE   EVP digest computation succeeded.
> > +  @retval FALSE  EVP digest computation failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdFinal (
> > +  IN OUT  VOID   *EvpMdContext,
> > +  OUT     UINT8  *DigestValue
> > +  )
> > +{
> > +  UINT32    Length;
> > +  BOOLEAN   ReturnValue;
> > +
> > +  ReturnValue = TRUE;
> > +
> > +  //
> > +  // Check input parameters.
> > +  //
> > +  if (EvpMdContext == NULL) {
> > +    return FALSE;
> > +  }
> > +  if (DigestValue == NULL) {
> > +    EVP_MD_CTX_free (EvpMdContext);
> > +    return FALSE;
> > +  }
> > +
> > +  //
> > +  // OpenSSL EVP digest finalization
> > +  //
> > +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
> > +    ReturnValue = FALSE;
> > +  }
> 
> 
> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
> deals fine with the third parameter being NULL, according to the docs
> (and the code).
> 
> 
> > +
> > +  //
> > +  // Free OpenSSL EVP_MD_CTX Context
> > +  //
> > +  EVP_MD_CTX_free (EvpMdContext);
> > +
> > +  return ReturnValue;
> > +}
> > +
> > +/**
> > +  Computes the message digest of an input data buffer.
> > +
> > +  This function performs the message digest of a given data buffer, and places
> > +  the digest value into the specified memory.
> > +
> > +  If DigestName is NULL, return FALSE.
> > +  If Data is NULL and DataSize is not zero, return FALSE.
> > +  If HashValue is NULL, return FALSE.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name.
> > +  @param[in]    Data          Pointer to the buffer containing the data to be
> hashed.
> > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> value.
> > +
> > +  @retval TRUE   Digest computation succeeded.
> > +  @retval FALSE  Digest computation failed.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdHashAll (
> > +  IN  CONST CHAR8   *DigestName,
> > +  IN  CONST VOID    *Data,
> > +  IN  UINTN         DataSize,
> > +  OUT UINT8         *HashValue
> > +  )
> > +{
> > +  BOOLEAN   Result;
> > +  VOID      *EvpMdContext;
> > +
> > +  EvpMdContext = EvpMdInit (DigestName);
> > +  if (EvpMdContext == NULL) {
> > +    return FALSE;
> > +  }
> > +
> > +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
> > +  if (Result == FALSE) {
> 
> (8) Style: please write (!Result).
> 
> 
> > +    EvpMdFinal (EvpMdContext, NULL);
> > +    return FALSE;
> > +  }
> > +
> > +  Result = EvpMdFinal (EvpMdContext, HashValue);
> > +
> > +  return Result;
> > +}
> > diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> > new file mode 100644
> > index 0000000000..038f63801f
> > --- /dev/null
> > +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> > @@ -0,0 +1,128 @@
> > +/** @file
> > +  EVP MD Wrapper Null Library.
> > +
> > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "InternalCryptLib.h"
> > +
> > +/**
> > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> use.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> terminated ASCII string.
> > +                              Valid digest names are:
> > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > +                              SM3
> > +
> > +  @return NULL  This interface is not supported.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +EvpMdInit (
> > +  IN  CONST CHAR8   *DigestName
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Makes a copy of an existing EVP_MD context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdDuplicate (
> > +  IN  CONST VOID    *EvpMdContext,
> > +  OUT VOID          *NewEvpMdContext
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Digests the input data and updates EVP_MD context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > +  @param[in]       Data               Pointer to the buffer containing the data to be
> digested.
> > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdUpdate (
> > +  IN OUT  VOID        *EvpMdContext,
> > +  IN      CONST VOID  *Data,
> > +  IN      UINTN       DataSize
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Completes computation of the EVP digest value.
> > +  Releases the specified EVP_MD_CTX context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
> value.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdFinal (
> > +  IN OUT  VOID   *EvpMdContext,
> > +  OUT     UINT8  *DigestValue
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Computes the message digest of an input data buffer.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name.
> > +  @param[in]    Data          Pointer to the buffer containing the data to be
> hashed.
> > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> value.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdHashAll (
> > +  IN  CONST CHAR8   *DigestName,
> > +  IN  CONST VOID    *Data,
> > +  IN  UINTN         DataSize,
> > +  OUT UINT8         *HashValue
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> > new file mode 100644
> > index 0000000000..038f63801f
> > --- /dev/null
> > +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> > @@ -0,0 +1,128 @@
> > +/** @file
> > +  EVP MD Wrapper Null Library.
> > +
> > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "InternalCryptLib.h"
> > +
> > +/**
> > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> use.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> terminated ASCII string.
> > +                              Valid digest names are:
> > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > +                              SM3
> > +
> > +  @return NULL  This interface is not supported.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +EvpMdInit (
> > +  IN  CONST CHAR8   *DigestName
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Makes a copy of an existing EVP_MD context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdDuplicate (
> > +  IN  CONST VOID    *EvpMdContext,
> > +  OUT VOID          *NewEvpMdContext
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Digests the input data and updates EVP_MD context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > +  @param[in]       Data               Pointer to the buffer containing the data to be
> digested.
> > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdUpdate (
> > +  IN OUT  VOID        *EvpMdContext,
> > +  IN      CONST VOID  *Data,
> > +  IN      UINTN       DataSize
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Completes computation of the EVP digest value.
> > +  Releases the specified EVP_MD_CTX context.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
> value.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdFinal (
> > +  IN OUT  VOID   *EvpMdContext,
> > +  OUT     UINT8  *DigestValue
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Computes the message digest of an input data buffer.
> > +
> > +  Return FALSE to indicate this interface is not supported.
> > +
> > +  @param[in]    DigestName    Pointer to the digest name.
> > +  @param[in]    Data          Pointer to the buffer containing the data to be
> hashed.
> > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> value.
> > +
> > +  @retval FALSE  This interface is not supported.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdHashAll (
> > +  IN  CONST CHAR8   *DigestName,
> > +  IN  CONST VOID    *Data,
> > +  IN  UINTN         DataSize,
> > +  OUT UINT8         *HashValue
> > +  )
> > +{
> > +  ASSERT (FALSE);
> > +  return FALSE;
> > +}
> >
> 
> Thanks,
> Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
@ 2020-09-16 12:07 Yao, Jiewen
  2020-09-16 12:39 ` Laszlo Ersek
  0 siblings, 1 reply; 13+ messages in thread
From: Yao, Jiewen @ 2020-09-16 12:07 UTC (permalink / raw)
  To: devel@edk2.groups.io, Yao, Jiewen, Laszlo Ersek,
	Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

I overlooked the behavior that NULL DigestValue will take side effect to free Context.

Hi Christopher
May I understand why we need free the context?
This does not align with other existing HASH or HMAC functions.

Thank you
Yao Jiewen

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yao, Jiewen
> Sent: Wednesday, September 16, 2020 7:45 PM
> To: Laszlo Ersek <lersek@redhat.com>; devel@edk2.groups.io; Zurcher,
> Christopher J <christopher.j.zurcher@intel.com>
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> 
> Hi Laszlo
> I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
> If we need follow this, then we need add OPTIONAL to almost all pointer, which
> is unnecessary.
> 
> > +BOOLEAN
> > +EFIAPI
> > +EvpMdFinal (
> > +  IN OUT  VOID   *EvpMdContext,
> > +  OUT     UINT8  *DigestValue
> 
> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> deliberately permitted (for just freeing the context).
> 
> 
> 
> 
> > -----Original Message-----
> > From: Laszlo Ersek <lersek@redhat.com>
> > Sent: Wednesday, September 16, 2020 7:07 PM
> > To: devel@edk2.groups.io; Zurcher, Christopher J
> > <christopher.j.zurcher@intel.com>
> > Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>;
> > Lu, XiaoyuX <xiaoyux.lu@intel.com>
> > Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> > (Envelope) Digest interface
> >
> > Hello Christopher,
> >
> > On 09/16/20 02:59, Zurcher, Christopher J wrote:
> > > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
> > >
> > > The EVP interface should be used in place of discrete digest function
> > > calls.
> > >
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
> > > Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
> > > ---
> > >  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
> > >  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
> > >  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
> > >  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
> > >  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
> > >  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
> > >  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
> > ++++++++++++++++++++
> > >  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128 ++++++++++
> > >  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128
> ++++++++++
> > >  9 files changed, 647 insertions(+)
> >
> > I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
> > is necessary. (If I understand correctly, that file was missing from
> > your v2 posting.)
> >
> > But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
> > superfluous. This file is never referenced in the INF files.
> >
> > The point of this file would be to allow *some* of the Base / Pei /
> > Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
> > only stub implementations). But this isn't what's happening -- all of
> > the Base / Pei / Runtime / Smm instances are getting the real deal
> > ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
> >
> > (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
> > should be dropped. Only the Null instance of the library needs null
> > versions of the new functions. The Base / Pei / Runtime / Smm instances
> > don't.
> >
> > >
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > > index 4aae2aba95..3968f29412 100644
> > > --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > > +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> > > @@ -50,6 +50,7 @@
> > >    Pk/CryptAuthenticode.c
> > >    Pk/CryptTs.c
> > >    Pem/CryptPem.c
> > > +  Evp/CryptEvpMd.c
> > >
> > >    SysCall/CrtWrapper.c
> > >    SysCall/TimerWrapper.c
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > > index dc28e3a11d..d0b91716d0 100644
> > > --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > > +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> > > @@ -57,6 +57,7 @@
> > >    Pk/CryptTsNull.c
> > >    Pem/CryptPemNull.c
> > >    Rand/CryptRandNull.c
> > > +  Evp/CryptEvpMd.c
> > >
> > >    SysCall/CrtWrapper.c
> > >    SysCall/ConstantTimeClock.c
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > > index 5005beed02..9f3accd35b 100644
> > > --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > > +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> > > @@ -56,6 +56,7 @@
> > >    Pk/CryptAuthenticodeNull.c
> > >    Pk/CryptTsNull.c
> > >    Pem/CryptPem.c
> > > +  Evp/CryptEvpMd.c
> > >
> > >    SysCall/CrtWrapper.c
> > >    SysCall/TimerWrapper.c
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > > index 91ec3e03bf..420623cdc6 100644
> > > --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > > +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> > > @@ -54,6 +54,7 @@
> > >    Pk/CryptAuthenticodeNull.c
> > >    Pk/CryptTsNull.c
> > >    Pem/CryptPem.c
> > > +  Evp/CryptEvpMd.c
> > >
> > >    SysCall/CrtWrapper.c
> > >    SysCall/ConstantTimeClock.c
> > > diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > > index 689af4fedd..542ac2e2e1 100644
> > > --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > > +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> > > @@ -50,6 +50,7 @@
> > >    Pk/CryptTsNull.c
> > >    Pem/CryptPemNull.c
> > >    Rand/CryptRandNull.c
> > > +  Evp/CryptEvpMdNull.c
> > >
> > >  [Packages]
> > >    MdePkg/MdePkg.dec
> > > diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
> > b/CryptoPkg/Include/Library/BaseCryptLib.h
> > > index ae9bde9e37..5e1b408b54 100644
> > > --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> > > +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> > > @@ -1012,6 +1012,135 @@ HmacSha256Final (
> > >    OUT     UINT8  *HmacValue
> > >    );
> > >
> > >
> >
> +//==============================================================
> > =======================
> > > +//    EVP (Envelope) Primitive
> > >
> >
> +//==============================================================
> > =======================
> > > +
> > > +/**
> > > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> > use.
> > > +
> > > +  If DigestName is NULL, then return FALSE.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> > terminated ASCII string.
> > > +                              Valid digest names are:
> > > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > > +                              SM3
> > > +
> > > +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> > initialized.
> > > +           If DigestName is invalid, returns NULL.
> > > +           If the allocations fails, returns NULL.
> > > +           If initialization fails, returns NULL.
> > > +
> > > +**/
> > > +VOID *
> > > +EFIAPI
> > > +EvpMdInit (
> > > +  IN  CONST CHAR8   *DigestName
> > > +  );
> > > +
> > > +/**
> > > +  Makes a copy of an existing EVP_MD context.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If NewEvpMdContext is NULL, then return FALSE.
> > > +
> > > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > > +
> > > +  @retval TRUE   EVP_MD context copy succeeded.
> > > +  @retval FALSE  EVP_MD context copy failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdDuplicate (
> > > +  IN  CONST VOID    *EvpMdContext,
> > > +  OUT VOID          *NewEvpMdContext
> > > +  );
> > > +
> > > +/**
> > > +  Digests the input data and updates EVP_MD context.
> > > +
> > > +  This function performs EVP digest on a data buffer of the specified size.
> > > +  It can be called multiple times to compute the digest of long or
> > discontinuous data streams.
> > > +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
> > should not
> > > +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If Data is NULL and DataSize is not zero, return FALSE.
> > > +
> > > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > > +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> > digested.
> > > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > > +
> > > +  @retval TRUE   EVP data digest succeeded.
> > > +  @retval FALSE  EVP data digest failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdUpdate (
> > > +  IN OUT  VOID        *EvpMdContext,
> > > +  IN      CONST VOID  *Data,
> > > +  IN      UINTN       DataSize
> > > +  );
> > > +
> > > +/**
> > > +  Completes computation of the EVP digest value.
> > > +  Releases the specified EVP_MD_CTX context.
> > > +
> > > +  This function completes EVP hash computation and retrieves the digest
> > value into
> > > +  the specified memory. After this function has been called, the EVP context
> > cannot
> > > +  be used again.
> > > +  EVP context should be already correctly initialized by EvpMdInit(), and
> > should
> > > +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> > undefined.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If DigestValue is NULL, free the Context then return FALSE.
> > > +
> > > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > > +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> > value.
> >
> > (2) Please extend the comment on Digest with the following:
> >
> >   The caller is responsible for providing enough storage for the digest
> >   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
> >   will suffice for storing the digest regardless of the algorithm chosen
> >   in EvpMdInit().
> >
> > (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
> > advertise it to consumers in edk2.)
> >
> > > +
> > > +  @retval TRUE   EVP digest computation succeeded.
> > > +  @retval FALSE  EVP digest computation failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdFinal (
> > > +  IN OUT  VOID   *EvpMdContext,
> > > +  OUT     UINT8  *DigestValue
> >
> > (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> > deliberately permitted (for just freeing the context).
> >
> > > +  );
> > > +
> > > +/**
> > > +  Computes the message digest of an input data buffer.
> > > +
> > > +  This function performs the message digest of a given data buffer, and
> places
> > > +  the digest value into the specified memory.
> > > +
> > > +  If DigestName is NULL, return FALSE.
> > > +  If Data is NULL and DataSize is not zero, return FALSE.
> > > +  If HashValue is NULL, return FALSE.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name.
> > > +  @param[in]    Data          Pointer to the buffer containing the data to be
> > hashed.
> > > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> > value.
> > > +
> > > +  @retval TRUE   Digest computation succeeded.
> > > +  @retval FALSE  Digest computation failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdHashAll (
> > > +  IN  CONST CHAR8   *DigestName,
> > > +  IN  CONST VOID    *Data,
> > > +  IN  UINTN         DataSize,
> > > +  OUT UINT8         *HashValue
> > > +  );
> > > +
> > >
> >
> //===============================================================
> > ======================
> > >  //    Symmetric Cryptography Primitive
> > >
> >
> //===============================================================
> > ======================
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> > b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> > > new file mode 100644
> > > index 0000000000..b2770a9186
> > > --- /dev/null
> > > +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> > > @@ -0,0 +1,257 @@
> > > +/** @file
> > > +  EVP MD Wrapper Implementation for OpenSSL.
> > > +
> > > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "InternalCryptLib.h"
> > > +#include <openssl/evp.h>
> > > +
> > > +/**
> > > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> > use.
> > > +
> > > +  If DigestName is NULL, then return FALSE.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> > terminated ASCII string.
> > > +                              Valid digest names are:
> > > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > > +                              SM3
> > > +
> > > +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> > initialized.
> > > +           If DigestName is invalid, returns NULL.
> > > +           If the allocations fails, returns NULL.
> > > +           If initialization fails, returns NULL.
> > > +
> > > +**/
> > > +VOID *
> > > +EFIAPI
> > > +EvpMdInit (
> > > +  IN  CONST CHAR8   *DigestName
> > > +  )
> > > +{
> > > +  EVP_MD    *Digest;
> > > +  VOID      *EvpMdContext;
> > > +
> > > +  //
> > > +  // Check input parameters.
> > > +  //
> > > +  if (DigestName == NULL) {
> > > +    return NULL;
> > > +  }
> > > +
> > > +  //
> > > +  // Allocate EVP_MD_CTX Context
> > > +  //
> > > +  EvpMdContext = EVP_MD_CTX_new ();
> > > +  if (EvpMdContext == NULL) {
> > > +    return NULL;
> > > +  }
> > > +
> > > +  Digest = EVP_get_digestbyname (DigestName);
> >
> > I think this may not compile with gcc (and correctly so). The pointer
> > returned by EVP_get_digestbyname() is const-qualified, but with the
> > assignment, we're throwing away the const-ness.
> >
> > (4) Please const-qualify the "Digest" local pointer.
> >
> > > +  if (Digest == NULL) {
> > > +    return NULL;
> > > +  }
> >
> > (5) This is a memory leak I believe; "EvpMdContext" is leaked.
> >
> > For keeping the control flow simple, consider moving
> > EVP_get_digestbyname() above EVP_MD_CTX_new().
> >
> > > +
> > > +  //
> > > +  // Initialize Context
> > > +  //
> > > +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
> > > +    EVP_MD_CTX_free (EvpMdContext);
> > > +    return NULL;
> > > +  }
> > > +
> > > +  return EvpMdContext;
> > > +}
> > > +
> > > +/**
> > > +  Makes a copy of an existing EVP_MD context.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If NewEvpMdContext is NULL, then return FALSE.
> > > +
> > > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > > +
> > > +  @retval TRUE   EVP_MD context copy succeeded.
> > > +  @retval FALSE  EVP_MD context copy failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdDuplicate (
> > > +  IN  CONST VOID    *EvpMdContext,
> > > +  OUT VOID          *NewEvpMdContext
> > > +  )
> > > +{
> > > +  //
> > > +  // Check input parameters.
> > > +  //
> > > +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  return TRUE;
> > > +}
> >
> > (6) Can you please confirm that the caller is supposed to initialize
> > "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
> >
> > > +
> > > +/**
> > > +  Digests the input data and updates EVP_MD context.
> > > +
> > > +  This function performs EVP digest on a data buffer of the specified size.
> > > +  It can be called multiple times to compute the digest of long or
> > discontinuous data streams.
> > > +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
> > should not
> > > +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If Data is NULL and DataSize is not zero, return FALSE.
> > > +
> > > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > > +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> > digested.
> > > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > > +
> > > +  @retval TRUE   EVP data digest succeeded.
> > > +  @retval FALSE  EVP data digest failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdUpdate (
> > > +  IN OUT  VOID        *EvpMdContext,
> > > +  IN      CONST VOID  *Data,
> > > +  IN      UINTN       DataSize
> > > +  )
> > > +{
> > > +  //
> > > +  // Check input parameters.
> > > +  //
> > > +  if (EvpMdContext == NULL) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  //
> > > +  // Check invalid parameters, in case only DataLength was checked in
> > OpenSSL
> > > +  //
> > > +  if (Data == NULL && DataSize != 0) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  //
> > > +  // OpenSSL EVP digest update
> > > +  //
> > > +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  return TRUE;
> > > +}
> > > +
> > > +/**
> > > +  Completes computation of the EVP digest value.
> > > +  Releases the specified EVP_MD_CTX context.
> > > +
> > > +  This function completes EVP hash computation and retrieves the digest
> > value into
> > > +  the specified memory. After this function has been called, the EVP context
> > cannot
> > > +  be used again.
> > > +  EVP context should be already correctly initialized by EvpMdInit(), and
> > should
> > > +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> > undefined.
> > > +
> > > +  If EvpMdContext is NULL, then return FALSE.
> > > +  If DigestValue is NULL, free the Context then return FALSE.
> > > +
> > > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > > +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> > value.
> > > +
> > > +  @retval TRUE   EVP digest computation succeeded.
> > > +  @retval FALSE  EVP digest computation failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdFinal (
> > > +  IN OUT  VOID   *EvpMdContext,
> > > +  OUT     UINT8  *DigestValue
> > > +  )
> > > +{
> > > +  UINT32    Length;
> > > +  BOOLEAN   ReturnValue;
> > > +
> > > +  ReturnValue = TRUE;
> > > +
> > > +  //
> > > +  // Check input parameters.
> > > +  //
> > > +  if (EvpMdContext == NULL) {
> > > +    return FALSE;
> > > +  }
> > > +  if (DigestValue == NULL) {
> > > +    EVP_MD_CTX_free (EvpMdContext);
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  //
> > > +  // OpenSSL EVP digest finalization
> > > +  //
> > > +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
> > > +    ReturnValue = FALSE;
> > > +  }
> >
> >
> > (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
> > deals fine with the third parameter being NULL, according to the docs
> > (and the code).
> >
> >
> > > +
> > > +  //
> > > +  // Free OpenSSL EVP_MD_CTX Context
> > > +  //
> > > +  EVP_MD_CTX_free (EvpMdContext);
> > > +
> > > +  return ReturnValue;
> > > +}
> > > +
> > > +/**
> > > +  Computes the message digest of an input data buffer.
> > > +
> > > +  This function performs the message digest of a given data buffer, and
> places
> > > +  the digest value into the specified memory.
> > > +
> > > +  If DigestName is NULL, return FALSE.
> > > +  If Data is NULL and DataSize is not zero, return FALSE.
> > > +  If HashValue is NULL, return FALSE.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name.
> > > +  @param[in]    Data          Pointer to the buffer containing the data to be
> > hashed.
> > > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> > value.
> > > +
> > > +  @retval TRUE   Digest computation succeeded.
> > > +  @retval FALSE  Digest computation failed.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdHashAll (
> > > +  IN  CONST CHAR8   *DigestName,
> > > +  IN  CONST VOID    *Data,
> > > +  IN  UINTN         DataSize,
> > > +  OUT UINT8         *HashValue
> > > +  )
> > > +{
> > > +  BOOLEAN   Result;
> > > +  VOID      *EvpMdContext;
> > > +
> > > +  EvpMdContext = EvpMdInit (DigestName);
> > > +  if (EvpMdContext == NULL) {
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
> > > +  if (Result == FALSE) {
> >
> > (8) Style: please write (!Result).
> >
> >
> > > +    EvpMdFinal (EvpMdContext, NULL);
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  Result = EvpMdFinal (EvpMdContext, HashValue);
> > > +
> > > +  return Result;
> > > +}
> > > diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> > b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> > > new file mode 100644
> > > index 0000000000..038f63801f
> > > --- /dev/null
> > > +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> > > @@ -0,0 +1,128 @@
> > > +/** @file
> > > +  EVP MD Wrapper Null Library.
> > > +
> > > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "InternalCryptLib.h"
> > > +
> > > +/**
> > > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> > use.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> > terminated ASCII string.
> > > +                              Valid digest names are:
> > > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > > +                              SM3
> > > +
> > > +  @return NULL  This interface is not supported.
> > > +
> > > +**/
> > > +VOID *
> > > +EFIAPI
> > > +EvpMdInit (
> > > +  IN  CONST CHAR8   *DigestName
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return NULL;
> > > +}
> > > +
> > > +/**
> > > +  Makes a copy of an existing EVP_MD context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdDuplicate (
> > > +  IN  CONST VOID    *EvpMdContext,
> > > +  OUT VOID          *NewEvpMdContext
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Digests the input data and updates EVP_MD context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > > +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> > digested.
> > > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdUpdate (
> > > +  IN OUT  VOID        *EvpMdContext,
> > > +  IN      CONST VOID  *Data,
> > > +  IN      UINTN       DataSize
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Completes computation of the EVP digest value.
> > > +  Releases the specified EVP_MD_CTX context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > > +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> > value.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdFinal (
> > > +  IN OUT  VOID   *EvpMdContext,
> > > +  OUT     UINT8  *DigestValue
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Computes the message digest of an input data buffer.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name.
> > > +  @param[in]    Data          Pointer to the buffer containing the data to be
> > hashed.
> > > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> > value.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdHashAll (
> > > +  IN  CONST CHAR8   *DigestName,
> > > +  IN  CONST VOID    *Data,
> > > +  IN  UINTN         DataSize,
> > > +  OUT UINT8         *HashValue
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> > b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> > > new file mode 100644
> > > index 0000000000..038f63801f
> > > --- /dev/null
> > > +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> > > @@ -0,0 +1,128 @@
> > > +/** @file
> > > +  EVP MD Wrapper Null Library.
> > > +
> > > +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "InternalCryptLib.h"
> > > +
> > > +/**
> > > +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
> > use.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> > terminated ASCII string.
> > > +                              Valid digest names are:
> > > +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> > > +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> > > +                              SM3
> > > +
> > > +  @return NULL  This interface is not supported.
> > > +
> > > +**/
> > > +VOID *
> > > +EFIAPI
> > > +EvpMdInit (
> > > +  IN  CONST CHAR8   *DigestName
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return NULL;
> > > +}
> > > +
> > > +/**
> > > +  Makes a copy of an existing EVP_MD context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> > > +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdDuplicate (
> > > +  IN  CONST VOID    *EvpMdContext,
> > > +  OUT VOID          *NewEvpMdContext
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Digests the input data and updates EVP_MD context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> > > +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> > digested.
> > > +  @param[in]       DataSize           Size of Data buffer in bytes.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdUpdate (
> > > +  IN OUT  VOID        *EvpMdContext,
> > > +  IN      CONST VOID  *Data,
> > > +  IN      UINTN       DataSize
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Completes computation of the EVP digest value.
> > > +  Releases the specified EVP_MD_CTX context.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> > > +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> > value.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdFinal (
> > > +  IN OUT  VOID   *EvpMdContext,
> > > +  OUT     UINT8  *DigestValue
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Computes the message digest of an input data buffer.
> > > +
> > > +  Return FALSE to indicate this interface is not supported.
> > > +
> > > +  @param[in]    DigestName    Pointer to the digest name.
> > > +  @param[in]    Data          Pointer to the buffer containing the data to be
> > hashed.
> > > +  @param[in]    DataSize      Size of Data buffer in bytes.
> > > +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> > value.
> > > +
> > > +  @retval FALSE  This interface is not supported.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +EvpMdHashAll (
> > > +  IN  CONST CHAR8   *DigestName,
> > > +  IN  CONST VOID    *Data,
> > > +  IN  UINTN         DataSize,
> > > +  OUT UINT8         *HashValue
> > > +  )
> > > +{
> > > +  ASSERT (FALSE);
> > > +  return FALSE;
> > > +}
> > >
> >
> > Thanks,
> > Laszlo
> 
> 
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 12:07 [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface Yao, Jiewen
@ 2020-09-16 12:39 ` Laszlo Ersek
  2020-09-16 12:56   ` Yao, Jiewen
  2020-09-16 13:31   ` Laszlo Ersek
  0 siblings, 2 replies; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 12:39 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

On 09/16/20 14:07, Yao, Jiewen wrote:
> I overlooked the behavior that NULL DigestValue will take side effect to free Context.
> 
> Hi Christopher
> May I understand why we need free the context?
> This does not align with other existing HASH or HMAC functions.

I requested that.

It makes no sense to keep a finalized context.

In other words, it makes no sense to separate finalization from freeing.
A context that has been finalized is unusable for anything except freeing.

Thanks
Laszlo

> 
> Thank you
> Yao Jiewen
> 
>> -----Original Message-----
>> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yao, Jiewen
>> Sent: Wednesday, September 16, 2020 7:45 PM
>> To: Laszlo Ersek <lersek@redhat.com>; devel@edk2.groups.io; Zurcher,
>> Christopher J <christopher.j.zurcher@intel.com>
>> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>> (Envelope) Digest interface
>>
>> Hi Laszlo
>> I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
>> If we need follow this, then we need add OPTIONAL to almost all pointer, which
>> is unnecessary.
>>
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdFinal (
>>> +  IN OUT  VOID   *EvpMdContext,
>>> +  OUT     UINT8  *DigestValue
>>
>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
>> deliberately permitted (for just freeing the context).
>>
>>
>>
>>
>>> -----Original Message-----
>>> From: Laszlo Ersek <lersek@redhat.com>
>>> Sent: Wednesday, September 16, 2020 7:07 PM
>>> To: devel@edk2.groups.io; Zurcher, Christopher J
>>> <christopher.j.zurcher@intel.com>
>>> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J
>> <jian.j.wang@intel.com>;
>>> Lu, XiaoyuX <xiaoyux.lu@intel.com>
>>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>>> (Envelope) Digest interface
>>>
>>> Hello Christopher,
>>>
>>> On 09/16/20 02:59, Zurcher, Christopher J wrote:
>>>> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
>>>>
>>>> The EVP interface should be used in place of discrete digest function
>>>> calls.
>>>>
>>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>>>> Cc: Jian J Wang <jian.j.wang@intel.com>
>>>> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
>>>> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
>>>> ---
>>>>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
>>>>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
>>>>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
>>>>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
>>>>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
>>>>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
>>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
>>> ++++++++++++++++++++
>>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128 ++++++++++
>>>>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128
>> ++++++++++
>>>>  9 files changed, 647 insertions(+)
>>>
>>> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
>>> is necessary. (If I understand correctly, that file was missing from
>>> your v2 posting.)
>>>
>>> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
>>> superfluous. This file is never referenced in the INF files.
>>>
>>> The point of this file would be to allow *some* of the Base / Pei /
>>> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
>>> only stub implementations). But this isn't what's happening -- all of
>>> the Base / Pei / Runtime / Smm instances are getting the real deal
>>> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
>>>
>>> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
>>> should be dropped. Only the Null instance of the library needs null
>>> versions of the new functions. The Base / Pei / Runtime / Smm instances
>>> don't.
>>>
>>>>
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>> index 4aae2aba95..3968f29412 100644
>>>> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>> @@ -50,6 +50,7 @@
>>>>    Pk/CryptAuthenticode.c
>>>>    Pk/CryptTs.c
>>>>    Pem/CryptPem.c
>>>> +  Evp/CryptEvpMd.c
>>>>
>>>>    SysCall/CrtWrapper.c
>>>>    SysCall/TimerWrapper.c
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>> index dc28e3a11d..d0b91716d0 100644
>>>> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>> @@ -57,6 +57,7 @@
>>>>    Pk/CryptTsNull.c
>>>>    Pem/CryptPemNull.c
>>>>    Rand/CryptRandNull.c
>>>> +  Evp/CryptEvpMd.c
>>>>
>>>>    SysCall/CrtWrapper.c
>>>>    SysCall/ConstantTimeClock.c
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>> index 5005beed02..9f3accd35b 100644
>>>> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>> @@ -56,6 +56,7 @@
>>>>    Pk/CryptAuthenticodeNull.c
>>>>    Pk/CryptTsNull.c
>>>>    Pem/CryptPem.c
>>>> +  Evp/CryptEvpMd.c
>>>>
>>>>    SysCall/CrtWrapper.c
>>>>    SysCall/TimerWrapper.c
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>> index 91ec3e03bf..420623cdc6 100644
>>>> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>> @@ -54,6 +54,7 @@
>>>>    Pk/CryptAuthenticodeNull.c
>>>>    Pk/CryptTsNull.c
>>>>    Pem/CryptPem.c
>>>> +  Evp/CryptEvpMd.c
>>>>
>>>>    SysCall/CrtWrapper.c
>>>>    SysCall/ConstantTimeClock.c
>>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>> index 689af4fedd..542ac2e2e1 100644
>>>> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>> @@ -50,6 +50,7 @@
>>>>    Pk/CryptTsNull.c
>>>>    Pem/CryptPemNull.c
>>>>    Rand/CryptRandNull.c
>>>> +  Evp/CryptEvpMdNull.c
>>>>
>>>>  [Packages]
>>>>    MdePkg/MdePkg.dec
>>>> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
>>> b/CryptoPkg/Include/Library/BaseCryptLib.h
>>>> index ae9bde9e37..5e1b408b54 100644
>>>> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
>>>> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
>>>> @@ -1012,6 +1012,135 @@ HmacSha256Final (
>>>>    OUT     UINT8  *HmacValue
>>>>    );
>>>>
>>>>
>>>
>> +//==============================================================
>>> =======================
>>>> +//    EVP (Envelope) Primitive
>>>>
>>>
>> +//==============================================================
>>> =======================
>>>> +
>>>> +/**
>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>>> use.
>>>> +
>>>> +  If DigestName is NULL, then return FALSE.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>> terminated ASCII string.
>>>> +                              Valid digest names are:
>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>> +                              SM3
>>>> +
>>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
>>> initialized.
>>>> +           If DigestName is invalid, returns NULL.
>>>> +           If the allocations fails, returns NULL.
>>>> +           If initialization fails, returns NULL.
>>>> +
>>>> +**/
>>>> +VOID *
>>>> +EFIAPI
>>>> +EvpMdInit (
>>>> +  IN  CONST CHAR8   *DigestName
>>>> +  );
>>>> +
>>>> +/**
>>>> +  Makes a copy of an existing EVP_MD context.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>>> +
>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>> +
>>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>>> +  @retval FALSE  EVP_MD context copy failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdDuplicate (
>>>> +  IN  CONST VOID    *EvpMdContext,
>>>> +  OUT VOID          *NewEvpMdContext
>>>> +  );
>>>> +
>>>> +/**
>>>> +  Digests the input data and updates EVP_MD context.
>>>> +
>>>> +  This function performs EVP digest on a data buffer of the specified size.
>>>> +  It can be called multiple times to compute the digest of long or
>>> discontinuous data streams.
>>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
>>> should not
>>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>> +
>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>> be
>>> digested.
>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>> +
>>>> +  @retval TRUE   EVP data digest succeeded.
>>>> +  @retval FALSE  EVP data digest failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdUpdate (
>>>> +  IN OUT  VOID        *EvpMdContext,
>>>> +  IN      CONST VOID  *Data,
>>>> +  IN      UINTN       DataSize
>>>> +  );
>>>> +
>>>> +/**
>>>> +  Completes computation of the EVP digest value.
>>>> +  Releases the specified EVP_MD_CTX context.
>>>> +
>>>> +  This function completes EVP hash computation and retrieves the digest
>>> value into
>>>> +  the specified memory. After this function has been called, the EVP context
>>> cannot
>>>> +  be used again.
>>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>>> should
>>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>>> undefined.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>>> +
>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>> digest
>>> value.
>>>
>>> (2) Please extend the comment on Digest with the following:
>>>
>>>   The caller is responsible for providing enough storage for the digest
>>>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
>>>   will suffice for storing the digest regardless of the algorithm chosen
>>>   in EvpMdInit().
>>>
>>> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
>>> advertise it to consumers in edk2.)
>>>
>>>> +
>>>> +  @retval TRUE   EVP digest computation succeeded.
>>>> +  @retval FALSE  EVP digest computation failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdFinal (
>>>> +  IN OUT  VOID   *EvpMdContext,
>>>> +  OUT     UINT8  *DigestValue
>>>
>>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
>>> deliberately permitted (for just freeing the context).
>>>
>>>> +  );
>>>> +
>>>> +/**
>>>> +  Computes the message digest of an input data buffer.
>>>> +
>>>> +  This function performs the message digest of a given data buffer, and
>> places
>>>> +  the digest value into the specified memory.
>>>> +
>>>> +  If DigestName is NULL, return FALSE.
>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>> +  If HashValue is NULL, return FALSE.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>> hashed.
>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>> value.
>>>> +
>>>> +  @retval TRUE   Digest computation succeeded.
>>>> +  @retval FALSE  Digest computation failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdHashAll (
>>>> +  IN  CONST CHAR8   *DigestName,
>>>> +  IN  CONST VOID    *Data,
>>>> +  IN  UINTN         DataSize,
>>>> +  OUT UINT8         *HashValue
>>>> +  );
>>>> +
>>>>
>>>
>> //===============================================================
>>> ======================
>>>>  //    Symmetric Cryptography Primitive
>>>>
>>>
>> //===============================================================
>>> ======================
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>>> new file mode 100644
>>>> index 0000000000..b2770a9186
>>>> --- /dev/null
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>>> @@ -0,0 +1,257 @@
>>>> +/** @file
>>>> +  EVP MD Wrapper Implementation for OpenSSL.
>>>> +
>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>> +
>>>> +**/
>>>> +
>>>> +#include "InternalCryptLib.h"
>>>> +#include <openssl/evp.h>
>>>> +
>>>> +/**
>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>>> use.
>>>> +
>>>> +  If DigestName is NULL, then return FALSE.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>> terminated ASCII string.
>>>> +                              Valid digest names are:
>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>> +                              SM3
>>>> +
>>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
>>> initialized.
>>>> +           If DigestName is invalid, returns NULL.
>>>> +           If the allocations fails, returns NULL.
>>>> +           If initialization fails, returns NULL.
>>>> +
>>>> +**/
>>>> +VOID *
>>>> +EFIAPI
>>>> +EvpMdInit (
>>>> +  IN  CONST CHAR8   *DigestName
>>>> +  )
>>>> +{
>>>> +  EVP_MD    *Digest;
>>>> +  VOID      *EvpMdContext;
>>>> +
>>>> +  //
>>>> +  // Check input parameters.
>>>> +  //
>>>> +  if (DigestName == NULL) {
>>>> +    return NULL;
>>>> +  }
>>>> +
>>>> +  //
>>>> +  // Allocate EVP_MD_CTX Context
>>>> +  //
>>>> +  EvpMdContext = EVP_MD_CTX_new ();
>>>> +  if (EvpMdContext == NULL) {
>>>> +    return NULL;
>>>> +  }
>>>> +
>>>> +  Digest = EVP_get_digestbyname (DigestName);
>>>
>>> I think this may not compile with gcc (and correctly so). The pointer
>>> returned by EVP_get_digestbyname() is const-qualified, but with the
>>> assignment, we're throwing away the const-ness.
>>>
>>> (4) Please const-qualify the "Digest" local pointer.
>>>
>>>> +  if (Digest == NULL) {
>>>> +    return NULL;
>>>> +  }
>>>
>>> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
>>>
>>> For keeping the control flow simple, consider moving
>>> EVP_get_digestbyname() above EVP_MD_CTX_new().
>>>
>>>> +
>>>> +  //
>>>> +  // Initialize Context
>>>> +  //
>>>> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
>>>> +    EVP_MD_CTX_free (EvpMdContext);
>>>> +    return NULL;
>>>> +  }
>>>> +
>>>> +  return EvpMdContext;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Makes a copy of an existing EVP_MD context.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>>> +
>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>> +
>>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>>> +  @retval FALSE  EVP_MD context copy failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdDuplicate (
>>>> +  IN  CONST VOID    *EvpMdContext,
>>>> +  OUT VOID          *NewEvpMdContext
>>>> +  )
>>>> +{
>>>> +  //
>>>> +  // Check input parameters.
>>>> +  //
>>>> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  return TRUE;
>>>> +}
>>>
>>> (6) Can you please confirm that the caller is supposed to initialize
>>> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
>>>
>>>> +
>>>> +/**
>>>> +  Digests the input data and updates EVP_MD context.
>>>> +
>>>> +  This function performs EVP digest on a data buffer of the specified size.
>>>> +  It can be called multiple times to compute the digest of long or
>>> discontinuous data streams.
>>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
>>> should not
>>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>> +
>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>> be
>>> digested.
>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>> +
>>>> +  @retval TRUE   EVP data digest succeeded.
>>>> +  @retval FALSE  EVP data digest failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdUpdate (
>>>> +  IN OUT  VOID        *EvpMdContext,
>>>> +  IN      CONST VOID  *Data,
>>>> +  IN      UINTN       DataSize
>>>> +  )
>>>> +{
>>>> +  //
>>>> +  // Check input parameters.
>>>> +  //
>>>> +  if (EvpMdContext == NULL) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  //
>>>> +  // Check invalid parameters, in case only DataLength was checked in
>>> OpenSSL
>>>> +  //
>>>> +  if (Data == NULL && DataSize != 0) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  //
>>>> +  // OpenSSL EVP digest update
>>>> +  //
>>>> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  return TRUE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Completes computation of the EVP digest value.
>>>> +  Releases the specified EVP_MD_CTX context.
>>>> +
>>>> +  This function completes EVP hash computation and retrieves the digest
>>> value into
>>>> +  the specified memory. After this function has been called, the EVP context
>>> cannot
>>>> +  be used again.
>>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>>> should
>>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>>> undefined.
>>>> +
>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>>> +
>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>> digest
>>> value.
>>>> +
>>>> +  @retval TRUE   EVP digest computation succeeded.
>>>> +  @retval FALSE  EVP digest computation failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdFinal (
>>>> +  IN OUT  VOID   *EvpMdContext,
>>>> +  OUT     UINT8  *DigestValue
>>>> +  )
>>>> +{
>>>> +  UINT32    Length;
>>>> +  BOOLEAN   ReturnValue;
>>>> +
>>>> +  ReturnValue = TRUE;
>>>> +
>>>> +  //
>>>> +  // Check input parameters.
>>>> +  //
>>>> +  if (EvpMdContext == NULL) {
>>>> +    return FALSE;
>>>> +  }
>>>> +  if (DigestValue == NULL) {
>>>> +    EVP_MD_CTX_free (EvpMdContext);
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  //
>>>> +  // OpenSSL EVP digest finalization
>>>> +  //
>>>> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
>>>> +    ReturnValue = FALSE;
>>>> +  }
>>>
>>>
>>> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
>>> deals fine with the third parameter being NULL, according to the docs
>>> (and the code).
>>>
>>>
>>>> +
>>>> +  //
>>>> +  // Free OpenSSL EVP_MD_CTX Context
>>>> +  //
>>>> +  EVP_MD_CTX_free (EvpMdContext);
>>>> +
>>>> +  return ReturnValue;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Computes the message digest of an input data buffer.
>>>> +
>>>> +  This function performs the message digest of a given data buffer, and
>> places
>>>> +  the digest value into the specified memory.
>>>> +
>>>> +  If DigestName is NULL, return FALSE.
>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>> +  If HashValue is NULL, return FALSE.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>> hashed.
>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>> value.
>>>> +
>>>> +  @retval TRUE   Digest computation succeeded.
>>>> +  @retval FALSE  Digest computation failed.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdHashAll (
>>>> +  IN  CONST CHAR8   *DigestName,
>>>> +  IN  CONST VOID    *Data,
>>>> +  IN  UINTN         DataSize,
>>>> +  OUT UINT8         *HashValue
>>>> +  )
>>>> +{
>>>> +  BOOLEAN   Result;
>>>> +  VOID      *EvpMdContext;
>>>> +
>>>> +  EvpMdContext = EvpMdInit (DigestName);
>>>> +  if (EvpMdContext == NULL) {
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
>>>> +  if (Result == FALSE) {
>>>
>>> (8) Style: please write (!Result).
>>>
>>>
>>>> +    EvpMdFinal (EvpMdContext, NULL);
>>>> +    return FALSE;
>>>> +  }
>>>> +
>>>> +  Result = EvpMdFinal (EvpMdContext, HashValue);
>>>> +
>>>> +  return Result;
>>>> +}
>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>>> new file mode 100644
>>>> index 0000000000..038f63801f
>>>> --- /dev/null
>>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>>> @@ -0,0 +1,128 @@
>>>> +/** @file
>>>> +  EVP MD Wrapper Null Library.
>>>> +
>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>> +
>>>> +**/
>>>> +
>>>> +#include "InternalCryptLib.h"
>>>> +
>>>> +/**
>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>>> use.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>> terminated ASCII string.
>>>> +                              Valid digest names are:
>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>> +                              SM3
>>>> +
>>>> +  @return NULL  This interface is not supported.
>>>> +
>>>> +**/
>>>> +VOID *
>>>> +EFIAPI
>>>> +EvpMdInit (
>>>> +  IN  CONST CHAR8   *DigestName
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return NULL;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Makes a copy of an existing EVP_MD context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdDuplicate (
>>>> +  IN  CONST VOID    *EvpMdContext,
>>>> +  OUT VOID          *NewEvpMdContext
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Digests the input data and updates EVP_MD context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>> be
>>> digested.
>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdUpdate (
>>>> +  IN OUT  VOID        *EvpMdContext,
>>>> +  IN      CONST VOID  *Data,
>>>> +  IN      UINTN       DataSize
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Completes computation of the EVP digest value.
>>>> +  Releases the specified EVP_MD_CTX context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>> digest
>>> value.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdFinal (
>>>> +  IN OUT  VOID   *EvpMdContext,
>>>> +  OUT     UINT8  *DigestValue
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Computes the message digest of an input data buffer.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>> hashed.
>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>> value.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdHashAll (
>>>> +  IN  CONST CHAR8   *DigestName,
>>>> +  IN  CONST VOID    *Data,
>>>> +  IN  UINTN         DataSize,
>>>> +  OUT UINT8         *HashValue
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>>> new file mode 100644
>>>> index 0000000000..038f63801f
>>>> --- /dev/null
>>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>>> @@ -0,0 +1,128 @@
>>>> +/** @file
>>>> +  EVP MD Wrapper Null Library.
>>>> +
>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>> +
>>>> +**/
>>>> +
>>>> +#include "InternalCryptLib.h"
>>>> +
>>>> +/**
>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>>> use.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>> terminated ASCII string.
>>>> +                              Valid digest names are:
>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>> +                              SM3
>>>> +
>>>> +  @return NULL  This interface is not supported.
>>>> +
>>>> +**/
>>>> +VOID *
>>>> +EFIAPI
>>>> +EvpMdInit (
>>>> +  IN  CONST CHAR8   *DigestName
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return NULL;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Makes a copy of an existing EVP_MD context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdDuplicate (
>>>> +  IN  CONST VOID    *EvpMdContext,
>>>> +  OUT VOID          *NewEvpMdContext
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Digests the input data and updates EVP_MD context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>> be
>>> digested.
>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdUpdate (
>>>> +  IN OUT  VOID        *EvpMdContext,
>>>> +  IN      CONST VOID  *Data,
>>>> +  IN      UINTN       DataSize
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Completes computation of the EVP digest value.
>>>> +  Releases the specified EVP_MD_CTX context.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>> digest
>>> value.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdFinal (
>>>> +  IN OUT  VOID   *EvpMdContext,
>>>> +  OUT     UINT8  *DigestValue
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>> +
>>>> +/**
>>>> +  Computes the message digest of an input data buffer.
>>>> +
>>>> +  Return FALSE to indicate this interface is not supported.
>>>> +
>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>> hashed.
>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>> value.
>>>> +
>>>> +  @retval FALSE  This interface is not supported.
>>>> +
>>>> +**/
>>>> +BOOLEAN
>>>> +EFIAPI
>>>> +EvpMdHashAll (
>>>> +  IN  CONST CHAR8   *DigestName,
>>>> +  IN  CONST VOID    *Data,
>>>> +  IN  UINTN         DataSize,
>>>> +  OUT UINT8         *HashValue
>>>> +  )
>>>> +{
>>>> +  ASSERT (FALSE);
>>>> +  return FALSE;
>>>> +}
>>>>
>>>
>>> Thanks,
>>> Laszlo
>>
>>
>> 
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 12:39 ` Laszlo Ersek
@ 2020-09-16 12:56   ` Yao, Jiewen
  2020-09-16 15:27     ` Laszlo Ersek
  2020-09-16 13:31   ` Laszlo Ersek
  1 sibling, 1 reply; 13+ messages in thread
From: Yao, Jiewen @ 2020-09-16 12:56 UTC (permalink / raw)
  To: devel@edk2.groups.io, lersek@redhat.com, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

I try to understand what is the use case to EvpMdFinal without DigestValue and only Free the EvpMdContext.

A normal usage will be:
1) EvpMdHashAll (Digest) - get digest.
2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest) - get digest

Why we need support the case to use EvpMdFinal (NULL) in the end to free context?


Thank you
Yao Jiewen

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Laszlo Ersek
> Sent: Wednesday, September 16, 2020 8:40 PM
> To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher,
> Christopher J <christopher.j.zurcher@intel.com>
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> 
> On 09/16/20 14:07, Yao, Jiewen wrote:
> > I overlooked the behavior that NULL DigestValue will take side effect to free
> Context.
> >
> > Hi Christopher
> > May I understand why we need free the context?
> > This does not align with other existing HASH or HMAC functions.
> 
> I requested that.
> 
> It makes no sense to keep a finalized context.
> 
> In other words, it makes no sense to separate finalization from freeing.
> A context that has been finalized is unusable for anything except freeing.
> 
> Thanks
> Laszlo
> 
> >
> > Thank you
> > Yao Jiewen
> >
> >> -----Original Message-----
> >> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yao,
> Jiewen
> >> Sent: Wednesday, September 16, 2020 7:45 PM
> >> To: Laszlo Ersek <lersek@redhat.com>; devel@edk2.groups.io; Zurcher,
> >> Christopher J <christopher.j.zurcher@intel.com>
> >> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX
> <xiaoyux.lu@intel.com>
> >> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> >> (Envelope) Digest interface
> >>
> >> Hi Laszlo
> >> I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
> >> If we need follow this, then we need add OPTIONAL to almost all pointer,
> which
> >> is unnecessary.
> >>
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdFinal (
> >>> +  IN OUT  VOID   *EvpMdContext,
> >>> +  OUT     UINT8  *DigestValue
> >>
> >> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> >> deliberately permitted (for just freeing the context).
> >>
> >>
> >>
> >>
> >>> -----Original Message-----
> >>> From: Laszlo Ersek <lersek@redhat.com>
> >>> Sent: Wednesday, September 16, 2020 7:07 PM
> >>> To: devel@edk2.groups.io; Zurcher, Christopher J
> >>> <christopher.j.zurcher@intel.com>
> >>> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J
> >> <jian.j.wang@intel.com>;
> >>> Lu, XiaoyuX <xiaoyux.lu@intel.com>
> >>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> >>> (Envelope) Digest interface
> >>>
> >>> Hello Christopher,
> >>>
> >>> On 09/16/20 02:59, Zurcher, Christopher J wrote:
> >>>> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
> >>>>
> >>>> The EVP interface should be used in place of discrete digest function
> >>>> calls.
> >>>>
> >>>> Cc: Laszlo Ersek <lersek@redhat.com>
> >>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
> >>>> Cc: Jian J Wang <jian.j.wang@intel.com>
> >>>> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
> >>>> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
> >>>> ---
> >>>>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
> >>>>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
> >>>>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
> >>>>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
> >>>>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
> >>>>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
> >>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
> >>> ++++++++++++++++++++
> >>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128
> ++++++++++
> >>>>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128
> >> ++++++++++
> >>>>  9 files changed, 647 insertions(+)
> >>>
> >>> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
> >>> is necessary. (If I understand correctly, that file was missing from
> >>> your v2 posting.)
> >>>
> >>> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
> >>> superfluous. This file is never referenced in the INF files.
> >>>
> >>> The point of this file would be to allow *some* of the Base / Pei /
> >>> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
> >>> only stub implementations). But this isn't what's happening -- all of
> >>> the Base / Pei / Runtime / Smm instances are getting the real deal
> >>> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
> >>>
> >>> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
> >>> should be dropped. Only the Null instance of the library needs null
> >>> versions of the new functions. The Base / Pei / Runtime / Smm instances
> >>> don't.
> >>>
> >>>>
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>>> index 4aae2aba95..3968f29412 100644
> >>>> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>>> @@ -50,6 +50,7 @@
> >>>>    Pk/CryptAuthenticode.c
> >>>>    Pk/CryptTs.c
> >>>>    Pem/CryptPem.c
> >>>> +  Evp/CryptEvpMd.c
> >>>>
> >>>>    SysCall/CrtWrapper.c
> >>>>    SysCall/TimerWrapper.c
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>>> index dc28e3a11d..d0b91716d0 100644
> >>>> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>>> @@ -57,6 +57,7 @@
> >>>>    Pk/CryptTsNull.c
> >>>>    Pem/CryptPemNull.c
> >>>>    Rand/CryptRandNull.c
> >>>> +  Evp/CryptEvpMd.c
> >>>>
> >>>>    SysCall/CrtWrapper.c
> >>>>    SysCall/ConstantTimeClock.c
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>>> index 5005beed02..9f3accd35b 100644
> >>>> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>>> @@ -56,6 +56,7 @@
> >>>>    Pk/CryptAuthenticodeNull.c
> >>>>    Pk/CryptTsNull.c
> >>>>    Pem/CryptPem.c
> >>>> +  Evp/CryptEvpMd.c
> >>>>
> >>>>    SysCall/CrtWrapper.c
> >>>>    SysCall/TimerWrapper.c
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>>> index 91ec3e03bf..420623cdc6 100644
> >>>> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>>> @@ -54,6 +54,7 @@
> >>>>    Pk/CryptAuthenticodeNull.c
> >>>>    Pk/CryptTsNull.c
> >>>>    Pem/CryptPem.c
> >>>> +  Evp/CryptEvpMd.c
> >>>>
> >>>>    SysCall/CrtWrapper.c
> >>>>    SysCall/ConstantTimeClock.c
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>>> index 689af4fedd..542ac2e2e1 100644
> >>>> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>>> @@ -50,6 +50,7 @@
> >>>>    Pk/CryptTsNull.c
> >>>>    Pem/CryptPemNull.c
> >>>>    Rand/CryptRandNull.c
> >>>> +  Evp/CryptEvpMdNull.c
> >>>>
> >>>>  [Packages]
> >>>>    MdePkg/MdePkg.dec
> >>>> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
> >>> b/CryptoPkg/Include/Library/BaseCryptLib.h
> >>>> index ae9bde9e37..5e1b408b54 100644
> >>>> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> >>>> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> >>>> @@ -1012,6 +1012,135 @@ HmacSha256Final (
> >>>>    OUT     UINT8  *HmacValue
> >>>>    );
> >>>>
> >>>>
> >>>
> >>
> +//==============================================================
> >>> =======================
> >>>> +//    EVP (Envelope) Primitive
> >>>>
> >>>
> >>
> +//==============================================================
> >>> =======================
> >>>> +
> >>>> +/**
> >>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >>> use.
> >>>> +
> >>>> +  If DigestName is NULL, then return FALSE.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >>> terminated ASCII string.
> >>>> +                              Valid digest names are:
> >>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>>> +                              SM3
> >>>> +
> >>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated
> and
> >>> initialized.
> >>>> +           If DigestName is invalid, returns NULL.
> >>>> +           If the allocations fails, returns NULL.
> >>>> +           If initialization fails, returns NULL.
> >>>> +
> >>>> +**/
> >>>> +VOID *
> >>>> +EFIAPI
> >>>> +EvpMdInit (
> >>>> +  IN  CONST CHAR8   *DigestName
> >>>> +  );
> >>>> +
> >>>> +/**
> >>>> +  Makes a copy of an existing EVP_MD context.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If NewEvpMdContext is NULL, then return FALSE.
> >>>> +
> >>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>>> +
> >>>> +  @retval TRUE   EVP_MD context copy succeeded.
> >>>> +  @retval FALSE  EVP_MD context copy failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdDuplicate (
> >>>> +  IN  CONST VOID    *EvpMdContext,
> >>>> +  OUT VOID          *NewEvpMdContext
> >>>> +  );
> >>>> +
> >>>> +/**
> >>>> +  Digests the input data and updates EVP_MD context.
> >>>> +
> >>>> +  This function performs EVP digest on a data buffer of the specified size.
> >>>> +  It can be called multiple times to compute the digest of long or
> >>> discontinuous data streams.
> >>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
> and
> >>> should not
> >>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>>> +  @param[in]       Data               Pointer to the buffer containing the data to
> >> be
> >>> digested.
> >>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>>> +
> >>>> +  @retval TRUE   EVP data digest succeeded.
> >>>> +  @retval FALSE  EVP data digest failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdUpdate (
> >>>> +  IN OUT  VOID        *EvpMdContext,
> >>>> +  IN      CONST VOID  *Data,
> >>>> +  IN      UINTN       DataSize
> >>>> +  );
> >>>> +
> >>>> +/**
> >>>> +  Completes computation of the EVP digest value.
> >>>> +  Releases the specified EVP_MD_CTX context.
> >>>> +
> >>>> +  This function completes EVP hash computation and retrieves the digest
> >>> value into
> >>>> +  the specified memory. After this function has been called, the EVP
> context
> >>> cannot
> >>>> +  be used again.
> >>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
> >>> should
> >>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> >>> undefined.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If DigestValue is NULL, free the Context then return FALSE.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> >> digest
> >>> value.
> >>>
> >>> (2) Please extend the comment on Digest with the following:
> >>>
> >>>   The caller is responsible for providing enough storage for the digest
> >>>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
> >>>   will suffice for storing the digest regardless of the algorithm chosen
> >>>   in EvpMdInit().
> >>>
> >>> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
> >>> advertise it to consumers in edk2.)
> >>>
> >>>> +
> >>>> +  @retval TRUE   EVP digest computation succeeded.
> >>>> +  @retval FALSE  EVP digest computation failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdFinal (
> >>>> +  IN OUT  VOID   *EvpMdContext,
> >>>> +  OUT     UINT8  *DigestValue
> >>>
> >>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> >>> deliberately permitted (for just freeing the context).
> >>>
> >>>> +  );
> >>>> +
> >>>> +/**
> >>>> +  Computes the message digest of an input data buffer.
> >>>> +
> >>>> +  This function performs the message digest of a given data buffer, and
> >> places
> >>>> +  the digest value into the specified memory.
> >>>> +
> >>>> +  If DigestName is NULL, return FALSE.
> >>>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>>> +  If HashValue is NULL, return FALSE.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name.
> >>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >>> hashed.
> >>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >>> value.
> >>>> +
> >>>> +  @retval TRUE   Digest computation succeeded.
> >>>> +  @retval FALSE  Digest computation failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdHashAll (
> >>>> +  IN  CONST CHAR8   *DigestName,
> >>>> +  IN  CONST VOID    *Data,
> >>>> +  IN  UINTN         DataSize,
> >>>> +  OUT UINT8         *HashValue
> >>>> +  );
> >>>> +
> >>>>
> >>>
> >>
> //===============================================================
> >>> ======================
> >>>>  //    Symmetric Cryptography Primitive
> >>>>
> >>>
> >>
> //===============================================================
> >>> ======================
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >>>> new file mode 100644
> >>>> index 0000000000..b2770a9186
> >>>> --- /dev/null
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >>>> @@ -0,0 +1,257 @@
> >>>> +/** @file
> >>>> +  EVP MD Wrapper Implementation for OpenSSL.
> >>>> +
> >>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>> +
> >>>> +**/
> >>>> +
> >>>> +#include "InternalCryptLib.h"
> >>>> +#include <openssl/evp.h>
> >>>> +
> >>>> +/**
> >>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >>> use.
> >>>> +
> >>>> +  If DigestName is NULL, then return FALSE.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >>> terminated ASCII string.
> >>>> +                              Valid digest names are:
> >>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>>> +                              SM3
> >>>> +
> >>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated
> and
> >>> initialized.
> >>>> +           If DigestName is invalid, returns NULL.
> >>>> +           If the allocations fails, returns NULL.
> >>>> +           If initialization fails, returns NULL.
> >>>> +
> >>>> +**/
> >>>> +VOID *
> >>>> +EFIAPI
> >>>> +EvpMdInit (
> >>>> +  IN  CONST CHAR8   *DigestName
> >>>> +  )
> >>>> +{
> >>>> +  EVP_MD    *Digest;
> >>>> +  VOID      *EvpMdContext;
> >>>> +
> >>>> +  //
> >>>> +  // Check input parameters.
> >>>> +  //
> >>>> +  if (DigestName == NULL) {
> >>>> +    return NULL;
> >>>> +  }
> >>>> +
> >>>> +  //
> >>>> +  // Allocate EVP_MD_CTX Context
> >>>> +  //
> >>>> +  EvpMdContext = EVP_MD_CTX_new ();
> >>>> +  if (EvpMdContext == NULL) {
> >>>> +    return NULL;
> >>>> +  }
> >>>> +
> >>>> +  Digest = EVP_get_digestbyname (DigestName);
> >>>
> >>> I think this may not compile with gcc (and correctly so). The pointer
> >>> returned by EVP_get_digestbyname() is const-qualified, but with the
> >>> assignment, we're throwing away the const-ness.
> >>>
> >>> (4) Please const-qualify the "Digest" local pointer.
> >>>
> >>>> +  if (Digest == NULL) {
> >>>> +    return NULL;
> >>>> +  }
> >>>
> >>> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
> >>>
> >>> For keeping the control flow simple, consider moving
> >>> EVP_get_digestbyname() above EVP_MD_CTX_new().
> >>>
> >>>> +
> >>>> +  //
> >>>> +  // Initialize Context
> >>>> +  //
> >>>> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
> >>>> +    EVP_MD_CTX_free (EvpMdContext);
> >>>> +    return NULL;
> >>>> +  }
> >>>> +
> >>>> +  return EvpMdContext;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Makes a copy of an existing EVP_MD context.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If NewEvpMdContext is NULL, then return FALSE.
> >>>> +
> >>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>>> +
> >>>> +  @retval TRUE   EVP_MD context copy succeeded.
> >>>> +  @retval FALSE  EVP_MD context copy failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdDuplicate (
> >>>> +  IN  CONST VOID    *EvpMdContext,
> >>>> +  OUT VOID          *NewEvpMdContext
> >>>> +  )
> >>>> +{
> >>>> +  //
> >>>> +  // Check input parameters.
> >>>> +  //
> >>>> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  return TRUE;
> >>>> +}
> >>>
> >>> (6) Can you please confirm that the caller is supposed to initialize
> >>> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
> >>>
> >>>> +
> >>>> +/**
> >>>> +  Digests the input data and updates EVP_MD context.
> >>>> +
> >>>> +  This function performs EVP digest on a data buffer of the specified size.
> >>>> +  It can be called multiple times to compute the digest of long or
> >>> discontinuous data streams.
> >>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
> and
> >>> should not
> >>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>>> +  @param[in]       Data               Pointer to the buffer containing the data to
> >> be
> >>> digested.
> >>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>>> +
> >>>> +  @retval TRUE   EVP data digest succeeded.
> >>>> +  @retval FALSE  EVP data digest failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdUpdate (
> >>>> +  IN OUT  VOID        *EvpMdContext,
> >>>> +  IN      CONST VOID  *Data,
> >>>> +  IN      UINTN       DataSize
> >>>> +  )
> >>>> +{
> >>>> +  //
> >>>> +  // Check input parameters.
> >>>> +  //
> >>>> +  if (EvpMdContext == NULL) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  //
> >>>> +  // Check invalid parameters, in case only DataLength was checked in
> >>> OpenSSL
> >>>> +  //
> >>>> +  if (Data == NULL && DataSize != 0) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  //
> >>>> +  // OpenSSL EVP digest update
> >>>> +  //
> >>>> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  return TRUE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Completes computation of the EVP digest value.
> >>>> +  Releases the specified EVP_MD_CTX context.
> >>>> +
> >>>> +  This function completes EVP hash computation and retrieves the digest
> >>> value into
> >>>> +  the specified memory. After this function has been called, the EVP
> context
> >>> cannot
> >>>> +  be used again.
> >>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
> >>> should
> >>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> >>> undefined.
> >>>> +
> >>>> +  If EvpMdContext is NULL, then return FALSE.
> >>>> +  If DigestValue is NULL, free the Context then return FALSE.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> >> digest
> >>> value.
> >>>> +
> >>>> +  @retval TRUE   EVP digest computation succeeded.
> >>>> +  @retval FALSE  EVP digest computation failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdFinal (
> >>>> +  IN OUT  VOID   *EvpMdContext,
> >>>> +  OUT     UINT8  *DigestValue
> >>>> +  )
> >>>> +{
> >>>> +  UINT32    Length;
> >>>> +  BOOLEAN   ReturnValue;
> >>>> +
> >>>> +  ReturnValue = TRUE;
> >>>> +
> >>>> +  //
> >>>> +  // Check input parameters.
> >>>> +  //
> >>>> +  if (EvpMdContext == NULL) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +  if (DigestValue == NULL) {
> >>>> +    EVP_MD_CTX_free (EvpMdContext);
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  //
> >>>> +  // OpenSSL EVP digest finalization
> >>>> +  //
> >>>> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
> >>>> +    ReturnValue = FALSE;
> >>>> +  }
> >>>
> >>>
> >>> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
> >>> deals fine with the third parameter being NULL, according to the docs
> >>> (and the code).
> >>>
> >>>
> >>>> +
> >>>> +  //
> >>>> +  // Free OpenSSL EVP_MD_CTX Context
> >>>> +  //
> >>>> +  EVP_MD_CTX_free (EvpMdContext);
> >>>> +
> >>>> +  return ReturnValue;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Computes the message digest of an input data buffer.
> >>>> +
> >>>> +  This function performs the message digest of a given data buffer, and
> >> places
> >>>> +  the digest value into the specified memory.
> >>>> +
> >>>> +  If DigestName is NULL, return FALSE.
> >>>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>>> +  If HashValue is NULL, return FALSE.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name.
> >>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >>> hashed.
> >>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >>> value.
> >>>> +
> >>>> +  @retval TRUE   Digest computation succeeded.
> >>>> +  @retval FALSE  Digest computation failed.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdHashAll (
> >>>> +  IN  CONST CHAR8   *DigestName,
> >>>> +  IN  CONST VOID    *Data,
> >>>> +  IN  UINTN         DataSize,
> >>>> +  OUT UINT8         *HashValue
> >>>> +  )
> >>>> +{
> >>>> +  BOOLEAN   Result;
> >>>> +  VOID      *EvpMdContext;
> >>>> +
> >>>> +  EvpMdContext = EvpMdInit (DigestName);
> >>>> +  if (EvpMdContext == NULL) {
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
> >>>> +  if (Result == FALSE) {
> >>>
> >>> (8) Style: please write (!Result).
> >>>
> >>>
> >>>> +    EvpMdFinal (EvpMdContext, NULL);
> >>>> +    return FALSE;
> >>>> +  }
> >>>> +
> >>>> +  Result = EvpMdFinal (EvpMdContext, HashValue);
> >>>> +
> >>>> +  return Result;
> >>>> +}
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >>>> new file mode 100644
> >>>> index 0000000000..038f63801f
> >>>> --- /dev/null
> >>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >>>> @@ -0,0 +1,128 @@
> >>>> +/** @file
> >>>> +  EVP MD Wrapper Null Library.
> >>>> +
> >>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>> +
> >>>> +**/
> >>>> +
> >>>> +#include "InternalCryptLib.h"
> >>>> +
> >>>> +/**
> >>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >>> use.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >>> terminated ASCII string.
> >>>> +                              Valid digest names are:
> >>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>>> +                              SM3
> >>>> +
> >>>> +  @return NULL  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +VOID *
> >>>> +EFIAPI
> >>>> +EvpMdInit (
> >>>> +  IN  CONST CHAR8   *DigestName
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return NULL;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Makes a copy of an existing EVP_MD context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdDuplicate (
> >>>> +  IN  CONST VOID    *EvpMdContext,
> >>>> +  OUT VOID          *NewEvpMdContext
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Digests the input data and updates EVP_MD context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>>> +  @param[in]       Data               Pointer to the buffer containing the data to
> >> be
> >>> digested.
> >>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdUpdate (
> >>>> +  IN OUT  VOID        *EvpMdContext,
> >>>> +  IN      CONST VOID  *Data,
> >>>> +  IN      UINTN       DataSize
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Completes computation of the EVP digest value.
> >>>> +  Releases the specified EVP_MD_CTX context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> >> digest
> >>> value.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdFinal (
> >>>> +  IN OUT  VOID   *EvpMdContext,
> >>>> +  OUT     UINT8  *DigestValue
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Computes the message digest of an input data buffer.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name.
> >>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >>> hashed.
> >>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >>> value.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdHashAll (
> >>>> +  IN  CONST CHAR8   *DigestName,
> >>>> +  IN  CONST VOID    *Data,
> >>>> +  IN  UINTN         DataSize,
> >>>> +  OUT UINT8         *HashValue
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >>> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >>>> new file mode 100644
> >>>> index 0000000000..038f63801f
> >>>> --- /dev/null
> >>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >>>> @@ -0,0 +1,128 @@
> >>>> +/** @file
> >>>> +  EVP MD Wrapper Null Library.
> >>>> +
> >>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>> +
> >>>> +**/
> >>>> +
> >>>> +#include "InternalCryptLib.h"
> >>>> +
> >>>> +/**
> >>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >>> use.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >>> terminated ASCII string.
> >>>> +                              Valid digest names are:
> >>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>>> +                              SM3
> >>>> +
> >>>> +  @return NULL  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +VOID *
> >>>> +EFIAPI
> >>>> +EvpMdInit (
> >>>> +  IN  CONST CHAR8   *DigestName
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return NULL;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Makes a copy of an existing EVP_MD context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdDuplicate (
> >>>> +  IN  CONST VOID    *EvpMdContext,
> >>>> +  OUT VOID          *NewEvpMdContext
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Digests the input data and updates EVP_MD context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>>> +  @param[in]       Data               Pointer to the buffer containing the data to
> >> be
> >>> digested.
> >>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdUpdate (
> >>>> +  IN OUT  VOID        *EvpMdContext,
> >>>> +  IN      CONST VOID  *Data,
> >>>> +  IN      UINTN       DataSize
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Completes computation of the EVP digest value.
> >>>> +  Releases the specified EVP_MD_CTX context.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> >> digest
> >>> value.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdFinal (
> >>>> +  IN OUT  VOID   *EvpMdContext,
> >>>> +  OUT     UINT8  *DigestValue
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>> +
> >>>> +/**
> >>>> +  Computes the message digest of an input data buffer.
> >>>> +
> >>>> +  Return FALSE to indicate this interface is not supported.
> >>>> +
> >>>> +  @param[in]    DigestName    Pointer to the digest name.
> >>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >>> hashed.
> >>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >>> value.
> >>>> +
> >>>> +  @retval FALSE  This interface is not supported.
> >>>> +
> >>>> +**/
> >>>> +BOOLEAN
> >>>> +EFIAPI
> >>>> +EvpMdHashAll (
> >>>> +  IN  CONST CHAR8   *DigestName,
> >>>> +  IN  CONST VOID    *Data,
> >>>> +  IN  UINTN         DataSize,
> >>>> +  OUT UINT8         *HashValue
> >>>> +  )
> >>>> +{
> >>>> +  ASSERT (FALSE);
> >>>> +  return FALSE;
> >>>> +}
> >>>>
> >>>
> >>> Thanks,
> >>> Laszlo
> >>
> >>
> >>
> >
> 
> 
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 12:39 ` Laszlo Ersek
  2020-09-16 12:56   ` Yao, Jiewen
@ 2020-09-16 13:31   ` Laszlo Ersek
  1 sibling, 0 replies; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 13:31 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

On 09/16/20 14:39, Laszlo Ersek wrote:
> On 09/16/20 14:07, Yao, Jiewen wrote:
>> I overlooked the behavior that NULL DigestValue will take side effect to free Context.
>>
>> Hi Christopher
>> May I understand why we need free the context?
>> This does not align with other existing HASH or HMAC functions.
> 
> I requested that.
> 
> It makes no sense to keep a finalized context.
> 
> In other words, it makes no sense to separate finalization from freeing.
> A context that has been finalized is unusable for anything except freeing.

To clarify my point about (DigestValue==NULL): the freeing of the
context is not tied to (DigestValue==NULL). When finalizing the context,
the context should *always* be freed. (DigestValue==NULL) only decides
whether we *only* free the context, or we store the computed digest *in
addition to* freeing the context.

So the unconditional part of the finalization is the freeing; the
conditional part is the storing of the digest.

Thanks
Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 11:44     ` Yao, Jiewen
@ 2020-09-16 13:35       ` Laszlo Ersek
  2020-09-16 14:17         ` Yao, Jiewen
  0 siblings, 1 reply; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 13:35 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

On 09/16/20 13:44, Yao, Jiewen wrote:
> Hi Laszlo
> I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
> If we need follow this, then we need add OPTIONAL to almost all pointer, which is unnecessary.

I'm not suggesting OPTIONAL *only* because NULL is checked. I'm
suggesting OPTIONAL because there is a specific use case related to
that. Please see the comments on the function:

"If DigestValue is NULL, free the Context then return FALSE."

But, anyway, I don't insist. It's not a huge deal. Feel free to ignore
my comment regarding OPTIONAL.

Thanks
Laszlo


> 
>> +BOOLEAN
>> +EFIAPI
>> +EvpMdFinal (
>> +  IN OUT  VOID   *EvpMdContext,
>> +  OUT     UINT8  *DigestValue
> 
> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> deliberately permitted (for just freeing the context).
> 
> 
> 
> 
>> -----Original Message-----
>> From: Laszlo Ersek <lersek@redhat.com>
>> Sent: Wednesday, September 16, 2020 7:07 PM
>> To: devel@edk2.groups.io; Zurcher, Christopher J
>> <christopher.j.zurcher@intel.com>
>> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J <jian.j.wang@intel.com>;
>> Lu, XiaoyuX <xiaoyux.lu@intel.com>
>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>> (Envelope) Digest interface
>>
>> Hello Christopher,
>>
>> On 09/16/20 02:59, Zurcher, Christopher J wrote:
>>> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
>>>
>>> The EVP interface should be used in place of discrete digest function
>>> calls.
>>>
>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>>> Cc: Jian J Wang <jian.j.wang@intel.com>
>>> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
>>> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
>>> ---
>>>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
>>>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
>>>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
>>>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
>>>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
>>>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
>> ++++++++++++++++++++
>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128 ++++++++++
>>>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128 ++++++++++
>>>  9 files changed, 647 insertions(+)
>>
>> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
>> is necessary. (If I understand correctly, that file was missing from
>> your v2 posting.)
>>
>> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
>> superfluous. This file is never referenced in the INF files.
>>
>> The point of this file would be to allow *some* of the Base / Pei /
>> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
>> only stub implementations). But this isn't what's happening -- all of
>> the Base / Pei / Runtime / Smm instances are getting the real deal
>> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
>>
>> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
>> should be dropped. Only the Null instance of the library needs null
>> versions of the new functions. The Base / Pei / Runtime / Smm instances
>> don't.
>>
>>>
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>> index 4aae2aba95..3968f29412 100644
>>> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>> @@ -50,6 +50,7 @@
>>>    Pk/CryptAuthenticode.c
>>>    Pk/CryptTs.c
>>>    Pem/CryptPem.c
>>> +  Evp/CryptEvpMd.c
>>>
>>>    SysCall/CrtWrapper.c
>>>    SysCall/TimerWrapper.c
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>> index dc28e3a11d..d0b91716d0 100644
>>> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>> @@ -57,6 +57,7 @@
>>>    Pk/CryptTsNull.c
>>>    Pem/CryptPemNull.c
>>>    Rand/CryptRandNull.c
>>> +  Evp/CryptEvpMd.c
>>>
>>>    SysCall/CrtWrapper.c
>>>    SysCall/ConstantTimeClock.c
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>> index 5005beed02..9f3accd35b 100644
>>> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>> @@ -56,6 +56,7 @@
>>>    Pk/CryptAuthenticodeNull.c
>>>    Pk/CryptTsNull.c
>>>    Pem/CryptPem.c
>>> +  Evp/CryptEvpMd.c
>>>
>>>    SysCall/CrtWrapper.c
>>>    SysCall/TimerWrapper.c
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>> index 91ec3e03bf..420623cdc6 100644
>>> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>> @@ -54,6 +54,7 @@
>>>    Pk/CryptAuthenticodeNull.c
>>>    Pk/CryptTsNull.c
>>>    Pem/CryptPem.c
>>> +  Evp/CryptEvpMd.c
>>>
>>>    SysCall/CrtWrapper.c
>>>    SysCall/ConstantTimeClock.c
>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>> index 689af4fedd..542ac2e2e1 100644
>>> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>> @@ -50,6 +50,7 @@
>>>    Pk/CryptTsNull.c
>>>    Pem/CryptPemNull.c
>>>    Rand/CryptRandNull.c
>>> +  Evp/CryptEvpMdNull.c
>>>
>>>  [Packages]
>>>    MdePkg/MdePkg.dec
>>> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
>> b/CryptoPkg/Include/Library/BaseCryptLib.h
>>> index ae9bde9e37..5e1b408b54 100644
>>> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
>>> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
>>> @@ -1012,6 +1012,135 @@ HmacSha256Final (
>>>    OUT     UINT8  *HmacValue
>>>    );
>>>
>>>
>> +//==============================================================
>> =======================
>>> +//    EVP (Envelope) Primitive
>>>
>> +//==============================================================
>> =======================
>>> +
>>> +/**
>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>> use.
>>> +
>>> +  If DigestName is NULL, then return FALSE.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>> terminated ASCII string.
>>> +                              Valid digest names are:
>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>> +                              SM3
>>> +
>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
>> initialized.
>>> +           If DigestName is invalid, returns NULL.
>>> +           If the allocations fails, returns NULL.
>>> +           If initialization fails, returns NULL.
>>> +
>>> +**/
>>> +VOID *
>>> +EFIAPI
>>> +EvpMdInit (
>>> +  IN  CONST CHAR8   *DigestName
>>> +  );
>>> +
>>> +/**
>>> +  Makes a copy of an existing EVP_MD context.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>> +
>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>> +
>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>> +  @retval FALSE  EVP_MD context copy failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdDuplicate (
>>> +  IN  CONST VOID    *EvpMdContext,
>>> +  OUT VOID          *NewEvpMdContext
>>> +  );
>>> +
>>> +/**
>>> +  Digests the input data and updates EVP_MD context.
>>> +
>>> +  This function performs EVP digest on a data buffer of the specified size.
>>> +  It can be called multiple times to compute the digest of long or
>> discontinuous data streams.
>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
>> should not
>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>> +
>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>> +  @param[in]       Data               Pointer to the buffer containing the data to be
>> digested.
>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>> +
>>> +  @retval TRUE   EVP data digest succeeded.
>>> +  @retval FALSE  EVP data digest failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdUpdate (
>>> +  IN OUT  VOID        *EvpMdContext,
>>> +  IN      CONST VOID  *Data,
>>> +  IN      UINTN       DataSize
>>> +  );
>>> +
>>> +/**
>>> +  Completes computation of the EVP digest value.
>>> +  Releases the specified EVP_MD_CTX context.
>>> +
>>> +  This function completes EVP hash computation and retrieves the digest
>> value into
>>> +  the specified memory. After this function has been called, the EVP context
>> cannot
>>> +  be used again.
>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>> should
>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>> undefined.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>> +
>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
>> value.
>>
>> (2) Please extend the comment on Digest with the following:
>>
>>   The caller is responsible for providing enough storage for the digest
>>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
>>   will suffice for storing the digest regardless of the algorithm chosen
>>   in EvpMdInit().
>>
>> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
>> advertise it to consumers in edk2.)
>>
>>> +
>>> +  @retval TRUE   EVP digest computation succeeded.
>>> +  @retval FALSE  EVP digest computation failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdFinal (
>>> +  IN OUT  VOID   *EvpMdContext,
>>> +  OUT     UINT8  *DigestValue
>>
>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
>> deliberately permitted (for just freeing the context).
>>
>>> +  );
>>> +
>>> +/**
>>> +  Computes the message digest of an input data buffer.
>>> +
>>> +  This function performs the message digest of a given data buffer, and places
>>> +  the digest value into the specified memory.
>>> +
>>> +  If DigestName is NULL, return FALSE.
>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>> +  If HashValue is NULL, return FALSE.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name.
>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>> hashed.
>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>> value.
>>> +
>>> +  @retval TRUE   Digest computation succeeded.
>>> +  @retval FALSE  Digest computation failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdHashAll (
>>> +  IN  CONST CHAR8   *DigestName,
>>> +  IN  CONST VOID    *Data,
>>> +  IN  UINTN         DataSize,
>>> +  OUT UINT8         *HashValue
>>> +  );
>>> +
>>>
>> //===============================================================
>> ======================
>>>  //    Symmetric Cryptography Primitive
>>>
>> //===============================================================
>> ======================
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>> new file mode 100644
>>> index 0000000000..b2770a9186
>>> --- /dev/null
>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>> @@ -0,0 +1,257 @@
>>> +/** @file
>>> +  EVP MD Wrapper Implementation for OpenSSL.
>>> +
>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>> +
>>> +**/
>>> +
>>> +#include "InternalCryptLib.h"
>>> +#include <openssl/evp.h>
>>> +
>>> +/**
>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>> use.
>>> +
>>> +  If DigestName is NULL, then return FALSE.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>> terminated ASCII string.
>>> +                              Valid digest names are:
>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>> +                              SM3
>>> +
>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
>> initialized.
>>> +           If DigestName is invalid, returns NULL.
>>> +           If the allocations fails, returns NULL.
>>> +           If initialization fails, returns NULL.
>>> +
>>> +**/
>>> +VOID *
>>> +EFIAPI
>>> +EvpMdInit (
>>> +  IN  CONST CHAR8   *DigestName
>>> +  )
>>> +{
>>> +  EVP_MD    *Digest;
>>> +  VOID      *EvpMdContext;
>>> +
>>> +  //
>>> +  // Check input parameters.
>>> +  //
>>> +  if (DigestName == NULL) {
>>> +    return NULL;
>>> +  }
>>> +
>>> +  //
>>> +  // Allocate EVP_MD_CTX Context
>>> +  //
>>> +  EvpMdContext = EVP_MD_CTX_new ();
>>> +  if (EvpMdContext == NULL) {
>>> +    return NULL;
>>> +  }
>>> +
>>> +  Digest = EVP_get_digestbyname (DigestName);
>>
>> I think this may not compile with gcc (and correctly so). The pointer
>> returned by EVP_get_digestbyname() is const-qualified, but with the
>> assignment, we're throwing away the const-ness.
>>
>> (4) Please const-qualify the "Digest" local pointer.
>>
>>> +  if (Digest == NULL) {
>>> +    return NULL;
>>> +  }
>>
>> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
>>
>> For keeping the control flow simple, consider moving
>> EVP_get_digestbyname() above EVP_MD_CTX_new().
>>
>>> +
>>> +  //
>>> +  // Initialize Context
>>> +  //
>>> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
>>> +    EVP_MD_CTX_free (EvpMdContext);
>>> +    return NULL;
>>> +  }
>>> +
>>> +  return EvpMdContext;
>>> +}
>>> +
>>> +/**
>>> +  Makes a copy of an existing EVP_MD context.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>> +
>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>> +
>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>> +  @retval FALSE  EVP_MD context copy failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdDuplicate (
>>> +  IN  CONST VOID    *EvpMdContext,
>>> +  OUT VOID          *NewEvpMdContext
>>> +  )
>>> +{
>>> +  //
>>> +  // Check input parameters.
>>> +  //
>>> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  return TRUE;
>>> +}
>>
>> (6) Can you please confirm that the caller is supposed to initialize
>> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
>>
>>> +
>>> +/**
>>> +  Digests the input data and updates EVP_MD context.
>>> +
>>> +  This function performs EVP digest on a data buffer of the specified size.
>>> +  It can be called multiple times to compute the digest of long or
>> discontinuous data streams.
>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(), and
>> should not
>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>> +
>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>> +  @param[in]       Data               Pointer to the buffer containing the data to be
>> digested.
>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>> +
>>> +  @retval TRUE   EVP data digest succeeded.
>>> +  @retval FALSE  EVP data digest failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdUpdate (
>>> +  IN OUT  VOID        *EvpMdContext,
>>> +  IN      CONST VOID  *Data,
>>> +  IN      UINTN       DataSize
>>> +  )
>>> +{
>>> +  //
>>> +  // Check input parameters.
>>> +  //
>>> +  if (EvpMdContext == NULL) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  //
>>> +  // Check invalid parameters, in case only DataLength was checked in
>> OpenSSL
>>> +  //
>>> +  if (Data == NULL && DataSize != 0) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  //
>>> +  // OpenSSL EVP digest update
>>> +  //
>>> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  return TRUE;
>>> +}
>>> +
>>> +/**
>>> +  Completes computation of the EVP digest value.
>>> +  Releases the specified EVP_MD_CTX context.
>>> +
>>> +  This function completes EVP hash computation and retrieves the digest
>> value into
>>> +  the specified memory. After this function has been called, the EVP context
>> cannot
>>> +  be used again.
>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>> should
>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>> undefined.
>>> +
>>> +  If EvpMdContext is NULL, then return FALSE.
>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>> +
>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
>> value.
>>> +
>>> +  @retval TRUE   EVP digest computation succeeded.
>>> +  @retval FALSE  EVP digest computation failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdFinal (
>>> +  IN OUT  VOID   *EvpMdContext,
>>> +  OUT     UINT8  *DigestValue
>>> +  )
>>> +{
>>> +  UINT32    Length;
>>> +  BOOLEAN   ReturnValue;
>>> +
>>> +  ReturnValue = TRUE;
>>> +
>>> +  //
>>> +  // Check input parameters.
>>> +  //
>>> +  if (EvpMdContext == NULL) {
>>> +    return FALSE;
>>> +  }
>>> +  if (DigestValue == NULL) {
>>> +    EVP_MD_CTX_free (EvpMdContext);
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  //
>>> +  // OpenSSL EVP digest finalization
>>> +  //
>>> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
>>> +    ReturnValue = FALSE;
>>> +  }
>>
>>
>> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
>> deals fine with the third parameter being NULL, according to the docs
>> (and the code).
>>
>>
>>> +
>>> +  //
>>> +  // Free OpenSSL EVP_MD_CTX Context
>>> +  //
>>> +  EVP_MD_CTX_free (EvpMdContext);
>>> +
>>> +  return ReturnValue;
>>> +}
>>> +
>>> +/**
>>> +  Computes the message digest of an input data buffer.
>>> +
>>> +  This function performs the message digest of a given data buffer, and places
>>> +  the digest value into the specified memory.
>>> +
>>> +  If DigestName is NULL, return FALSE.
>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>> +  If HashValue is NULL, return FALSE.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name.
>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>> hashed.
>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>> value.
>>> +
>>> +  @retval TRUE   Digest computation succeeded.
>>> +  @retval FALSE  Digest computation failed.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdHashAll (
>>> +  IN  CONST CHAR8   *DigestName,
>>> +  IN  CONST VOID    *Data,
>>> +  IN  UINTN         DataSize,
>>> +  OUT UINT8         *HashValue
>>> +  )
>>> +{
>>> +  BOOLEAN   Result;
>>> +  VOID      *EvpMdContext;
>>> +
>>> +  EvpMdContext = EvpMdInit (DigestName);
>>> +  if (EvpMdContext == NULL) {
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
>>> +  if (Result == FALSE) {
>>
>> (8) Style: please write (!Result).
>>
>>
>>> +    EvpMdFinal (EvpMdContext, NULL);
>>> +    return FALSE;
>>> +  }
>>> +
>>> +  Result = EvpMdFinal (EvpMdContext, HashValue);
>>> +
>>> +  return Result;
>>> +}
>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>> new file mode 100644
>>> index 0000000000..038f63801f
>>> --- /dev/null
>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>> @@ -0,0 +1,128 @@
>>> +/** @file
>>> +  EVP MD Wrapper Null Library.
>>> +
>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>> +
>>> +**/
>>> +
>>> +#include "InternalCryptLib.h"
>>> +
>>> +/**
>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>> use.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>> terminated ASCII string.
>>> +                              Valid digest names are:
>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>> +                              SM3
>>> +
>>> +  @return NULL  This interface is not supported.
>>> +
>>> +**/
>>> +VOID *
>>> +EFIAPI
>>> +EvpMdInit (
>>> +  IN  CONST CHAR8   *DigestName
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return NULL;
>>> +}
>>> +
>>> +/**
>>> +  Makes a copy of an existing EVP_MD context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdDuplicate (
>>> +  IN  CONST VOID    *EvpMdContext,
>>> +  OUT VOID          *NewEvpMdContext
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Digests the input data and updates EVP_MD context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>> +  @param[in]       Data               Pointer to the buffer containing the data to be
>> digested.
>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdUpdate (
>>> +  IN OUT  VOID        *EvpMdContext,
>>> +  IN      CONST VOID  *Data,
>>> +  IN      UINTN       DataSize
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Completes computation of the EVP digest value.
>>> +  Releases the specified EVP_MD_CTX context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
>> value.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdFinal (
>>> +  IN OUT  VOID   *EvpMdContext,
>>> +  OUT     UINT8  *DigestValue
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Computes the message digest of an input data buffer.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name.
>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>> hashed.
>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>> value.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdHashAll (
>>> +  IN  CONST CHAR8   *DigestName,
>>> +  IN  CONST VOID    *Data,
>>> +  IN  UINTN         DataSize,
>>> +  OUT UINT8         *HashValue
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>> new file mode 100644
>>> index 0000000000..038f63801f
>>> --- /dev/null
>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>> @@ -0,0 +1,128 @@
>>> +/** @file
>>> +  EVP MD Wrapper Null Library.
>>> +
>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>> +
>>> +**/
>>> +
>>> +#include "InternalCryptLib.h"
>>> +
>>> +/**
>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent EVP_MD
>> use.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>> terminated ASCII string.
>>> +                              Valid digest names are:
>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>> +                              SM3
>>> +
>>> +  @return NULL  This interface is not supported.
>>> +
>>> +**/
>>> +VOID *
>>> +EFIAPI
>>> +EvpMdInit (
>>> +  IN  CONST CHAR8   *DigestName
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return NULL;
>>> +}
>>> +
>>> +/**
>>> +  Makes a copy of an existing EVP_MD context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdDuplicate (
>>> +  IN  CONST VOID    *EvpMdContext,
>>> +  OUT VOID          *NewEvpMdContext
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Digests the input data and updates EVP_MD context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>> +  @param[in]       Data               Pointer to the buffer containing the data to be
>> digested.
>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdUpdate (
>>> +  IN OUT  VOID        *EvpMdContext,
>>> +  IN      CONST VOID  *Data,
>>> +  IN      UINTN       DataSize
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Completes computation of the EVP digest value.
>>> +  Releases the specified EVP_MD_CTX context.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP digest
>> value.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdFinal (
>>> +  IN OUT  VOID   *EvpMdContext,
>>> +  OUT     UINT8  *DigestValue
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>> +
>>> +/**
>>> +  Computes the message digest of an input data buffer.
>>> +
>>> +  Return FALSE to indicate this interface is not supported.
>>> +
>>> +  @param[in]    DigestName    Pointer to the digest name.
>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>> hashed.
>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>> value.
>>> +
>>> +  @retval FALSE  This interface is not supported.
>>> +
>>> +**/
>>> +BOOLEAN
>>> +EFIAPI
>>> +EvpMdHashAll (
>>> +  IN  CONST CHAR8   *DigestName,
>>> +  IN  CONST VOID    *Data,
>>> +  IN  UINTN         DataSize,
>>> +  OUT UINT8         *HashValue
>>> +  )
>>> +{
>>> +  ASSERT (FALSE);
>>> +  return FALSE;
>>> +}
>>>
>>
>> Thanks,
>> Laszlo
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 13:35       ` Laszlo Ersek
@ 2020-09-16 14:17         ` Yao, Jiewen
  2020-09-16 16:04           ` Laszlo Ersek
  0 siblings, 1 reply; 13+ messages in thread
From: Yao, Jiewen @ 2020-09-16 14:17 UTC (permalink / raw)
  To: Laszlo Ersek, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

Thank you Laszlo.
You forced me to read the code again and more carefully. :-)

I believe I understand why you use NULL to free the context now - to support error path.

First, I did have some new thought for EVP API. Let’s consider 3 cases, 1) Hash all data one time, 2) Hash update data multiple times, 3) Error during update.

A. In current design, the API sequence is:
1) EvpMdHashAll (Digest)
2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
3) EvpMdInit, EvpMdUpdate->ERROR, EvpMdFinal (NULL)

B. We can auto free context in EvpMdUpdate in error path  - the API sequence is:
1) EvpMdHashAll (Digest)
2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
3) EvpMdInit, EvpMdUpdate->ERROR

C: We can use New/Free style - similar to HMAC
1) EvpMdHashAll (Digest)
2) EvpMdNew, EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest), EvpMdFree
3) EvpMdNew, EvpMdInit, EvpMdUpdate->ERROR, EvpMdFree

I checked below APIs:
1) openssl (EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_free)
2) mbedtls (mbedtls_sha256_init, mbedtls_sha256_starts_ret, mbedtls_sha256_update_ret, mbedtls_sha256_finish_ret, mbedtls_sha256_free)
3) BaseCryptoLib HMAC (HmacSha256New, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Free)

All APIs include free function to free the context, I don’t think it is a bad idea to have EvpMdFree() API.
I would like to propose option - C.


Second, I believe EVP is just something in openssl. It does not mean that we *have to* design API to follow openssl.
After rethink the size impact, I do have concern to merge all Hash implementation together in one function.
It might means nothing if the crypto library is based upon openssl.
But if the cryptolib implementation is based upon other crypto, such as Intel IPP (https://github.com/slimbootloader/slimbootloader/tree/master/BootloaderCommonPkg/Library/IppCryptoLib) or mbedTls (https://github.com/jyao1/edk2/tree/DeviceSecurity/CryptoMbedTlsPkg), then we can NOT get size benefit by separating the hash algorithm.


I would like to propose separate EvpMdxxx.
EvpMdNew -> Sha256New, Sha384New
EvpMdInit -> Sha256Init, Sha384Init
EvpMdUpdate -> Sha256Update, Sha384Update
EvpMdFinal -> Sha256Final, Sha384Final
EvpMdFree -> Sha256Free, Sha384Free

We can do similar thing with Hmac, to deprecate Sha256GetContextSize() API, and replace caller with Sha256New() / Sha384Free()

Thank you
Yao Jiewen


> -----Original Message-----
> From: Laszlo Ersek <lersek@redhat.com>
> Sent: Wednesday, September 16, 2020 9:36 PM
> To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher,
> Christopher J <christopher.j.zurcher@intel.com>
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> 
> On 09/16/20 13:44, Yao, Jiewen wrote:
> > Hi Laszlo
> > I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
> > If we need follow this, then we need add OPTIONAL to almost all pointer,
> which is unnecessary.
> 
> I'm not suggesting OPTIONAL *only* because NULL is checked. I'm
> suggesting OPTIONAL because there is a specific use case related to
> that. Please see the comments on the function:
> 
> "If DigestValue is NULL, free the Context then return FALSE."
> 
> But, anyway, I don't insist. It's not a huge deal. Feel free to ignore
> my comment regarding OPTIONAL.
> 
> Thanks
> Laszlo
> 
> 
> >
> >> +BOOLEAN
> >> +EFIAPI
> >> +EvpMdFinal (
> >> +  IN OUT  VOID   *EvpMdContext,
> >> +  OUT     UINT8  *DigestValue
> >
> > (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> > deliberately permitted (for just freeing the context).
> >
> >
> >
> >
> >> -----Original Message-----
> >> From: Laszlo Ersek <lersek@redhat.com>
> >> Sent: Wednesday, September 16, 2020 7:07 PM
> >> To: devel@edk2.groups.io; Zurcher, Christopher J
> >> <christopher.j.zurcher@intel.com>
> >> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>;
> >> Lu, XiaoyuX <xiaoyux.lu@intel.com>
> >> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> >> (Envelope) Digest interface
> >>
> >> Hello Christopher,
> >>
> >> On 09/16/20 02:59, Zurcher, Christopher J wrote:
> >>> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
> >>>
> >>> The EVP interface should be used in place of discrete digest function
> >>> calls.
> >>>
> >>> Cc: Laszlo Ersek <lersek@redhat.com>
> >>> Cc: Jiewen Yao <jiewen.yao@intel.com>
> >>> Cc: Jian J Wang <jian.j.wang@intel.com>
> >>> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
> >>> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
> >>> ---
> >>>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
> >>>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
> >>>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
> >>>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
> >>>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
> >>>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
> >>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
> >> ++++++++++++++++++++
> >>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128
> ++++++++++
> >>>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128
> ++++++++++
> >>>  9 files changed, 647 insertions(+)
> >>
> >> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
> >> is necessary. (If I understand correctly, that file was missing from
> >> your v2 posting.)
> >>
> >> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
> >> superfluous. This file is never referenced in the INF files.
> >>
> >> The point of this file would be to allow *some* of the Base / Pei /
> >> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
> >> only stub implementations). But this isn't what's happening -- all of
> >> the Base / Pei / Runtime / Smm instances are getting the real deal
> >> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
> >>
> >> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
> >> should be dropped. Only the Null instance of the library needs null
> >> versions of the new functions. The Base / Pei / Runtime / Smm instances
> >> don't.
> >>
> >>>
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>> index 4aae2aba95..3968f29412 100644
> >>> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
> >>> @@ -50,6 +50,7 @@
> >>>    Pk/CryptAuthenticode.c
> >>>    Pk/CryptTs.c
> >>>    Pem/CryptPem.c
> >>> +  Evp/CryptEvpMd.c
> >>>
> >>>    SysCall/CrtWrapper.c
> >>>    SysCall/TimerWrapper.c
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>> index dc28e3a11d..d0b91716d0 100644
> >>> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
> >>> @@ -57,6 +57,7 @@
> >>>    Pk/CryptTsNull.c
> >>>    Pem/CryptPemNull.c
> >>>    Rand/CryptRandNull.c
> >>> +  Evp/CryptEvpMd.c
> >>>
> >>>    SysCall/CrtWrapper.c
> >>>    SysCall/ConstantTimeClock.c
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>> index 5005beed02..9f3accd35b 100644
> >>> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
> >>> @@ -56,6 +56,7 @@
> >>>    Pk/CryptAuthenticodeNull.c
> >>>    Pk/CryptTsNull.c
> >>>    Pem/CryptPem.c
> >>> +  Evp/CryptEvpMd.c
> >>>
> >>>    SysCall/CrtWrapper.c
> >>>    SysCall/TimerWrapper.c
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>> index 91ec3e03bf..420623cdc6 100644
> >>> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
> >>> @@ -54,6 +54,7 @@
> >>>    Pk/CryptAuthenticodeNull.c
> >>>    Pk/CryptTsNull.c
> >>>    Pem/CryptPem.c
> >>> +  Evp/CryptEvpMd.c
> >>>
> >>>    SysCall/CrtWrapper.c
> >>>    SysCall/ConstantTimeClock.c
> >>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>> index 689af4fedd..542ac2e2e1 100644
> >>> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
> >>> @@ -50,6 +50,7 @@
> >>>    Pk/CryptTsNull.c
> >>>    Pem/CryptPemNull.c
> >>>    Rand/CryptRandNull.c
> >>> +  Evp/CryptEvpMdNull.c
> >>>
> >>>  [Packages]
> >>>    MdePkg/MdePkg.dec
> >>> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
> >> b/CryptoPkg/Include/Library/BaseCryptLib.h
> >>> index ae9bde9e37..5e1b408b54 100644
> >>> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
> >>> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
> >>> @@ -1012,6 +1012,135 @@ HmacSha256Final (
> >>>    OUT     UINT8  *HmacValue
> >>>    );
> >>>
> >>>
> >>
> +//==============================================================
> >> =======================
> >>> +//    EVP (Envelope) Primitive
> >>>
> >>
> +//==============================================================
> >> =======================
> >>> +
> >>> +/**
> >>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >> use.
> >>> +
> >>> +  If DigestName is NULL, then return FALSE.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >> terminated ASCII string.
> >>> +                              Valid digest names are:
> >>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>> +                              SM3
> >>> +
> >>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> >> initialized.
> >>> +           If DigestName is invalid, returns NULL.
> >>> +           If the allocations fails, returns NULL.
> >>> +           If initialization fails, returns NULL.
> >>> +
> >>> +**/
> >>> +VOID *
> >>> +EFIAPI
> >>> +EvpMdInit (
> >>> +  IN  CONST CHAR8   *DigestName
> >>> +  );
> >>> +
> >>> +/**
> >>> +  Makes a copy of an existing EVP_MD context.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If NewEvpMdContext is NULL, then return FALSE.
> >>> +
> >>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>> +
> >>> +  @retval TRUE   EVP_MD context copy succeeded.
> >>> +  @retval FALSE  EVP_MD context copy failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdDuplicate (
> >>> +  IN  CONST VOID    *EvpMdContext,
> >>> +  OUT VOID          *NewEvpMdContext
> >>> +  );
> >>> +
> >>> +/**
> >>> +  Digests the input data and updates EVP_MD context.
> >>> +
> >>> +  This function performs EVP digest on a data buffer of the specified size.
> >>> +  It can be called multiple times to compute the digest of long or
> >> discontinuous data streams.
> >>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
> and
> >> should not
> >>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>> +
> >>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>> +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> >> digested.
> >>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>> +
> >>> +  @retval TRUE   EVP data digest succeeded.
> >>> +  @retval FALSE  EVP data digest failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdUpdate (
> >>> +  IN OUT  VOID        *EvpMdContext,
> >>> +  IN      CONST VOID  *Data,
> >>> +  IN      UINTN       DataSize
> >>> +  );
> >>> +
> >>> +/**
> >>> +  Completes computation of the EVP digest value.
> >>> +  Releases the specified EVP_MD_CTX context.
> >>> +
> >>> +  This function completes EVP hash computation and retrieves the digest
> >> value into
> >>> +  the specified memory. After this function has been called, the EVP context
> >> cannot
> >>> +  be used again.
> >>> +  EVP context should be already correctly initialized by EvpMdInit(), and
> >> should
> >>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> >> undefined.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If DigestValue is NULL, free the Context then return FALSE.
> >>> +
> >>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> >> value.
> >>
> >> (2) Please extend the comment on Digest with the following:
> >>
> >>   The caller is responsible for providing enough storage for the digest
> >>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
> >>   will suffice for storing the digest regardless of the algorithm chosen
> >>   in EvpMdInit().
> >>
> >> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
> >> advertise it to consumers in edk2.)
> >>
> >>> +
> >>> +  @retval TRUE   EVP digest computation succeeded.
> >>> +  @retval FALSE  EVP digest computation failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdFinal (
> >>> +  IN OUT  VOID   *EvpMdContext,
> >>> +  OUT     UINT8  *DigestValue
> >>
> >> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
> >> deliberately permitted (for just freeing the context).
> >>
> >>> +  );
> >>> +
> >>> +/**
> >>> +  Computes the message digest of an input data buffer.
> >>> +
> >>> +  This function performs the message digest of a given data buffer, and
> places
> >>> +  the digest value into the specified memory.
> >>> +
> >>> +  If DigestName is NULL, return FALSE.
> >>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>> +  If HashValue is NULL, return FALSE.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name.
> >>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >> hashed.
> >>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >> value.
> >>> +
> >>> +  @retval TRUE   Digest computation succeeded.
> >>> +  @retval FALSE  Digest computation failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdHashAll (
> >>> +  IN  CONST CHAR8   *DigestName,
> >>> +  IN  CONST VOID    *Data,
> >>> +  IN  UINTN         DataSize,
> >>> +  OUT UINT8         *HashValue
> >>> +  );
> >>> +
> >>>
> >>
> //===============================================================
> >> ======================
> >>>  //    Symmetric Cryptography Primitive
> >>>
> >>
> //===============================================================
> >> ======================
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >>> new file mode 100644
> >>> index 0000000000..b2770a9186
> >>> --- /dev/null
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
> >>> @@ -0,0 +1,257 @@
> >>> +/** @file
> >>> +  EVP MD Wrapper Implementation for OpenSSL.
> >>> +
> >>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>> +
> >>> +**/
> >>> +
> >>> +#include "InternalCryptLib.h"
> >>> +#include <openssl/evp.h>
> >>> +
> >>> +/**
> >>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >> use.
> >>> +
> >>> +  If DigestName is NULL, then return FALSE.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >> terminated ASCII string.
> >>> +                              Valid digest names are:
> >>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>> +                              SM3
> >>> +
> >>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated and
> >> initialized.
> >>> +           If DigestName is invalid, returns NULL.
> >>> +           If the allocations fails, returns NULL.
> >>> +           If initialization fails, returns NULL.
> >>> +
> >>> +**/
> >>> +VOID *
> >>> +EFIAPI
> >>> +EvpMdInit (
> >>> +  IN  CONST CHAR8   *DigestName
> >>> +  )
> >>> +{
> >>> +  EVP_MD    *Digest;
> >>> +  VOID      *EvpMdContext;
> >>> +
> >>> +  //
> >>> +  // Check input parameters.
> >>> +  //
> >>> +  if (DigestName == NULL) {
> >>> +    return NULL;
> >>> +  }
> >>> +
> >>> +  //
> >>> +  // Allocate EVP_MD_CTX Context
> >>> +  //
> >>> +  EvpMdContext = EVP_MD_CTX_new ();
> >>> +  if (EvpMdContext == NULL) {
> >>> +    return NULL;
> >>> +  }
> >>> +
> >>> +  Digest = EVP_get_digestbyname (DigestName);
> >>
> >> I think this may not compile with gcc (and correctly so). The pointer
> >> returned by EVP_get_digestbyname() is const-qualified, but with the
> >> assignment, we're throwing away the const-ness.
> >>
> >> (4) Please const-qualify the "Digest" local pointer.
> >>
> >>> +  if (Digest == NULL) {
> >>> +    return NULL;
> >>> +  }
> >>
> >> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
> >>
> >> For keeping the control flow simple, consider moving
> >> EVP_get_digestbyname() above EVP_MD_CTX_new().
> >>
> >>> +
> >>> +  //
> >>> +  // Initialize Context
> >>> +  //
> >>> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
> >>> +    EVP_MD_CTX_free (EvpMdContext);
> >>> +    return NULL;
> >>> +  }
> >>> +
> >>> +  return EvpMdContext;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Makes a copy of an existing EVP_MD context.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If NewEvpMdContext is NULL, then return FALSE.
> >>> +
> >>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>> +
> >>> +  @retval TRUE   EVP_MD context copy succeeded.
> >>> +  @retval FALSE  EVP_MD context copy failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdDuplicate (
> >>> +  IN  CONST VOID    *EvpMdContext,
> >>> +  OUT VOID          *NewEvpMdContext
> >>> +  )
> >>> +{
> >>> +  //
> >>> +  // Check input parameters.
> >>> +  //
> >>> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  return TRUE;
> >>> +}
> >>
> >> (6) Can you please confirm that the caller is supposed to initialize
> >> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
> >>
> >>> +
> >>> +/**
> >>> +  Digests the input data and updates EVP_MD context.
> >>> +
> >>> +  This function performs EVP digest on a data buffer of the specified size.
> >>> +  It can be called multiple times to compute the digest of long or
> >> discontinuous data streams.
> >>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
> and
> >> should not
> >>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>> +
> >>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>> +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> >> digested.
> >>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>> +
> >>> +  @retval TRUE   EVP data digest succeeded.
> >>> +  @retval FALSE  EVP data digest failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdUpdate (
> >>> +  IN OUT  VOID        *EvpMdContext,
> >>> +  IN      CONST VOID  *Data,
> >>> +  IN      UINTN       DataSize
> >>> +  )
> >>> +{
> >>> +  //
> >>> +  // Check input parameters.
> >>> +  //
> >>> +  if (EvpMdContext == NULL) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  //
> >>> +  // Check invalid parameters, in case only DataLength was checked in
> >> OpenSSL
> >>> +  //
> >>> +  if (Data == NULL && DataSize != 0) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  //
> >>> +  // OpenSSL EVP digest update
> >>> +  //
> >>> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  return TRUE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Completes computation of the EVP digest value.
> >>> +  Releases the specified EVP_MD_CTX context.
> >>> +
> >>> +  This function completes EVP hash computation and retrieves the digest
> >> value into
> >>> +  the specified memory. After this function has been called, the EVP context
> >> cannot
> >>> +  be used again.
> >>> +  EVP context should be already correctly initialized by EvpMdInit(), and
> >> should
> >>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
> >> undefined.
> >>> +
> >>> +  If EvpMdContext is NULL, then return FALSE.
> >>> +  If DigestValue is NULL, free the Context then return FALSE.
> >>> +
> >>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> >> value.
> >>> +
> >>> +  @retval TRUE   EVP digest computation succeeded.
> >>> +  @retval FALSE  EVP digest computation failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdFinal (
> >>> +  IN OUT  VOID   *EvpMdContext,
> >>> +  OUT     UINT8  *DigestValue
> >>> +  )
> >>> +{
> >>> +  UINT32    Length;
> >>> +  BOOLEAN   ReturnValue;
> >>> +
> >>> +  ReturnValue = TRUE;
> >>> +
> >>> +  //
> >>> +  // Check input parameters.
> >>> +  //
> >>> +  if (EvpMdContext == NULL) {
> >>> +    return FALSE;
> >>> +  }
> >>> +  if (DigestValue == NULL) {
> >>> +    EVP_MD_CTX_free (EvpMdContext);
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  //
> >>> +  // OpenSSL EVP digest finalization
> >>> +  //
> >>> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
> >>> +    ReturnValue = FALSE;
> >>> +  }
> >>
> >>
> >> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
> >> deals fine with the third parameter being NULL, according to the docs
> >> (and the code).
> >>
> >>
> >>> +
> >>> +  //
> >>> +  // Free OpenSSL EVP_MD_CTX Context
> >>> +  //
> >>> +  EVP_MD_CTX_free (EvpMdContext);
> >>> +
> >>> +  return ReturnValue;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Computes the message digest of an input data buffer.
> >>> +
> >>> +  This function performs the message digest of a given data buffer, and
> places
> >>> +  the digest value into the specified memory.
> >>> +
> >>> +  If DigestName is NULL, return FALSE.
> >>> +  If Data is NULL and DataSize is not zero, return FALSE.
> >>> +  If HashValue is NULL, return FALSE.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name.
> >>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >> hashed.
> >>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >> value.
> >>> +
> >>> +  @retval TRUE   Digest computation succeeded.
> >>> +  @retval FALSE  Digest computation failed.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdHashAll (
> >>> +  IN  CONST CHAR8   *DigestName,
> >>> +  IN  CONST VOID    *Data,
> >>> +  IN  UINTN         DataSize,
> >>> +  OUT UINT8         *HashValue
> >>> +  )
> >>> +{
> >>> +  BOOLEAN   Result;
> >>> +  VOID      *EvpMdContext;
> >>> +
> >>> +  EvpMdContext = EvpMdInit (DigestName);
> >>> +  if (EvpMdContext == NULL) {
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
> >>> +  if (Result == FALSE) {
> >>
> >> (8) Style: please write (!Result).
> >>
> >>
> >>> +    EvpMdFinal (EvpMdContext, NULL);
> >>> +    return FALSE;
> >>> +  }
> >>> +
> >>> +  Result = EvpMdFinal (EvpMdContext, HashValue);
> >>> +
> >>> +  return Result;
> >>> +}
> >>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >>> new file mode 100644
> >>> index 0000000000..038f63801f
> >>> --- /dev/null
> >>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
> >>> @@ -0,0 +1,128 @@
> >>> +/** @file
> >>> +  EVP MD Wrapper Null Library.
> >>> +
> >>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>> +
> >>> +**/
> >>> +
> >>> +#include "InternalCryptLib.h"
> >>> +
> >>> +/**
> >>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >> use.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >> terminated ASCII string.
> >>> +                              Valid digest names are:
> >>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>> +                              SM3
> >>> +
> >>> +  @return NULL  This interface is not supported.
> >>> +
> >>> +**/
> >>> +VOID *
> >>> +EFIAPI
> >>> +EvpMdInit (
> >>> +  IN  CONST CHAR8   *DigestName
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return NULL;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Makes a copy of an existing EVP_MD context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdDuplicate (
> >>> +  IN  CONST VOID    *EvpMdContext,
> >>> +  OUT VOID          *NewEvpMdContext
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Digests the input data and updates EVP_MD context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>> +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> >> digested.
> >>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdUpdate (
> >>> +  IN OUT  VOID        *EvpMdContext,
> >>> +  IN      CONST VOID  *Data,
> >>> +  IN      UINTN       DataSize
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Completes computation of the EVP digest value.
> >>> +  Releases the specified EVP_MD_CTX context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> >> value.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdFinal (
> >>> +  IN OUT  VOID   *EvpMdContext,
> >>> +  OUT     UINT8  *DigestValue
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Computes the message digest of an input data buffer.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name.
> >>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >> hashed.
> >>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >> value.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdHashAll (
> >>> +  IN  CONST CHAR8   *DigestName,
> >>> +  IN  CONST VOID    *Data,
> >>> +  IN  UINTN         DataSize,
> >>> +  OUT UINT8         *HashValue
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >>> new file mode 100644
> >>> index 0000000000..038f63801f
> >>> --- /dev/null
> >>> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
> >>> @@ -0,0 +1,128 @@
> >>> +/** @file
> >>> +  EVP MD Wrapper Null Library.
> >>> +
> >>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> >>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>> +
> >>> +**/
> >>> +
> >>> +#include "InternalCryptLib.h"
> >>> +
> >>> +/**
> >>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
> EVP_MD
> >> use.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
> >> terminated ASCII string.
> >>> +                              Valid digest names are:
> >>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
> >>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
> >>> +                              SM3
> >>> +
> >>> +  @return NULL  This interface is not supported.
> >>> +
> >>> +**/
> >>> +VOID *
> >>> +EFIAPI
> >>> +EvpMdInit (
> >>> +  IN  CONST CHAR8   *DigestName
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return NULL;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Makes a copy of an existing EVP_MD context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
> >>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdDuplicate (
> >>> +  IN  CONST VOID    *EvpMdContext,
> >>> +  OUT VOID          *NewEvpMdContext
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Digests the input data and updates EVP_MD context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
> >>> +  @param[in]       Data               Pointer to the buffer containing the data to
> be
> >> digested.
> >>> +  @param[in]       DataSize           Size of Data buffer in bytes.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdUpdate (
> >>> +  IN OUT  VOID        *EvpMdContext,
> >>> +  IN      CONST VOID  *Data,
> >>> +  IN      UINTN       DataSize
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Completes computation of the EVP digest value.
> >>> +  Releases the specified EVP_MD_CTX context.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
> >>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
> digest
> >> value.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdFinal (
> >>> +  IN OUT  VOID   *EvpMdContext,
> >>> +  OUT     UINT8  *DigestValue
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>> +
> >>> +/**
> >>> +  Computes the message digest of an input data buffer.
> >>> +
> >>> +  Return FALSE to indicate this interface is not supported.
> >>> +
> >>> +  @param[in]    DigestName    Pointer to the digest name.
> >>> +  @param[in]    Data          Pointer to the buffer containing the data to be
> >> hashed.
> >>> +  @param[in]    DataSize      Size of Data buffer in bytes.
> >>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
> >> value.
> >>> +
> >>> +  @retval FALSE  This interface is not supported.
> >>> +
> >>> +**/
> >>> +BOOLEAN
> >>> +EFIAPI
> >>> +EvpMdHashAll (
> >>> +  IN  CONST CHAR8   *DigestName,
> >>> +  IN  CONST VOID    *Data,
> >>> +  IN  UINTN         DataSize,
> >>> +  OUT UINT8         *HashValue
> >>> +  )
> >>> +{
> >>> +  ASSERT (FALSE);
> >>> +  return FALSE;
> >>> +}
> >>>
> >>
> >> Thanks,
> >> Laszlo
> >


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 12:56   ` Yao, Jiewen
@ 2020-09-16 15:27     ` Laszlo Ersek
  0 siblings, 0 replies; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 15:27 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

On 09/16/20 14:56, Yao, Jiewen wrote:
> I try to understand what is the use case to EvpMdFinal without DigestValue and only Free the EvpMdContext.

Error paths. You got some hashing going, detect an error condition
(related to the hashing itself, or to something else), and want to abort
/ clean up the hashing. You need to release the context, but don't care
about the digest calculated thus far.
> 
> A normal usage will be:
> 1) EvpMdHashAll (Digest) - get digest.
> 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest) - get digest
> 
> Why we need support the case to use EvpMdFinal (NULL) in the end to free context?

We don't strictly need it. In the above (error path) use case, we can
still pass a pointer to an actual digest-receiving object, and then go
on to ignore the contents of that object. Passing NULL in that case
improves readability though.

Thanks
Laszlo

> 
> 
> Thank you
> Yao Jiewen
> 
>> -----Original Message-----
>> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Laszlo Ersek
>> Sent: Wednesday, September 16, 2020 8:40 PM
>> To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher,
>> Christopher J <christopher.j.zurcher@intel.com>
>> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>> (Envelope) Digest interface
>>
>> On 09/16/20 14:07, Yao, Jiewen wrote:
>>> I overlooked the behavior that NULL DigestValue will take side effect to free
>> Context.
>>>
>>> Hi Christopher
>>> May I understand why we need free the context?
>>> This does not align with other existing HASH or HMAC functions.
>>
>> I requested that.
>>
>> It makes no sense to keep a finalized context.
>>
>> In other words, it makes no sense to separate finalization from freeing.
>> A context that has been finalized is unusable for anything except freeing.
>>
>> Thanks
>> Laszlo
>>
>>>
>>> Thank you
>>> Yao Jiewen
>>>
>>>> -----Original Message-----
>>>> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yao,
>> Jiewen
>>>> Sent: Wednesday, September 16, 2020 7:45 PM
>>>> To: Laszlo Ersek <lersek@redhat.com>; devel@edk2.groups.io; Zurcher,
>>>> Christopher J <christopher.j.zurcher@intel.com>
>>>> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX
>> <xiaoyux.lu@intel.com>
>>>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>>>> (Envelope) Digest interface
>>>>
>>>> Hi Laszlo
>>>> I disagree to put OPTIONAL for DigestValue, just because NULL is checked.
>>>> If we need follow this, then we need add OPTIONAL to almost all pointer,
>> which
>>>> is unnecessary.
>>>>
>>>>> +BOOLEAN
>>>>> +EFIAPI
>>>>> +EvpMdFinal (
>>>>> +  IN OUT  VOID   *EvpMdContext,
>>>>> +  OUT     UINT8  *DigestValue
>>>>
>>>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
>>>> deliberately permitted (for just freeing the context).
>>>>
>>>>
>>>>
>>>>
>>>>> -----Original Message-----
>>>>> From: Laszlo Ersek <lersek@redhat.com>
>>>>> Sent: Wednesday, September 16, 2020 7:07 PM
>>>>> To: devel@edk2.groups.io; Zurcher, Christopher J
>>>>> <christopher.j.zurcher@intel.com>
>>>>> Cc: Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian J
>>>> <jian.j.wang@intel.com>;
>>>>> Lu, XiaoyuX <xiaoyux.lu@intel.com>
>>>>> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
>>>>> (Envelope) Digest interface
>>>>>
>>>>> Hello Christopher,
>>>>>
>>>>> On 09/16/20 02:59, Zurcher, Christopher J wrote:
>>>>>> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2545
>>>>>>
>>>>>> The EVP interface should be used in place of discrete digest function
>>>>>> calls.
>>>>>>
>>>>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>>>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>>>>>> Cc: Jian J Wang <jian.j.wang@intel.com>
>>>>>> Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
>>>>>> Signed-off-by: Christopher J Zurcher <christopher.j.zurcher@intel.com>
>>>>>> ---
>>>>>>  CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf         |   1 +
>>>>>>  CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf          |   1 +
>>>>>>  CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf      |   1 +
>>>>>>  CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf          |   1 +
>>>>>>  CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf |   1 +
>>>>>>  CryptoPkg/Include/Library/BaseCryptLib.h                | 129 ++++++++++
>>>>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c         | 257
>>>>> ++++++++++++++++++++
>>>>>>  CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c     | 128
>> ++++++++++
>>>>>>  CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c | 128
>>>> ++++++++++
>>>>>>  9 files changed, 647 insertions(+)
>>>>>
>>>>> I agree that "CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c"
>>>>> is necessary. (If I understand correctly, that file was missing from
>>>>> your v2 posting.)
>>>>>
>>>>> But "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c" seems
>>>>> superfluous. This file is never referenced in the INF files.
>>>>>
>>>>> The point of this file would be to allow *some* of the Base / Pei /
>>>>> Runtime / Smm instances to "stub out" the EVP MD functions (i.e. provide
>>>>> only stub implementations). But this isn't what's happening -- all of
>>>>> the Base / Pei / Runtime / Smm instances are getting the real deal
>>>>> ("CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c").
>>>>>
>>>>> (1) So I think "CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c"
>>>>> should be dropped. Only the Null instance of the library needs null
>>>>> versions of the new functions. The Base / Pei / Runtime / Smm instances
>>>>> don't.
>>>>>
>>>>>>
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>>> b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>>>> index 4aae2aba95..3968f29412 100644
>>>>>> --- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
>>>>>> @@ -50,6 +50,7 @@
>>>>>>    Pk/CryptAuthenticode.c
>>>>>>    Pk/CryptTs.c
>>>>>>    Pem/CryptPem.c
>>>>>> +  Evp/CryptEvpMd.c
>>>>>>
>>>>>>    SysCall/CrtWrapper.c
>>>>>>    SysCall/TimerWrapper.c
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>>> b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>>>> index dc28e3a11d..d0b91716d0 100644
>>>>>> --- a/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
>>>>>> @@ -57,6 +57,7 @@
>>>>>>    Pk/CryptTsNull.c
>>>>>>    Pem/CryptPemNull.c
>>>>>>    Rand/CryptRandNull.c
>>>>>> +  Evp/CryptEvpMd.c
>>>>>>
>>>>>>    SysCall/CrtWrapper.c
>>>>>>    SysCall/ConstantTimeClock.c
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>>> b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>>>> index 5005beed02..9f3accd35b 100644
>>>>>> --- a/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
>>>>>> @@ -56,6 +56,7 @@
>>>>>>    Pk/CryptAuthenticodeNull.c
>>>>>>    Pk/CryptTsNull.c
>>>>>>    Pem/CryptPem.c
>>>>>> +  Evp/CryptEvpMd.c
>>>>>>
>>>>>>    SysCall/CrtWrapper.c
>>>>>>    SysCall/TimerWrapper.c
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>>> b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>>>> index 91ec3e03bf..420623cdc6 100644
>>>>>> --- a/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
>>>>>> @@ -54,6 +54,7 @@
>>>>>>    Pk/CryptAuthenticodeNull.c
>>>>>>    Pk/CryptTsNull.c
>>>>>>    Pem/CryptPem.c
>>>>>> +  Evp/CryptEvpMd.c
>>>>>>
>>>>>>    SysCall/CrtWrapper.c
>>>>>>    SysCall/ConstantTimeClock.c
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>>> b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>>>> index 689af4fedd..542ac2e2e1 100644
>>>>>> --- a/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
>>>>>> @@ -50,6 +50,7 @@
>>>>>>    Pk/CryptTsNull.c
>>>>>>    Pem/CryptPemNull.c
>>>>>>    Rand/CryptRandNull.c
>>>>>> +  Evp/CryptEvpMdNull.c
>>>>>>
>>>>>>  [Packages]
>>>>>>    MdePkg/MdePkg.dec
>>>>>> diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h
>>>>> b/CryptoPkg/Include/Library/BaseCryptLib.h
>>>>>> index ae9bde9e37..5e1b408b54 100644
>>>>>> --- a/CryptoPkg/Include/Library/BaseCryptLib.h
>>>>>> +++ b/CryptoPkg/Include/Library/BaseCryptLib.h
>>>>>> @@ -1012,6 +1012,135 @@ HmacSha256Final (
>>>>>>    OUT     UINT8  *HmacValue
>>>>>>    );
>>>>>>
>>>>>>
>>>>>
>>>>
>> +//==============================================================
>>>>> =======================
>>>>>> +//    EVP (Envelope) Primitive
>>>>>>
>>>>>
>>>>
>> +//==============================================================
>>>>> =======================
>>>>>> +
>>>>>> +/**
>>>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
>> EVP_MD
>>>>> use.
>>>>>> +
>>>>>> +  If DigestName is NULL, then return FALSE.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>>>> terminated ASCII string.
>>>>>> +                              Valid digest names are:
>>>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>>>> +                              SM3
>>>>>> +
>>>>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated
>> and
>>>>> initialized.
>>>>>> +           If DigestName is invalid, returns NULL.
>>>>>> +           If the allocations fails, returns NULL.
>>>>>> +           If initialization fails, returns NULL.
>>>>>> +
>>>>>> +**/
>>>>>> +VOID *
>>>>>> +EFIAPI
>>>>>> +EvpMdInit (
>>>>>> +  IN  CONST CHAR8   *DigestName
>>>>>> +  );
>>>>>> +
>>>>>> +/**
>>>>>> +  Makes a copy of an existing EVP_MD context.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>>>>> +
>>>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>>>> +
>>>>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>>>>> +  @retval FALSE  EVP_MD context copy failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdDuplicate (
>>>>>> +  IN  CONST VOID    *EvpMdContext,
>>>>>> +  OUT VOID          *NewEvpMdContext
>>>>>> +  );
>>>>>> +
>>>>>> +/**
>>>>>> +  Digests the input data and updates EVP_MD context.
>>>>>> +
>>>>>> +  This function performs EVP digest on a data buffer of the specified size.
>>>>>> +  It can be called multiple times to compute the digest of long or
>>>>> discontinuous data streams.
>>>>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
>> and
>>>>> should not
>>>>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>>>> be
>>>>> digested.
>>>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>>>> +
>>>>>> +  @retval TRUE   EVP data digest succeeded.
>>>>>> +  @retval FALSE  EVP data digest failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdUpdate (
>>>>>> +  IN OUT  VOID        *EvpMdContext,
>>>>>> +  IN      CONST VOID  *Data,
>>>>>> +  IN      UINTN       DataSize
>>>>>> +  );
>>>>>> +
>>>>>> +/**
>>>>>> +  Completes computation of the EVP digest value.
>>>>>> +  Releases the specified EVP_MD_CTX context.
>>>>>> +
>>>>>> +  This function completes EVP hash computation and retrieves the digest
>>>>> value into
>>>>>> +  the specified memory. After this function has been called, the EVP
>> context
>>>>> cannot
>>>>>> +  be used again.
>>>>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>>>>> should
>>>>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>>>>> undefined.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>>>> digest
>>>>> value.
>>>>>
>>>>> (2) Please extend the comment on Digest with the following:
>>>>>
>>>>>   The caller is responsible for providing enough storage for the digest
>>>>>   algorithm selected with EvpMdInit(). Providing EVP_MAX_MD_SIZE bytes
>>>>>   will suffice for storing the digest regardless of the algorithm chosen
>>>>>   in EvpMdInit().
>>>>>
>>>>> (EVP_MAX_MD_SIZE is a public OpenSSL macro and I think we should openly
>>>>> advertise it to consumers in edk2.)
>>>>>
>>>>>> +
>>>>>> +  @retval TRUE   EVP digest computation succeeded.
>>>>>> +  @retval FALSE  EVP digest computation failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdFinal (
>>>>>> +  IN OUT  VOID   *EvpMdContext,
>>>>>> +  OUT     UINT8  *DigestValue
>>>>>
>>>>> (3) DigestValue should be marked OPTIONAL in my opinion, as NULL is
>>>>> deliberately permitted (for just freeing the context).
>>>>>
>>>>>> +  );
>>>>>> +
>>>>>> +/**
>>>>>> +  Computes the message digest of an input data buffer.
>>>>>> +
>>>>>> +  This function performs the message digest of a given data buffer, and
>>>> places
>>>>>> +  the digest value into the specified memory.
>>>>>> +
>>>>>> +  If DigestName is NULL, return FALSE.
>>>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>>>> +  If HashValue is NULL, return FALSE.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>>>> hashed.
>>>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval TRUE   Digest computation succeeded.
>>>>>> +  @retval FALSE  Digest computation failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdHashAll (
>>>>>> +  IN  CONST CHAR8   *DigestName,
>>>>>> +  IN  CONST VOID    *Data,
>>>>>> +  IN  UINTN         DataSize,
>>>>>> +  OUT UINT8         *HashValue
>>>>>> +  );
>>>>>> +
>>>>>>
>>>>>
>>>>
>> //===============================================================
>>>>> ======================
>>>>>>  //    Symmetric Cryptography Primitive
>>>>>>
>>>>>
>>>>
>> //===============================================================
>>>>> ======================
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>>>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>>>>> new file mode 100644
>>>>>> index 0000000000..b2770a9186
>>>>>> --- /dev/null
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMd.c
>>>>>> @@ -0,0 +1,257 @@
>>>>>> +/** @file
>>>>>> +  EVP MD Wrapper Implementation for OpenSSL.
>>>>>> +
>>>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>> +
>>>>>> +**/
>>>>>> +
>>>>>> +#include "InternalCryptLib.h"
>>>>>> +#include <openssl/evp.h>
>>>>>> +
>>>>>> +/**
>>>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
>> EVP_MD
>>>>> use.
>>>>>> +
>>>>>> +  If DigestName is NULL, then return FALSE.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>>>> terminated ASCII string.
>>>>>> +                              Valid digest names are:
>>>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>>>> +                              SM3
>>>>>> +
>>>>>> +  @return  Pointer to the EVP_MD_CTX context that has been allocated
>> and
>>>>> initialized.
>>>>>> +           If DigestName is invalid, returns NULL.
>>>>>> +           If the allocations fails, returns NULL.
>>>>>> +           If initialization fails, returns NULL.
>>>>>> +
>>>>>> +**/
>>>>>> +VOID *
>>>>>> +EFIAPI
>>>>>> +EvpMdInit (
>>>>>> +  IN  CONST CHAR8   *DigestName
>>>>>> +  )
>>>>>> +{
>>>>>> +  EVP_MD    *Digest;
>>>>>> +  VOID      *EvpMdContext;
>>>>>> +
>>>>>> +  //
>>>>>> +  // Check input parameters.
>>>>>> +  //
>>>>>> +  if (DigestName == NULL) {
>>>>>> +    return NULL;
>>>>>> +  }
>>>>>> +
>>>>>> +  //
>>>>>> +  // Allocate EVP_MD_CTX Context
>>>>>> +  //
>>>>>> +  EvpMdContext = EVP_MD_CTX_new ();
>>>>>> +  if (EvpMdContext == NULL) {
>>>>>> +    return NULL;
>>>>>> +  }
>>>>>> +
>>>>>> +  Digest = EVP_get_digestbyname (DigestName);
>>>>>
>>>>> I think this may not compile with gcc (and correctly so). The pointer
>>>>> returned by EVP_get_digestbyname() is const-qualified, but with the
>>>>> assignment, we're throwing away the const-ness.
>>>>>
>>>>> (4) Please const-qualify the "Digest" local pointer.
>>>>>
>>>>>> +  if (Digest == NULL) {
>>>>>> +    return NULL;
>>>>>> +  }
>>>>>
>>>>> (5) This is a memory leak I believe; "EvpMdContext" is leaked.
>>>>>
>>>>> For keeping the control flow simple, consider moving
>>>>> EVP_get_digestbyname() above EVP_MD_CTX_new().
>>>>>
>>>>>> +
>>>>>> +  //
>>>>>> +  // Initialize Context
>>>>>> +  //
>>>>>> +  if (EVP_DigestInit_ex (EvpMdContext, Digest, NULL) != 1) {
>>>>>> +    EVP_MD_CTX_free (EvpMdContext);
>>>>>> +    return NULL;
>>>>>> +  }
>>>>>> +
>>>>>> +  return EvpMdContext;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Makes a copy of an existing EVP_MD context.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If NewEvpMdContext is NULL, then return FALSE.
>>>>>> +
>>>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>>>> +
>>>>>> +  @retval TRUE   EVP_MD context copy succeeded.
>>>>>> +  @retval FALSE  EVP_MD context copy failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdDuplicate (
>>>>>> +  IN  CONST VOID    *EvpMdContext,
>>>>>> +  OUT VOID          *NewEvpMdContext
>>>>>> +  )
>>>>>> +{
>>>>>> +  //
>>>>>> +  // Check input parameters.
>>>>>> +  //
>>>>>> +  if (EvpMdContext == NULL || NewEvpMdContext == NULL) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  if (EVP_MD_CTX_copy (NewEvpMdContext, EvpMdContext) != 1) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  return TRUE;
>>>>>> +}
>>>>>
>>>>> (6) Can you please confirm that the caller is supposed to initialize
>>>>> "NewEvpMdContext" with EvpMdInit() first, before calling EvpMdDuplicate()?
>>>>>
>>>>>> +
>>>>>> +/**
>>>>>> +  Digests the input data and updates EVP_MD context.
>>>>>> +
>>>>>> +  This function performs EVP digest on a data buffer of the specified size.
>>>>>> +  It can be called multiple times to compute the digest of long or
>>>>> discontinuous data streams.
>>>>>> +  EVP_MD context should be already correctly initialized by EvpMdInit(),
>> and
>>>>> should not
>>>>>> +  be finalized by EvpMdFinal(). Behavior with invalid context is undefined.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>>>> be
>>>>> digested.
>>>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>>>> +
>>>>>> +  @retval TRUE   EVP data digest succeeded.
>>>>>> +  @retval FALSE  EVP data digest failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdUpdate (
>>>>>> +  IN OUT  VOID        *EvpMdContext,
>>>>>> +  IN      CONST VOID  *Data,
>>>>>> +  IN      UINTN       DataSize
>>>>>> +  )
>>>>>> +{
>>>>>> +  //
>>>>>> +  // Check input parameters.
>>>>>> +  //
>>>>>> +  if (EvpMdContext == NULL) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  //
>>>>>> +  // Check invalid parameters, in case only DataLength was checked in
>>>>> OpenSSL
>>>>>> +  //
>>>>>> +  if (Data == NULL && DataSize != 0) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  //
>>>>>> +  // OpenSSL EVP digest update
>>>>>> +  //
>>>>>> +  if (EVP_DigestUpdate (EvpMdContext, Data, DataSize) != 1) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  return TRUE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Completes computation of the EVP digest value.
>>>>>> +  Releases the specified EVP_MD_CTX context.
>>>>>> +
>>>>>> +  This function completes EVP hash computation and retrieves the digest
>>>>> value into
>>>>>> +  the specified memory. After this function has been called, the EVP
>> context
>>>>> cannot
>>>>>> +  be used again.
>>>>>> +  EVP context should be already correctly initialized by EvpMdInit(), and
>>>>> should
>>>>>> +  not be finalized by EvpMdFinal(). Behavior with invalid EVP context is
>>>>> undefined.
>>>>>> +
>>>>>> +  If EvpMdContext is NULL, then return FALSE.
>>>>>> +  If DigestValue is NULL, free the Context then return FALSE.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>>>> digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval TRUE   EVP digest computation succeeded.
>>>>>> +  @retval FALSE  EVP digest computation failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdFinal (
>>>>>> +  IN OUT  VOID   *EvpMdContext,
>>>>>> +  OUT     UINT8  *DigestValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  UINT32    Length;
>>>>>> +  BOOLEAN   ReturnValue;
>>>>>> +
>>>>>> +  ReturnValue = TRUE;
>>>>>> +
>>>>>> +  //
>>>>>> +  // Check input parameters.
>>>>>> +  //
>>>>>> +  if (EvpMdContext == NULL) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +  if (DigestValue == NULL) {
>>>>>> +    EVP_MD_CTX_free (EvpMdContext);
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  //
>>>>>> +  // OpenSSL EVP digest finalization
>>>>>> +  //
>>>>>> +  if (EVP_DigestFinal_ex (EvpMdContext, DigestValue, &Length) != 1) {
>>>>>> +    ReturnValue = FALSE;
>>>>>> +  }
>>>>>
>>>>>
>>>>> (7) I suggest dropping the "Length" local variable. EVP_DigestFinal_ex()
>>>>> deals fine with the third parameter being NULL, according to the docs
>>>>> (and the code).
>>>>>
>>>>>
>>>>>> +
>>>>>> +  //
>>>>>> +  // Free OpenSSL EVP_MD_CTX Context
>>>>>> +  //
>>>>>> +  EVP_MD_CTX_free (EvpMdContext);
>>>>>> +
>>>>>> +  return ReturnValue;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Computes the message digest of an input data buffer.
>>>>>> +
>>>>>> +  This function performs the message digest of a given data buffer, and
>>>> places
>>>>>> +  the digest value into the specified memory.
>>>>>> +
>>>>>> +  If DigestName is NULL, return FALSE.
>>>>>> +  If Data is NULL and DataSize is not zero, return FALSE.
>>>>>> +  If HashValue is NULL, return FALSE.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>>>> hashed.
>>>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval TRUE   Digest computation succeeded.
>>>>>> +  @retval FALSE  Digest computation failed.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdHashAll (
>>>>>> +  IN  CONST CHAR8   *DigestName,
>>>>>> +  IN  CONST VOID    *Data,
>>>>>> +  IN  UINTN         DataSize,
>>>>>> +  OUT UINT8         *HashValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  BOOLEAN   Result;
>>>>>> +  VOID      *EvpMdContext;
>>>>>> +
>>>>>> +  EvpMdContext = EvpMdInit (DigestName);
>>>>>> +  if (EvpMdContext == NULL) {
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  Result = EvpMdUpdate (EvpMdContext, Data, DataSize);
>>>>>> +  if (Result == FALSE) {
>>>>>
>>>>> (8) Style: please write (!Result).
>>>>>
>>>>>
>>>>>> +    EvpMdFinal (EvpMdContext, NULL);
>>>>>> +    return FALSE;
>>>>>> +  }
>>>>>> +
>>>>>> +  Result = EvpMdFinal (EvpMdContext, HashValue);
>>>>>> +
>>>>>> +  return Result;
>>>>>> +}
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>>>> b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>>>>> new file mode 100644
>>>>>> index 0000000000..038f63801f
>>>>>> --- /dev/null
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLib/Evp/CryptEvpMdNull.c
>>>>>> @@ -0,0 +1,128 @@
>>>>>> +/** @file
>>>>>> +  EVP MD Wrapper Null Library.
>>>>>> +
>>>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>> +
>>>>>> +**/
>>>>>> +
>>>>>> +#include "InternalCryptLib.h"
>>>>>> +
>>>>>> +/**
>>>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
>> EVP_MD
>>>>> use.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>>>> terminated ASCII string.
>>>>>> +                              Valid digest names are:
>>>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>>>> +                              SM3
>>>>>> +
>>>>>> +  @return NULL  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +VOID *
>>>>>> +EFIAPI
>>>>>> +EvpMdInit (
>>>>>> +  IN  CONST CHAR8   *DigestName
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return NULL;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Makes a copy of an existing EVP_MD context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdDuplicate (
>>>>>> +  IN  CONST VOID    *EvpMdContext,
>>>>>> +  OUT VOID          *NewEvpMdContext
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Digests the input data and updates EVP_MD context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>>>> be
>>>>> digested.
>>>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdUpdate (
>>>>>> +  IN OUT  VOID        *EvpMdContext,
>>>>>> +  IN      CONST VOID  *Data,
>>>>>> +  IN      UINTN       DataSize
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Completes computation of the EVP digest value.
>>>>>> +  Releases the specified EVP_MD_CTX context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>>>> digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdFinal (
>>>>>> +  IN OUT  VOID   *EvpMdContext,
>>>>>> +  OUT     UINT8  *DigestValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Computes the message digest of an input data buffer.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>>>> hashed.
>>>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdHashAll (
>>>>>> +  IN  CONST CHAR8   *DigestName,
>>>>>> +  IN  CONST VOID    *Data,
>>>>>> +  IN  UINTN         DataSize,
>>>>>> +  OUT UINT8         *HashValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> diff --git a/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>>>> b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>>>>> new file mode 100644
>>>>>> index 0000000000..038f63801f
>>>>>> --- /dev/null
>>>>>> +++ b/CryptoPkg/Library/BaseCryptLibNull/Evp/CryptEvpMdNull.c
>>>>>> @@ -0,0 +1,128 @@
>>>>>> +/** @file
>>>>>> +  EVP MD Wrapper Null Library.
>>>>>> +
>>>>>> +Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
>>>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>> +
>>>>>> +**/
>>>>>> +
>>>>>> +#include "InternalCryptLib.h"
>>>>>> +
>>>>>> +/**
>>>>>> +  Allocates and initializes one EVP_MD_CTX context for subsequent
>> EVP_MD
>>>>> use.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name as a NULL-
>>>>> terminated ASCII string.
>>>>>> +                              Valid digest names are:
>>>>>> +                              MD5, SHA1, SHA224, SHA256, SHA384, SHA512
>>>>>> +                              SHA3-224, SHA3-256, SHA3-384, SHA3-512
>>>>>> +                              SM3
>>>>>> +
>>>>>> +  @return NULL  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +VOID *
>>>>>> +EFIAPI
>>>>>> +EvpMdInit (
>>>>>> +  IN  CONST CHAR8   *DigestName
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return NULL;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Makes a copy of an existing EVP_MD context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]  EvpMdContext     Pointer to EVP_MD context being copied.
>>>>>> +  @param[out] NewEvpMdContext  Pointer to new EVP_MD context.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdDuplicate (
>>>>>> +  IN  CONST VOID    *EvpMdContext,
>>>>>> +  OUT VOID          *NewEvpMdContext
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Digests the input data and updates EVP_MD context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext       Pointer to the EVP_MD context.
>>>>>> +  @param[in]       Data               Pointer to the buffer containing the data to
>>>> be
>>>>> digested.
>>>>>> +  @param[in]       DataSize           Size of Data buffer in bytes.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdUpdate (
>>>>>> +  IN OUT  VOID        *EvpMdContext,
>>>>>> +  IN      CONST VOID  *Data,
>>>>>> +  IN      UINTN       DataSize
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Completes computation of the EVP digest value.
>>>>>> +  Releases the specified EVP_MD_CTX context.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in, out]  EvpMdContext   Pointer to the EVP context.
>>>>>> +  @param[out]      Digest         Pointer to a buffer that receives the EVP
>>>> digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdFinal (
>>>>>> +  IN OUT  VOID   *EvpMdContext,
>>>>>> +  OUT     UINT8  *DigestValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>> +
>>>>>> +/**
>>>>>> +  Computes the message digest of an input data buffer.
>>>>>> +
>>>>>> +  Return FALSE to indicate this interface is not supported.
>>>>>> +
>>>>>> +  @param[in]    DigestName    Pointer to the digest name.
>>>>>> +  @param[in]    Data          Pointer to the buffer containing the data to be
>>>>> hashed.
>>>>>> +  @param[in]    DataSize      Size of Data buffer in bytes.
>>>>>> +  @param[out]   HashValue     Pointer to a buffer that receives the digest
>>>>> value.
>>>>>> +
>>>>>> +  @retval FALSE  This interface is not supported.
>>>>>> +
>>>>>> +**/
>>>>>> +BOOLEAN
>>>>>> +EFIAPI
>>>>>> +EvpMdHashAll (
>>>>>> +  IN  CONST CHAR8   *DigestName,
>>>>>> +  IN  CONST VOID    *Data,
>>>>>> +  IN  UINTN         DataSize,
>>>>>> +  OUT UINT8         *HashValue
>>>>>> +  )
>>>>>> +{
>>>>>> +  ASSERT (FALSE);
>>>>>> +  return FALSE;
>>>>>> +}
>>>>>>
>>>>>
>>>>> Thanks,
>>>>> Laszlo
>>>>
>>>>
>>>>
>>>
>>
>>
>> 
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 14:17         ` Yao, Jiewen
@ 2020-09-16 16:04           ` Laszlo Ersek
  2020-09-17  1:21             ` Zurcher, Christopher J
  0 siblings, 1 reply; 13+ messages in thread
From: Laszlo Ersek @ 2020-09-16 16:04 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Zurcher, Christopher J
  Cc: Wang, Jian J, Lu, XiaoyuX

On 09/16/20 16:17, Yao, Jiewen wrote:
> Thank you Laszlo.
> You forced me to read the code again and more carefully. :-)
> 
> I believe I understand why you use NULL to free the context now - to support error path.
> 
> First, I did have some new thought for EVP API. Let’s consider 3 cases, 1) Hash all data one time, 2) Hash update data multiple times, 3) Error during update.
> 
> A. In current design, the API sequence is:
> 1) EvpMdHashAll (Digest)
> 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> 3) EvpMdInit, EvpMdUpdate->ERROR, EvpMdFinal (NULL)
> 
> B. We can auto free context in EvpMdUpdate in error path  - the API sequence is:
> 1) EvpMdHashAll (Digest)
> 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> 3) EvpMdInit, EvpMdUpdate->ERROR

I don't like "B" because in the loop body where you call EvpMdUpdate(),
you may encounter an error from a different source than EvpMdUpdate()
itself. For example, you may be reading data from the network or a disk
file. If fetching the next chunk fails, we'd still want to clean up the
EVP MD context. Therefore, if EvpMdUpdate() would itself invalidate the
context, if it failed, then that would *complicate* the error path. (=
Clean up the context after the loop body *only* if something different
from EvpMdUpdate() failed.)

> 
> C: We can use New/Free style - similar to HMAC
> 1) EvpMdHashAll (Digest)
> 2) EvpMdNew, EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest), EvpMdFree
> 3) EvpMdNew, EvpMdInit, EvpMdUpdate->ERROR, EvpMdFree

Yes, this was the first pattern, the one that caused me to say "please
don't do this". In this pattern, the context may exist between "New" and
"Init", and also between "Final" and "Free". Those two states are
useless and only good for causing confusion.

For example, are you allowed to Duplicate a context in those states?
It's best to prevent even the *asking* of that question.

> 
> I checked below APIs:
> 1) openssl (EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_free)
> 2) mbedtls (mbedtls_sha256_init, mbedtls_sha256_starts_ret, mbedtls_sha256_update_ret, mbedtls_sha256_finish_ret, mbedtls_sha256_free)
> 3) BaseCryptoLib HMAC (HmacSha256New, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Free)
> 
> All APIs include free function to free the context, I don’t think it is a bad idea to have EvpMdFree() API.
> I would like to propose option - C.

- I cannot comment on mbedtls (2).

- I think the current BaseCryptLib HMAC APIs (3) are not great, and we
should use this opportunity to improve upon them.

- Regarding openssl (1), as I understand it, the situation is different
from edk2. In OpenSSL, EVP_MD_CTX is *not* an opaque type (expressed in
C language terms, it is not an "incomplete" type, but a "complete"
type). Therefore, an expression such as sizeof(EVP_MD_CTX) works.

The consequence is that OpenSSL code itself can use the following style:

void func(void)
{
  EVP_MD_CTX ctx;

  EVP_DigestInit_ex(&ctx, ...);
  ...
  EVP_DigestFinal_ex(&ctx, ...)
}

In other words, OpenSSL-native code is permitted to know the internals
of EVP_MD_CTX, therefore the OpenSSL-internal code may allocate
EVP_MD_CTX with means that are *different* from EVP_MD_CTX_new() and
EVP_MD_CTX_free().

The caller of (Init, Final) may use (New, Free) for memory management,
or may use something completely different (for example, local variables).

Therefore, offering the (Init, Final) APIs separately from (New, Free)
is meaningful.

But the situation in edk2 -- or any other OpenSSL *consumer* -- is
different. In edk2, EVP_MD_CTX is an opaque type -- in C language terms:
an "incomplete" type. OpenSSL deliberately hides the internals of
EVP_MD_CTX.

See "CryptoPkg/Library/OpensslLib/openssl/include/openssl/ossl_typ.h":

> typedef struct evp_md_ctx_st EVP_MD_CTX;

and the "evp_md_ctx_st" structure type is only defined in
"CryptoPkg/Library/OpensslLib/openssl/crypto/evp/evp_local.h", whose
contents -- I think! -- OpenSSL client code cannot, or *should not*,
refer to.

This means that OpenSSL consumer code can *only* rely on
EVP_MD_CTX_new() and EVP_MD_CTX_free(), for allocating and releasing the
context, respectively. "sizeof(EVP_MD_CTX)" is not permitted, and
defining a local or global variable of type EVP_MD_CTX is also not valid.

This means that the only reason for separating (Init, Final) from (New,
Free) falls away, in OpenSSL consumer code. In OpenSSL consumer code,
there is no reason to keep *both* pairs of APIs.


Please note that, this (very prudent) information hiding / encapsulation
in OpenSSL used to be violated by edk2 in the past, intentionally.
That's what the HmacXxxGetContextSize() APIs were about -- those APIs
forcefully leaked the context sizes to client code, so that client code
in edk2 could perform its own allocation.

But in <https://bugzilla.tianocore.org/show_bug.cgi?id=1792>, we finally
eliminated HmacXxxGetContextSize(). As a part of that, we also
simplified the HMAC API -- note that Init was replaced by SetKey. And
because we were reworking an existent API, I didn't propose very
intrusive changes -- I didn't propose fusing Final and Free, for example.

Now, the EVP MD APIs are just being introduced to edk2. So we have a
chance at getting them right, and making them minimal.

> Second, I believe EVP is just something in openssl. It does not mean that we *have to* design API to follow openssl.
> After rethink the size impact, I do have concern to merge all Hash implementation together in one function.
> It might means nothing if the crypto library is based upon openssl.
> But if the cryptolib implementation is based upon other crypto, such as Intel IPP (https://github.com/slimbootloader/slimbootloader/tree/master/BootloaderCommonPkg/Library/IppCryptoLib) or mbedTls (https://github.com/jyao1/edk2/tree/DeviceSecurity/CryptoMbedTlsPkg), then we can NOT get size benefit by separating the hash algorithm.
> 
> 
> I would like to propose separate EvpMdxxx.
> EvpMdNew -> Sha256New, Sha384New
> EvpMdInit -> Sha256Init, Sha384Init
> EvpMdUpdate -> Sha256Update, Sha384Update
> EvpMdFinal -> Sha256Final, Sha384Final
> EvpMdFree -> Sha256Free, Sha384Free

I have no comment on this.

> We can do similar thing with Hmac, to deprecate Sha256GetContextSize() API,

Yes, good idea.

> and replace caller with Sha256New() / Sha384Free()

Indeed.

But then -- there's no reason left for keeping (New, Free) separate from
(Init, Final).

Thanks
Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-16 16:04           ` Laszlo Ersek
@ 2020-09-17  1:21             ` Zurcher, Christopher J
  2020-09-17  1:33               ` Yao, Jiewen
  0 siblings, 1 reply; 13+ messages in thread
From: Zurcher, Christopher J @ 2020-09-17  1:21 UTC (permalink / raw)
  To: Laszlo Ersek, Yao, Jiewen, devel@edk2.groups.io; +Cc: Wang, Jian J, Lu, XiaoyuX

Jiewen,

This patch set assumes that EDK2 is linked with OpenSSL for the foreseeable future. The end goal would be to move all applicable Crypto access to the EVP interface and remove the individual modules we maintain for each algorithm. The primary benefit here is reducing complexity and code duplication. Without this end goal, this patch set will not be particularly useful.

If the design goal of BaseCryptLib is to allow OpenSSL to be replaced by other crypto providers, I should abandon this patch set, and we can save the EVP transition for when moving to OpenSSL 3 becomes mandatory. At that time, the CryptX.c family can be modified to call EVP functions.
(This could even be done transparently, by returning UINTN from GetContextSize and using the user-supplied "context" to store the OpenSSL-supplied context pointer.)
Delaying EVP adoption in this case would also allow more time for platforms to adopt the Crypto Services model, which should help offset the size impact.

Please let me know which path should be taken.

Thanks,
Christopher Zurcher

> -----Original Message-----
> From: Laszlo Ersek <lersek@redhat.com>
> Sent: Wednesday, September 16, 2020 09:05
> To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher, Christopher J
> <christopher.j.zurcher@intel.com>
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
> 
> On 09/16/20 16:17, Yao, Jiewen wrote:
> > Thank you Laszlo.
> > You forced me to read the code again and more carefully. :-)
> >
> > I believe I understand why you use NULL to free the context now - to support error path.
> >
> > First, I did have some new thought for EVP API. Let’s consider 3 cases, 1) Hash all data one time, 2) Hash update
> data multiple times, 3) Error during update.
> >
> > A. In current design, the API sequence is:
> > 1) EvpMdHashAll (Digest)
> > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > 3) EvpMdInit, EvpMdUpdate->ERROR, EvpMdFinal (NULL)
> >
> > B. We can auto free context in EvpMdUpdate in error path  - the API sequence is:
> > 1) EvpMdHashAll (Digest)
> > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > 3) EvpMdInit, EvpMdUpdate->ERROR
> 
> I don't like "B" because in the loop body where you call EvpMdUpdate(),
> you may encounter an error from a different source than EvpMdUpdate()
> itself. For example, you may be reading data from the network or a disk
> file. If fetching the next chunk fails, we'd still want to clean up the
> EVP MD context. Therefore, if EvpMdUpdate() would itself invalidate the
> context, if it failed, then that would *complicate* the error path. (=
> Clean up the context after the loop body *only* if something different
> from EvpMdUpdate() failed.)
> 
> >
> > C: We can use New/Free style - similar to HMAC
> > 1) EvpMdHashAll (Digest)
> > 2) EvpMdNew, EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest), EvpMdFree
> > 3) EvpMdNew, EvpMdInit, EvpMdUpdate->ERROR, EvpMdFree
> 
> Yes, this was the first pattern, the one that caused me to say "please
> don't do this". In this pattern, the context may exist between "New" and
> "Init", and also between "Final" and "Free". Those two states are
> useless and only good for causing confusion.
> 
> For example, are you allowed to Duplicate a context in those states?
> It's best to prevent even the *asking* of that question.
> 
> >
> > I checked below APIs:
> > 1) openssl (EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_free)
> > 2) mbedtls (mbedtls_sha256_init, mbedtls_sha256_starts_ret, mbedtls_sha256_update_ret, mbedtls_sha256_finish_ret,
> mbedtls_sha256_free)
> > 3) BaseCryptoLib HMAC (HmacSha256New, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Free)
> >
> > All APIs include free function to free the context, I don’t think it is a bad idea to have EvpMdFree() API.
> > I would like to propose option - C.
> 
> - I cannot comment on mbedtls (2).
> 
> - I think the current BaseCryptLib HMAC APIs (3) are not great, and we
> should use this opportunity to improve upon them.
> 
> - Regarding openssl (1), as I understand it, the situation is different
> from edk2. In OpenSSL, EVP_MD_CTX is *not* an opaque type (expressed in
> C language terms, it is not an "incomplete" type, but a "complete"
> type). Therefore, an expression such as sizeof(EVP_MD_CTX) works.
> 
> The consequence is that OpenSSL code itself can use the following style:
> 
> void func(void)
> {
>   EVP_MD_CTX ctx;
> 
>   EVP_DigestInit_ex(&ctx, ...);
>   ...
>   EVP_DigestFinal_ex(&ctx, ...)
> }
> 
> In other words, OpenSSL-native code is permitted to know the internals
> of EVP_MD_CTX, therefore the OpenSSL-internal code may allocate
> EVP_MD_CTX with means that are *different* from EVP_MD_CTX_new() and
> EVP_MD_CTX_free().
> 
> The caller of (Init, Final) may use (New, Free) for memory management,
> or may use something completely different (for example, local variables).
> 
> Therefore, offering the (Init, Final) APIs separately from (New, Free)
> is meaningful.
> 
> But the situation in edk2 -- or any other OpenSSL *consumer* -- is
> different. In edk2, EVP_MD_CTX is an opaque type -- in C language terms:
> an "incomplete" type. OpenSSL deliberately hides the internals of
> EVP_MD_CTX.
> 
> See "CryptoPkg/Library/OpensslLib/openssl/include/openssl/ossl_typ.h":
> 
> > typedef struct evp_md_ctx_st EVP_MD_CTX;
> 
> and the "evp_md_ctx_st" structure type is only defined in
> "CryptoPkg/Library/OpensslLib/openssl/crypto/evp/evp_local.h", whose
> contents -- I think! -- OpenSSL client code cannot, or *should not*,
> refer to.
> 
> This means that OpenSSL consumer code can *only* rely on
> EVP_MD_CTX_new() and EVP_MD_CTX_free(), for allocating and releasing the
> context, respectively. "sizeof(EVP_MD_CTX)" is not permitted, and
> defining a local or global variable of type EVP_MD_CTX is also not valid.
> 
> This means that the only reason for separating (Init, Final) from (New,
> Free) falls away, in OpenSSL consumer code. In OpenSSL consumer code,
> there is no reason to keep *both* pairs of APIs.
> 
> 
> Please note that, this (very prudent) information hiding / encapsulation
> in OpenSSL used to be violated by edk2 in the past, intentionally.
> That's what the HmacXxxGetContextSize() APIs were about -- those APIs
> forcefully leaked the context sizes to client code, so that client code
> in edk2 could perform its own allocation.
> 
> But in <https://bugzilla.tianocore.org/show_bug.cgi?id=1792>, we finally
> eliminated HmacXxxGetContextSize(). As a part of that, we also
> simplified the HMAC API -- note that Init was replaced by SetKey. And
> because we were reworking an existent API, I didn't propose very
> intrusive changes -- I didn't propose fusing Final and Free, for example.
> 
> Now, the EVP MD APIs are just being introduced to edk2. So we have a
> chance at getting them right, and making them minimal.
> 
> > Second, I believe EVP is just something in openssl. It does not mean that we *have to* design API to follow
> openssl.
> > After rethink the size impact, I do have concern to merge all Hash implementation together in one function.
> > It might means nothing if the crypto library is based upon openssl.
> > But if the cryptolib implementation is based upon other crypto, such as Intel IPP
> (https://github.com/slimbootloader/slimbootloader/tree/master/BootloaderCommonPkg/Library/IppCryptoLib) or mbedTls
> (https://github.com/jyao1/edk2/tree/DeviceSecurity/CryptoMbedTlsPkg), then we can NOT get size benefit by separating
> the hash algorithm.
> >
> >
> > I would like to propose separate EvpMdxxx.
> > EvpMdNew -> Sha256New, Sha384New
> > EvpMdInit -> Sha256Init, Sha384Init
> > EvpMdUpdate -> Sha256Update, Sha384Update
> > EvpMdFinal -> Sha256Final, Sha384Final
> > EvpMdFree -> Sha256Free, Sha384Free
> 
> I have no comment on this.
> 
> > We can do similar thing with Hmac, to deprecate Sha256GetContextSize() API,
> 
> Yes, good idea.
> 
> > and replace caller with Sha256New() / Sha384Free()
> 
> Indeed.
> 
> But then -- there's no reason left for keeping (New, Free) separate from
> (Init, Final).
> 
> Thanks
> Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-17  1:21             ` Zurcher, Christopher J
@ 2020-09-17  1:33               ` Yao, Jiewen
  2020-09-17  1:44                 ` Zurcher, Christopher J
  0 siblings, 1 reply; 13+ messages in thread
From: Yao, Jiewen @ 2020-09-17  1:33 UTC (permalink / raw)
  To: Zurcher, Christopher J, Laszlo Ersek, devel@edk2.groups.io
  Cc: Wang, Jian J, Lu, XiaoyuX

Thanks to ask this question Christopher.

When we create the CryptoPkg, our goal is to pick openssl as implementation choice. We don’t want to mandate any EDKII consumer use openssl.

The EDKII consumer shall have freedom to choose whatever crypto library they want to use. Even Intel IPP and mebdTLS are options. A vendor may choose a GPL wolfssl, or vendor proprietary close source implementation. We choose openssl just because it is the BSD license and it is used widely when we make decision.

That is why we spend effort to design BaseCryptoLib API to abstract the crypto action, instead of calling openssl function directly in our code.
Whenever we add new API, we need evaluate if it is applicable for any other crypto implantation and have negative impact.

That assumption that EDKII must link openssl is NOT our original intent.
Feel free to drop the patch or reject the Bugzilla, if you want.

Thank you
Yao Jiewen

> -----Original Message-----
> From: Zurcher, Christopher J <christopher.j.zurcher@intel.com>
> Sent: Thursday, September 17, 2020 9:21 AM
> To: Laszlo Ersek <lersek@redhat.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: RE: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> 
> Jiewen,
> 
> This patch set assumes that EDK2 is linked with OpenSSL for the foreseeable
> future. The end goal would be to move all applicable Crypto access to the EVP
> interface and remove the individual modules we maintain for each algorithm.
> The primary benefit here is reducing complexity and code duplication. Without
> this end goal, this patch set will not be particularly useful.
> 
> If the design goal of BaseCryptLib is to allow OpenSSL to be replaced by other
> crypto providers, I should abandon this patch set, and we can save the EVP
> transition for when moving to OpenSSL 3 becomes mandatory. At that time, the
> CryptX.c family can be modified to call EVP functions.
> (This could even be done transparently, by returning UINTN from GetContextSize
> and using the user-supplied "context" to store the OpenSSL-supplied context
> pointer.)
> Delaying EVP adoption in this case would also allow more time for platforms to
> adopt the Crypto Services model, which should help offset the size impact.
> 
> Please let me know which path should be taken.
> 
> Thanks,
> Christopher Zurcher
> 
> > -----Original Message-----
> > From: Laszlo Ersek <lersek@redhat.com>
> > Sent: Wednesday, September 16, 2020 09:05
> > To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher,
> Christopher J
> > <christopher.j.zurcher@intel.com>
> > Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> > Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> (Envelope) Digest interface
> >
> > On 09/16/20 16:17, Yao, Jiewen wrote:
> > > Thank you Laszlo.
> > > You forced me to read the code again and more carefully. :-)
> > >
> > > I believe I understand why you use NULL to free the context now - to support
> error path.
> > >
> > > First, I did have some new thought for EVP API. Let’s consider 3 cases, 1)
> Hash all data one time, 2) Hash update
> > data multiple times, 3) Error during update.
> > >
> > > A. In current design, the API sequence is:
> > > 1) EvpMdHashAll (Digest)
> > > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > > 3) EvpMdInit, EvpMdUpdate->ERROR, EvpMdFinal (NULL)
> > >
> > > B. We can auto free context in EvpMdUpdate in error path  - the API
> sequence is:
> > > 1) EvpMdHashAll (Digest)
> > > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > > 3) EvpMdInit, EvpMdUpdate->ERROR
> >
> > I don't like "B" because in the loop body where you call EvpMdUpdate(),
> > you may encounter an error from a different source than EvpMdUpdate()
> > itself. For example, you may be reading data from the network or a disk
> > file. If fetching the next chunk fails, we'd still want to clean up the
> > EVP MD context. Therefore, if EvpMdUpdate() would itself invalidate the
> > context, if it failed, then that would *complicate* the error path. (=
> > Clean up the context after the loop body *only* if something different
> > from EvpMdUpdate() failed.)
> >
> > >
> > > C: We can use New/Free style - similar to HMAC
> > > 1) EvpMdHashAll (Digest)
> > > 2) EvpMdNew, EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal
> (Digest), EvpMdFree
> > > 3) EvpMdNew, EvpMdInit, EvpMdUpdate->ERROR, EvpMdFree
> >
> > Yes, this was the first pattern, the one that caused me to say "please
> > don't do this". In this pattern, the context may exist between "New" and
> > "Init", and also between "Final" and "Free". Those two states are
> > useless and only good for causing confusion.
> >
> > For example, are you allowed to Duplicate a context in those states?
> > It's best to prevent even the *asking* of that question.
> >
> > >
> > > I checked below APIs:
> > > 1) openssl (EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_DigestUpdate,
> EVP_DigestFinal_ex, EVP_MD_CTX_free)
> > > 2) mbedtls (mbedtls_sha256_init, mbedtls_sha256_starts_ret,
> mbedtls_sha256_update_ret, mbedtls_sha256_finish_ret,
> > mbedtls_sha256_free)
> > > 3) BaseCryptoLib HMAC (HmacSha256New, HmacSha256SetKey,
> HmacSha256Update, HmacSha256Final, HmacSha256Free)
> > >
> > > All APIs include free function to free the context, I don’t think it is a bad idea
> to have EvpMdFree() API.
> > > I would like to propose option - C.
> >
> > - I cannot comment on mbedtls (2).
> >
> > - I think the current BaseCryptLib HMAC APIs (3) are not great, and we
> > should use this opportunity to improve upon them.
> >
> > - Regarding openssl (1), as I understand it, the situation is different
> > from edk2. In OpenSSL, EVP_MD_CTX is *not* an opaque type (expressed in
> > C language terms, it is not an "incomplete" type, but a "complete"
> > type). Therefore, an expression such as sizeof(EVP_MD_CTX) works.
> >
> > The consequence is that OpenSSL code itself can use the following style:
> >
> > void func(void)
> > {
> >   EVP_MD_CTX ctx;
> >
> >   EVP_DigestInit_ex(&ctx, ...);
> >   ...
> >   EVP_DigestFinal_ex(&ctx, ...)
> > }
> >
> > In other words, OpenSSL-native code is permitted to know the internals
> > of EVP_MD_CTX, therefore the OpenSSL-internal code may allocate
> > EVP_MD_CTX with means that are *different* from EVP_MD_CTX_new() and
> > EVP_MD_CTX_free().
> >
> > The caller of (Init, Final) may use (New, Free) for memory management,
> > or may use something completely different (for example, local variables).
> >
> > Therefore, offering the (Init, Final) APIs separately from (New, Free)
> > is meaningful.
> >
> > But the situation in edk2 -- or any other OpenSSL *consumer* -- is
> > different. In edk2, EVP_MD_CTX is an opaque type -- in C language terms:
> > an "incomplete" type. OpenSSL deliberately hides the internals of
> > EVP_MD_CTX.
> >
> > See "CryptoPkg/Library/OpensslLib/openssl/include/openssl/ossl_typ.h":
> >
> > > typedef struct evp_md_ctx_st EVP_MD_CTX;
> >
> > and the "evp_md_ctx_st" structure type is only defined in
> > "CryptoPkg/Library/OpensslLib/openssl/crypto/evp/evp_local.h", whose
> > contents -- I think! -- OpenSSL client code cannot, or *should not*,
> > refer to.
> >
> > This means that OpenSSL consumer code can *only* rely on
> > EVP_MD_CTX_new() and EVP_MD_CTX_free(), for allocating and releasing the
> > context, respectively. "sizeof(EVP_MD_CTX)" is not permitted, and
> > defining a local or global variable of type EVP_MD_CTX is also not valid.
> >
> > This means that the only reason for separating (Init, Final) from (New,
> > Free) falls away, in OpenSSL consumer code. In OpenSSL consumer code,
> > there is no reason to keep *both* pairs of APIs.
> >
> >
> > Please note that, this (very prudent) information hiding / encapsulation
> > in OpenSSL used to be violated by edk2 in the past, intentionally.
> > That's what the HmacXxxGetContextSize() APIs were about -- those APIs
> > forcefully leaked the context sizes to client code, so that client code
> > in edk2 could perform its own allocation.
> >
> > But in <https://bugzilla.tianocore.org/show_bug.cgi?id=1792>, we finally
> > eliminated HmacXxxGetContextSize(). As a part of that, we also
> > simplified the HMAC API -- note that Init was replaced by SetKey. And
> > because we were reworking an existent API, I didn't propose very
> > intrusive changes -- I didn't propose fusing Final and Free, for example.
> >
> > Now, the EVP MD APIs are just being introduced to edk2. So we have a
> > chance at getting them right, and making them minimal.
> >
> > > Second, I believe EVP is just something in openssl. It does not mean that we
> *have to* design API to follow
> > openssl.
> > > After rethink the size impact, I do have concern to merge all Hash
> implementation together in one function.
> > > It might means nothing if the crypto library is based upon openssl.
> > > But if the cryptolib implementation is based upon other crypto, such as Intel
> IPP
> >
> (https://github.com/slimbootloader/slimbootloader/tree/master/BootloaderCo
> mmonPkg/Library/IppCryptoLib) or mbedTls
> > (https://github.com/jyao1/edk2/tree/DeviceSecurity/CryptoMbedTlsPkg), then
> we can NOT get size benefit by separating
> > the hash algorithm.
> > >
> > >
> > > I would like to propose separate EvpMdxxx.
> > > EvpMdNew -> Sha256New, Sha384New
> > > EvpMdInit -> Sha256Init, Sha384Init
> > > EvpMdUpdate -> Sha256Update, Sha384Update
> > > EvpMdFinal -> Sha256Final, Sha384Final
> > > EvpMdFree -> Sha256Free, Sha384Free
> >
> > I have no comment on this.
> >
> > > We can do similar thing with Hmac, to deprecate Sha256GetContextSize()
> API,
> >
> > Yes, good idea.
> >
> > > and replace caller with Sha256New() / Sha384Free()
> >
> > Indeed.
> >
> > But then -- there's no reason left for keeping (New, Free) separate from
> > (Init, Final).
> >
> > Thanks
> > Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
  2020-09-17  1:33               ` Yao, Jiewen
@ 2020-09-17  1:44                 ` Zurcher, Christopher J
  0 siblings, 0 replies; 13+ messages in thread
From: Zurcher, Christopher J @ 2020-09-17  1:44 UTC (permalink / raw)
  To: Yao, Jiewen, Laszlo Ersek, devel@edk2.groups.io; +Cc: Wang, Jian J, Lu, XiaoyuX

OK, abandoning this patch set.

The BZ can stay open as we will still have to migrate the existing files to EVP eventually.

--
Christopher Zurcher

> -----Original Message-----
> From: Yao, Jiewen <jiewen.yao@intel.com>
> Sent: Wednesday, September 16, 2020 18:34
> To: Zurcher, Christopher J <christopher.j.zurcher@intel.com>; Laszlo Ersek <lersek@redhat.com>; devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> Subject: RE: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface
> 
> Thanks to ask this question Christopher.
> 
> When we create the CryptoPkg, our goal is to pick openssl as implementation choice. We don’t want to mandate any
> EDKII consumer use openssl.
> 
> The EDKII consumer shall have freedom to choose whatever crypto library they want to use. Even Intel IPP and mebdTLS
> are options. A vendor may choose a GPL wolfssl, or vendor proprietary close source implementation. We choose openssl
> just because it is the BSD license and it is used widely when we make decision.
> 
> That is why we spend effort to design BaseCryptoLib API to abstract the crypto action, instead of calling openssl
> function directly in our code.
> Whenever we add new API, we need evaluate if it is applicable for any other crypto implantation and have negative
> impact.
> 
> That assumption that EDKII must link openssl is NOT our original intent.
> Feel free to drop the patch or reject the Bugzilla, if you want.
> 
> Thank you
> Yao Jiewen
> 
> > -----Original Message-----
> > From: Zurcher, Christopher J <christopher.j.zurcher@intel.com>
> > Sent: Thursday, September 17, 2020 9:21 AM
> > To: Laszlo Ersek <lersek@redhat.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> > devel@edk2.groups.io
> > Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> > Subject: RE: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> > (Envelope) Digest interface
> >
> > Jiewen,
> >
> > This patch set assumes that EDK2 is linked with OpenSSL for the foreseeable
> > future. The end goal would be to move all applicable Crypto access to the EVP
> > interface and remove the individual modules we maintain for each algorithm.
> > The primary benefit here is reducing complexity and code duplication. Without
> > this end goal, this patch set will not be particularly useful.
> >
> > If the design goal of BaseCryptLib is to allow OpenSSL to be replaced by other
> > crypto providers, I should abandon this patch set, and we can save the EVP
> > transition for when moving to OpenSSL 3 becomes mandatory. At that time, the
> > CryptX.c family can be modified to call EVP functions.
> > (This could even be done transparently, by returning UINTN from GetContextSize
> > and using the user-supplied "context" to store the OpenSSL-supplied context
> > pointer.)
> > Delaying EVP adoption in this case would also allow more time for platforms to
> > adopt the Crypto Services model, which should help offset the size impact.
> >
> > Please let me know which path should be taken.
> >
> > Thanks,
> > Christopher Zurcher
> >
> > > -----Original Message-----
> > > From: Laszlo Ersek <lersek@redhat.com>
> > > Sent: Wednesday, September 16, 2020 09:05
> > > To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Zurcher,
> > Christopher J
> > > <christopher.j.zurcher@intel.com>
> > > Cc: Wang, Jian J <jian.j.wang@intel.com>; Lu, XiaoyuX <xiaoyux.lu@intel.com>
> > > Subject: Re: [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP
> > (Envelope) Digest interface
> > >
> > > On 09/16/20 16:17, Yao, Jiewen wrote:
> > > > Thank you Laszlo.
> > > > You forced me to read the code again and more carefully. :-)
> > > >
> > > > I believe I understand why you use NULL to free the context now - to support
> > error path.
> > > >
> > > > First, I did have some new thought for EVP API. Let’s consider 3 cases, 1)
> > Hash all data one time, 2) Hash update
> > > data multiple times, 3) Error during update.
> > > >
> > > > A. In current design, the API sequence is:
> > > > 1) EvpMdHashAll (Digest)
> > > > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > > > 3) EvpMdInit, EvpMdUpdate->ERROR, EvpMdFinal (NULL)
> > > >
> > > > B. We can auto free context in EvpMdUpdate in error path  - the API
> > sequence is:
> > > > 1) EvpMdHashAll (Digest)
> > > > 2) EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal (Digest)
> > > > 3) EvpMdInit, EvpMdUpdate->ERROR
> > >
> > > I don't like "B" because in the loop body where you call EvpMdUpdate(),
> > > you may encounter an error from a different source than EvpMdUpdate()
> > > itself. For example, you may be reading data from the network or a disk
> > > file. If fetching the next chunk fails, we'd still want to clean up the
> > > EVP MD context. Therefore, if EvpMdUpdate() would itself invalidate the
> > > context, if it failed, then that would *complicate* the error path. (=
> > > Clean up the context after the loop body *only* if something different
> > > from EvpMdUpdate() failed.)
> > >
> > > >
> > > > C: We can use New/Free style - similar to HMAC
> > > > 1) EvpMdHashAll (Digest)
> > > > 2) EvpMdNew, EvpMdInit, EvpMdUpdate, EvpMdUpdate, ... , EvpMdFinal
> > (Digest), EvpMdFree
> > > > 3) EvpMdNew, EvpMdInit, EvpMdUpdate->ERROR, EvpMdFree
> > >
> > > Yes, this was the first pattern, the one that caused me to say "please
> > > don't do this". In this pattern, the context may exist between "New" and
> > > "Init", and also between "Final" and "Free". Those two states are
> > > useless and only good for causing confusion.
> > >
> > > For example, are you allowed to Duplicate a context in those states?
> > > It's best to prevent even the *asking* of that question.
> > >
> > > >
> > > > I checked below APIs:
> > > > 1) openssl (EVP_MD_CTX_new, EVP_DigestInit_ex, EVP_DigestUpdate,
> > EVP_DigestFinal_ex, EVP_MD_CTX_free)
> > > > 2) mbedtls (mbedtls_sha256_init, mbedtls_sha256_starts_ret,
> > mbedtls_sha256_update_ret, mbedtls_sha256_finish_ret,
> > > mbedtls_sha256_free)
> > > > 3) BaseCryptoLib HMAC (HmacSha256New, HmacSha256SetKey,
> > HmacSha256Update, HmacSha256Final, HmacSha256Free)
> > > >
> > > > All APIs include free function to free the context, I don’t think it is a bad idea
> > to have EvpMdFree() API.
> > > > I would like to propose option - C.
> > >
> > > - I cannot comment on mbedtls (2).
> > >
> > > - I think the current BaseCryptLib HMAC APIs (3) are not great, and we
> > > should use this opportunity to improve upon them.
> > >
> > > - Regarding openssl (1), as I understand it, the situation is different
> > > from edk2. In OpenSSL, EVP_MD_CTX is *not* an opaque type (expressed in
> > > C language terms, it is not an "incomplete" type, but a "complete"
> > > type). Therefore, an expression such as sizeof(EVP_MD_CTX) works.
> > >
> > > The consequence is that OpenSSL code itself can use the following style:
> > >
> > > void func(void)
> > > {
> > >   EVP_MD_CTX ctx;
> > >
> > >   EVP_DigestInit_ex(&ctx, ...);
> > >   ...
> > >   EVP_DigestFinal_ex(&ctx, ...)
> > > }
> > >
> > > In other words, OpenSSL-native code is permitted to know the internals
> > > of EVP_MD_CTX, therefore the OpenSSL-internal code may allocate
> > > EVP_MD_CTX with means that are *different* from EVP_MD_CTX_new() and
> > > EVP_MD_CTX_free().
> > >
> > > The caller of (Init, Final) may use (New, Free) for memory management,
> > > or may use something completely different (for example, local variables).
> > >
> > > Therefore, offering the (Init, Final) APIs separately from (New, Free)
> > > is meaningful.
> > >
> > > But the situation in edk2 -- or any other OpenSSL *consumer* -- is
> > > different. In edk2, EVP_MD_CTX is an opaque type -- in C language terms:
> > > an "incomplete" type. OpenSSL deliberately hides the internals of
> > > EVP_MD_CTX.
> > >
> > > See "CryptoPkg/Library/OpensslLib/openssl/include/openssl/ossl_typ.h":
> > >
> > > > typedef struct evp_md_ctx_st EVP_MD_CTX;
> > >
> > > and the "evp_md_ctx_st" structure type is only defined in
> > > "CryptoPkg/Library/OpensslLib/openssl/crypto/evp/evp_local.h", whose
> > > contents -- I think! -- OpenSSL client code cannot, or *should not*,
> > > refer to.
> > >
> > > This means that OpenSSL consumer code can *only* rely on
> > > EVP_MD_CTX_new() and EVP_MD_CTX_free(), for allocating and releasing the
> > > context, respectively. "sizeof(EVP_MD_CTX)" is not permitted, and
> > > defining a local or global variable of type EVP_MD_CTX is also not valid.
> > >
> > > This means that the only reason for separating (Init, Final) from (New,
> > > Free) falls away, in OpenSSL consumer code. In OpenSSL consumer code,
> > > there is no reason to keep *both* pairs of APIs.
> > >
> > >
> > > Please note that, this (very prudent) information hiding / encapsulation
> > > in OpenSSL used to be violated by edk2 in the past, intentionally.
> > > That's what the HmacXxxGetContextSize() APIs were about -- those APIs
> > > forcefully leaked the context sizes to client code, so that client code
> > > in edk2 could perform its own allocation.
> > >
> > > But in <https://bugzilla.tianocore.org/show_bug.cgi?id=1792>, we finally
> > > eliminated HmacXxxGetContextSize(). As a part of that, we also
> > > simplified the HMAC API -- note that Init was replaced by SetKey. And
> > > because we were reworking an existent API, I didn't propose very
> > > intrusive changes -- I didn't propose fusing Final and Free, for example.
> > >
> > > Now, the EVP MD APIs are just being introduced to edk2. So we have a
> > > chance at getting them right, and making them minimal.
> > >
> > > > Second, I believe EVP is just something in openssl. It does not mean that we
> > *have to* design API to follow
> > > openssl.
> > > > After rethink the size impact, I do have concern to merge all Hash
> > implementation together in one function.
> > > > It might means nothing if the crypto library is based upon openssl.
> > > > But if the cryptolib implementation is based upon other crypto, such as Intel
> > IPP
> > >
> > (https://github.com/slimbootloader/slimbootloader/tree/master/BootloaderCo
> > mmonPkg/Library/IppCryptoLib) or mbedTls
> > > (https://github.com/jyao1/edk2/tree/DeviceSecurity/CryptoMbedTlsPkg), then
> > we can NOT get size benefit by separating
> > > the hash algorithm.
> > > >
> > > >
> > > > I would like to propose separate EvpMdxxx.
> > > > EvpMdNew -> Sha256New, Sha384New
> > > > EvpMdInit -> Sha256Init, Sha384Init
> > > > EvpMdUpdate -> Sha256Update, Sha384Update
> > > > EvpMdFinal -> Sha256Final, Sha384Final
> > > > EvpMdFree -> Sha256Free, Sha384Free
> > >
> > > I have no comment on this.
> > >
> > > > We can do similar thing with Hmac, to deprecate Sha256GetContextSize()
> > API,
> > >
> > > Yes, good idea.
> > >
> > > > and replace caller with Sha256New() / Sha384Free()
> > >
> > > Indeed.
> > >
> > > But then -- there's no reason left for keeping (New, Free) separate from
> > > (Init, Final).
> > >
> > > Thanks
> > > Laszlo


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-09-17  1:44 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-16 12:07 [edk2-devel] [PATCH v3 1/3] CryptoPkg/BaseCryptLib: Add EVP (Envelope) Digest interface Yao, Jiewen
2020-09-16 12:39 ` Laszlo Ersek
2020-09-16 12:56   ` Yao, Jiewen
2020-09-16 15:27     ` Laszlo Ersek
2020-09-16 13:31   ` Laszlo Ersek
  -- strict thread matches above, loose matches on Subject: below --
2020-09-16  0:58 [PATCH v3 0/3] " Zurcher, Christopher J
2020-09-16  0:59 ` [PATCH v3 1/3] " Zurcher, Christopher J
2020-09-16 11:06   ` [edk2-devel] " Laszlo Ersek
2020-09-16 11:44     ` Yao, Jiewen
2020-09-16 13:35       ` Laszlo Ersek
2020-09-16 14:17         ` Yao, Jiewen
2020-09-16 16:04           ` Laszlo Ersek
2020-09-17  1:21             ` Zurcher, Christopher J
2020-09-17  1:33               ` Yao, Jiewen
2020-09-17  1:44                 ` Zurcher, Christopher J

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox