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