TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptSha3.c
Go to the documentation of this file.
1
14#include "CryptParallelHash.h"
15
29UINT8
30EFIAPI
32 OUT Keccak1600_Ctx *Context,
33 IN UINT8 Pad,
34 IN UINTN BlockSize,
35 IN UINTN MessageDigestLen
36 )
37{
38 if (BlockSize <= sizeof (Context->buf)) {
39 memset (Context->A, 0, sizeof (Context->A));
40
41 Context->num = 0;
42 Context->block_size = BlockSize;
43 Context->md_size = MessageDigestLen;
44 Context->pad = Pad;
45
46 return 1;
47 }
48
49 return 0;
50}
51
64UINT8
65EFIAPI
67 IN OUT Keccak1600_Ctx *Context,
68 IN const VOID *Data,
69 IN UINTN DataSize
70 )
71{
72 const UINT8 *DataCopy;
73 UINTN BlockSize;
74 UINTN Num;
75 UINTN Rem;
76
77 DataCopy = Data;
78 BlockSize = (UINT8)(Context->block_size);
79
80 if (DataSize == 0) {
81 return 1;
82 }
83
84 if ((Num = Context->num) != 0) {
85 //
86 // process intermediate buffer
87 //
88 Rem = BlockSize - Num;
89
90 if (DataSize < Rem) {
91 memcpy (Context->buf + Num, DataCopy, DataSize);
92 Context->num += DataSize;
93 return 1;
94 }
95
96 //
97 // We have enough data to fill or overflow the intermediate
98 // buffer. So we append |Rem| bytes and process the block,
99 // leaving the rest for later processing.
100 //
101 memcpy (Context->buf + Num, DataCopy, Rem);
102 DataCopy += Rem;
103 DataSize -= Rem;
104 (void)SHA3_absorb (Context->A, Context->buf, BlockSize, BlockSize);
105 Context->num = 0;
106 // Context->buf is processed, Context->num is guaranteed to be zero.
107 }
108
109 if (DataSize >= BlockSize) {
110 Rem = SHA3_absorb (Context->A, DataCopy, DataSize, BlockSize);
111 } else {
112 Rem = DataSize;
113 }
114
115 if (Rem > 0) {
116 memcpy (Context->buf, DataCopy + DataSize - Rem, Rem);
117 Context->num = Rem;
118 }
119
120 return 1;
121}
122
135UINT8
136EFIAPI
138 IN OUT Keccak1600_Ctx *Context,
139 OUT UINT8 *MessageDigest
140 )
141{
142 UINTN BlockSize;
143 UINTN Num;
144
145 BlockSize = Context->block_size;
146 Num = Context->num;
147
148 if (Context->md_size == 0) {
149 return 1;
150 }
151
152 //
153 // Pad the data with 10*1. Note that |Num| can be |BlockSize - 1|
154 // in which case both byte operations below are performed on
155 // same byte.
156 //
157 memset (Context->buf + Num, 0, BlockSize - Num);
158 Context->buf[Num] = Context->pad;
159 Context->buf[BlockSize - 1] |= 0x80;
160
161 (void)SHA3_absorb (Context->A, Context->buf, BlockSize, BlockSize);
162
163 SHA3_squeeze (Context->A, MessageDigest, Context->md_size, BlockSize);
164
165 return 1;
166}
UINT64 UINTN
void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r)
size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len, size_t r)
UINT8 EFIAPI KeccakInit(OUT Keccak1600_Ctx *Context, IN UINT8 Pad, IN UINTN BlockSize, IN UINTN MessageDigestLen)
Definition: CryptSha3.c:31
UINT8 EFIAPI Sha3Final(IN OUT Keccak1600_Ctx *Context, OUT UINT8 *MessageDigest)
Definition: CryptSha3.c:137
UINT8 EFIAPI Sha3Update(IN OUT Keccak1600_Ctx *Context, IN const VOID *Data, IN UINTN DataSize)
Definition: CryptSha3.c:66
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284