]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Hash/CryptXkcp.c
CryptoPkg: Add new hash algorithm ParallelHash256HashAll in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptXkcp.c
diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptXkcp.c b/CryptoPkg/Library/BaseCryptLib/Hash/CryptXkcp.c
new file mode 100644 (file)
index 0000000..12c46cf
--- /dev/null
@@ -0,0 +1,107 @@
+/** @file\r
+  Encode realted functions from Xkcp.\r
+\r
+Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\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
+\r
+#include "CryptParallelHash.h"\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
+  UINT32  BlockNum;\r
+  UINT32  EncLen;\r
+  UINT32  Index;\r
+  UINTN   ValueCopy;\r
+\r
+  for ( ValueCopy = Value, BlockNum = 0; ValueCopy && (BlockNum < sizeof (UINTN)); ++BlockNum, ValueCopy >>= 8 ) {\r
+    //\r
+    // Empty\r
+    //\r
+  }\r
+\r
+  if (BlockNum == 0) {\r
+    BlockNum = 1;\r
+  }\r
+\r
+  for (Index = 1; Index <= BlockNum; ++Index) {\r
+    EncBuf[Index] = (UINT8)(Value >> (8 * (BlockNum - Index)));\r
+  }\r
+\r
+  EncBuf[0] = (UINT8)BlockNum;\r
+  EncLen    = BlockNum + 1;\r
+\r
+  return EncLen;\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
+  UINT32  BlockNum;\r
+  UINT32  EncLen;\r
+  UINT32  Index;\r
+  UINTN   ValueCopy;\r
+\r
+  for (ValueCopy = Value, BlockNum = 0; ValueCopy && (BlockNum < sizeof (UINTN)); ++BlockNum, ValueCopy >>= 8) {\r
+    //\r
+    // Empty\r
+    //\r
+  }\r
+\r
+  if (BlockNum == 0) {\r
+    BlockNum = 1;\r
+  }\r
+\r
+  for (Index = 1; Index <= BlockNum; ++Index) {\r
+    EncBuf[Index-1] = (UINT8)(Value >> (8 * (BlockNum-Index)));\r
+  }\r
+\r
+  EncBuf[BlockNum] = (UINT8)BlockNum;\r
+  EncLen           = BlockNum + 1;\r
+\r
+  return EncLen;\r
+}\r