]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h
CryptoPkg: Add new hash algorithm ParallelHash256HashAll in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptParallelHash.h
diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h b/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h
new file mode 100644 (file)
index 0000000..fe08d49
--- /dev/null
@@ -0,0 +1,201 @@
+/** @file\r
+  ParallelHash related function and type declaration.\r
+\r
+Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.\r
+Licensed under the OpenSSL license (the "License").  You may not use\r
+this file except in compliance with the License.  You can obtain a copy\r
+in the file LICENSE in the source distribution or at\r
+https://www.openssl.org/source/license.html\r
+\r
+Copyright 2022 The eXtended Keccak Code Package (XKCP)\r
+https://github.com/XKCP/XKCP\r
+Keccak, designed by Guido Bertoni, Joan Daemen, Michael Peeters and Gilles Van Assche.\r
+Implementation by the designers, hereby denoted as "the implementer".\r
+For more information, feedback or questions, please refer to the Keccak Team website:\r
+https://keccak.team/\r
+To the extent possible under law, the implementer has waived all copyright\r
+and related or neighboring rights to the source code in this file.\r
+http://creativecommons.org/publicdomain/zero/1.0/\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+#define KECCAK1600_WIDTH  1600\r
+\r
+//\r
+// This struct referring to m_sha3.c from opessl and modified its type name.\r
+//\r
+typedef struct {\r
+  uint64_t         A[5][5];\r
+  size_t           block_size;  /* cached ctx->digest->block_size */\r
+  size_t           md_size;     /* output length, variable in XOF */\r
+  size_t           num;         /* used bytes in below buffer */\r
+  unsigned char    buf[KECCAK1600_WIDTH / 8 - 32];\r
+  unsigned char    pad;\r
+} Keccak1600_Ctx;\r
+\r
+/**\r
+  SHA3_absorb can be called multiple times, but at each invocation\r
+  largest multiple of |r| out of |len| bytes are processed. Then\r
+  remaining amount of bytes is returned. This is done to spare caller\r
+  trouble of calculating the largest multiple of |r|. |r| can be viewed\r
+  as blocksize. It is commonly (1600 - 256*n)/8, e.g. 168, 136, 104,\r
+  72, but can also be (1600 - 448)/8 = 144. All this means that message\r
+  padding and intermediate sub-block buffering, byte- or bitwise, is\r
+  caller's responsibility.\r
+**/\r
+size_t\r
+SHA3_absorb (\r
+  uint64_t             A[5][5],\r
+  const unsigned char  *inp,\r
+  size_t               len,\r
+  size_t               r\r
+  );\r
+\r
+/**\r
+  SHA3_squeeze is called once at the end to generate |out| hash value\r
+  of |len| bytes.\r
+**/\r
+void\r
+SHA3_squeeze (\r
+  uint64_t       A[5][5],\r
+  unsigned char  *out,\r
+  size_t         len,\r
+  size_t         r\r
+  );\r
+\r
+/**\r
+  Encode function from XKCP.\r
+\r
+  Encodes the input as a byte string in a way that can be unambiguously parsed\r
+  from the beginning of the string by inserting the length of the byte string\r
+  before the byte string representation of input.\r
+\r
+  @param[out] EncBuf  Result of left encode.\r
+  @param[in]  Value   Input of left encode.\r
+\r
+  @retval EncLen  Size of encode result in bytes.\r
+**/\r
+UINTN\r
+EFIAPI\r
+LeftEncode (\r
+  OUT UINT8  *EncBuf,\r
+  IN  UINTN  Value\r
+  );\r
+\r
+/**\r
+  Encode function from XKCP.\r
+\r
+  Encodes the input as a byte string in a way that can be unambiguously parsed\r
+  from the end of the string by inserting the length of the byte string after\r
+  the byte string representation of input.\r
+\r
+  @param[out] EncBuf  Result of right encode.\r
+  @param[in]  Value   Input of right encode.\r
+\r
+  @retval EncLen  Size of encode result in bytes.\r
+**/\r
+UINTN\r
+EFIAPI\r
+RightEncode (\r
+  OUT UINT8  *EncBuf,\r
+  IN  UINTN  Value\r
+  );\r
+\r
+/**\r
+  Keccak initial fuction.\r
+\r
+  Set up state with specified capacity.\r
+\r
+  @param[out] Context           Pointer to the context being initialized.\r
+  @param[in]  Pad               Delimited Suffix.\r
+  @param[in]  BlockSize         Size of context block.\r
+  @param[in]  MessageDigestLen  Size of message digest in bytes.\r
+\r
+  @retval 1  Initialize successfully.\r
+  @retval 0  Fail to initialize.\r
+**/\r
+UINT8\r
+EFIAPI\r
+KeccakInit (\r
+  OUT Keccak1600_Ctx  *Context,\r
+  IN  UINT8           Pad,\r
+  IN  UINTN           BlockSize,\r
+  IN  UINTN           MessageDigstLen\r
+  );\r
+\r
+/**\r
+  Sha3 update fuction.\r
+\r
+  This function performs Sha3 digest on a data buffer of the specified size.\r
+  It can be called multiple times to compute the digest of long or discontinuous data streams.\r
+\r
+  @param[in,out] Context   Pointer to the Keccak context.\r
+  @param[in]     Data      Pointer to the buffer containing the data to be hashed.\r
+  @param[in]     DataSize  Size of Data buffer in bytes.\r
+\r
+  @retval 1  Update successfully.\r
+**/\r
+UINT8\r
+EFIAPI\r
+Sha3Update (\r
+  IN OUT Keccak1600_Ctx  *Context,\r
+  IN const VOID          *Data,\r
+  IN UINTN               DataSize\r
+  );\r
+\r
+/**\r
+  Completes computation of Sha3 message digest.\r
+\r
+  This function completes sha3 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the keccak context cannot\r
+  be used again.\r
+\r
+  @param[in, out]  Context        Pointer to the keccak context.\r
+  @param[out]      MessageDigest  Pointer to a buffer that receives the message digest.\r
+\r
+  @retval 1   Meaasge digest computation succeeded.\r
+**/\r
+UINT8\r
+EFIAPI\r
+Sha3Final (\r
+  IN OUT Keccak1600_Ctx  *Context,\r
+  OUT    UINT8           *MessageDigest\r
+  );\r
+\r
+/**\r
+  Computes the CSHAKE-256 message digest of a input data buffer.\r
+\r
+  This function performs the CSHAKE-256 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  @param[in]   Data               Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize           Size of Data buffer in bytes.\r
+  @param[in]   OutputLen          Size of output in bytes.\r
+  @param[in]   Name               Pointer to the function name string.\r
+  @param[in]   NameLen            Size of the function name in bytes.\r
+  @param[in]   Customization      Pointer to the customization string.\r
+  @param[in]   CustomizationLen   Size of the customization string in bytes.\r
+  @param[out]  HashValue          Pointer to a buffer that receives the CSHAKE-256 digest\r
+                                  value.\r
+\r
+  @retval TRUE   CSHAKE-256 digest computation succeeded.\r
+  @retval FALSE  CSHAKE-256 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+CShake256HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  IN   UINTN       OutputLen,\r
+  IN   CONST VOID  *Name,\r
+  IN   UINTN       NameLen,\r
+  IN   CONST VOID  *Customization,\r
+  IN   UINTN       CustomizationLen,\r
+  OUT  UINT8       *HashValue\r
+  );\r