]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptXkcp.c
CryptoPkg: Add new hash algorithm ParallelHash256HashAll in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptXkcp.c
CommitLineData
c1e66210
ZL
1/** @file\r
2 Encode realted functions from Xkcp.\r
3\r
4Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7Copyright 2022 The eXtended Keccak Code Package (XKCP)\r
8https://github.com/XKCP/XKCP\r
9Keccak, designed by Guido Bertoni, Joan Daemen, Michael Peeters and Gilles Van Assche.\r
10Implementation by the designers, hereby denoted as "the implementer".\r
11For more information, feedback or questions, please refer to the Keccak Team website:\r
12https://keccak.team/\r
13To the extent possible under law, the implementer has waived all copyright\r
14and related or neighboring rights to the source code in this file.\r
15http://creativecommons.org/publicdomain/zero/1.0/\r
16\r
17**/\r
18\r
19#include "CryptParallelHash.h"\r
20\r
21/**\r
22 Encode function from XKCP.\r
23\r
24 Encodes the input as a byte string in a way that can be unambiguously parsed\r
25 from the beginning of the string by inserting the length of the byte string\r
26 before the byte string representation of input.\r
27\r
28 @param[out] EncBuf Result of left encode.\r
29 @param[in] Value Input of left encode.\r
30\r
31 @retval EncLen Size of encode result in bytes.\r
32**/\r
33UINTN\r
34EFIAPI\r
35LeftEncode (\r
36 OUT UINT8 *EncBuf,\r
37 IN UINTN Value\r
38 )\r
39{\r
40 UINT32 BlockNum;\r
41 UINT32 EncLen;\r
42 UINT32 Index;\r
43 UINTN ValueCopy;\r
44\r
45 for ( ValueCopy = Value, BlockNum = 0; ValueCopy && (BlockNum < sizeof (UINTN)); ++BlockNum, ValueCopy >>= 8 ) {\r
46 //\r
47 // Empty\r
48 //\r
49 }\r
50\r
51 if (BlockNum == 0) {\r
52 BlockNum = 1;\r
53 }\r
54\r
55 for (Index = 1; Index <= BlockNum; ++Index) {\r
56 EncBuf[Index] = (UINT8)(Value >> (8 * (BlockNum - Index)));\r
57 }\r
58\r
59 EncBuf[0] = (UINT8)BlockNum;\r
60 EncLen = BlockNum + 1;\r
61\r
62 return EncLen;\r
63}\r
64\r
65/**\r
66 Encode function from XKCP.\r
67\r
68 Encodes the input as a byte string in a way that can be unambiguously parsed\r
69 from the end of the string by inserting the length of the byte string after\r
70 the byte string representation of input.\r
71\r
72 @param[out] EncBuf Result of right encode.\r
73 @param[in] Value Input of right encode.\r
74\r
75 @retval EncLen Size of encode result in bytes.\r
76**/\r
77UINTN\r
78EFIAPI\r
79RightEncode (\r
80 OUT UINT8 *EncBuf,\r
81 IN UINTN Value\r
82 )\r
83{\r
84 UINT32 BlockNum;\r
85 UINT32 EncLen;\r
86 UINT32 Index;\r
87 UINTN ValueCopy;\r
88\r
89 for (ValueCopy = Value, BlockNum = 0; ValueCopy && (BlockNum < sizeof (UINTN)); ++BlockNum, ValueCopy >>= 8) {\r
90 //\r
91 // Empty\r
92 //\r
93 }\r
94\r
95 if (BlockNum == 0) {\r
96 BlockNum = 1;\r
97 }\r
98\r
99 for (Index = 1; Index <= BlockNum; ++Index) {\r
100 EncBuf[Index-1] = (UINT8)(Value >> (8 * (BlockNum-Index)));\r
101 }\r
102\r
103 EncBuf[BlockNum] = (UINT8)BlockNum;\r
104 EncLen = BlockNum + 1;\r
105\r
106 return EncLen;\r
107}\r