summaryrefslogtreecommitdiffstats
path: root/CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.c
blob: ede9fa8c09ec10f91244c81927ccddafd36e3839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/** @file
  Unified Hash API Implementation

  This file implements the Unified Hash API.

  This API, when called, will calculate the Hash using the
  hashing algorithm specified by PcdHashApiLibPolicy.

  Copyright (c) 2020-2022, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Base.h>
#include <IndustryStandard/Tpm20.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/HashApiLib.h>

/**
  Retrieves the size, in bytes, of the context buffer required for hash operations.

  @return  The size, in bytes, of the context buffer required for hash operations.
**/
UINTN
EFIAPI
HashApiGetContextSize (
  VOID
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1GetContextSize ();
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256GetContextSize ();
      break;

    case HASH_ALG_SHA384:
      return Sha384GetContextSize ();
      break;

    case HASH_ALG_SHA512:
      return Sha512GetContextSize ();
      break;

    case HASH_ALG_SM3_256:
      return Sm3GetContextSize ();
      break;

    default:
      ASSERT (FALSE);
      return 0;
      break;
  }
}

/**
  Init hash sequence.

  @param[out] HashContext   Hash context.

  @retval TRUE         Hash start and HashHandle returned.
  @retval FALSE        Hash Init unsuccessful.
**/
BOOLEAN
EFIAPI
HashApiInit (
  OUT HASH_API_CONTEXT  HashContext
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1Init (HashContext);
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256Init (HashContext);
      break;

    case HASH_ALG_SHA384:
      return Sha384Init (HashContext);
      break;

    case HASH_ALG_SHA512:
      return Sha512Init (HashContext);
      break;

    case HASH_ALG_SM3_256:
      return Sm3Init (HashContext);
      break;

    default:
      ASSERT (FALSE);
      return FALSE;
      break;
  }
}

/**
  Makes a copy of an existing hash context.

  @param[in]  HashContext     Hash context.
  @param[out] NewHashContext  New copy of hash context.

  @retval TRUE         Hash context copy succeeded.
  @retval FALSE        Hash context copy failed.
**/
BOOLEAN
EFIAPI
HashApiDuplicate (
  IN  HASH_API_CONTEXT  HashContext,
  OUT HASH_API_CONTEXT  NewHashContext
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1Duplicate (HashContext, NewHashContext);
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256Duplicate (HashContext, NewHashContext);
      break;

    case HASH_ALG_SHA384:
      return Sha384Duplicate (HashContext, NewHashContext);
      break;

    case HASH_ALG_SHA512:
      return Sha512Duplicate (HashContext, NewHashContext);
      break;

    case HASH_ALG_SM3_256:
      return Sm3Duplicate (HashContext, NewHashContext);
      break;

    default:
      ASSERT (FALSE);
      return FALSE;
      break;
  }
}

/**
  Update hash data.

  @param[in] HashContext   Hash context.
  @param[in] DataToHash    Data to be hashed.
  @param[in] DataToHashLen Data size.

  @retval TRUE         Hash updated.
  @retval FALSE        Hash updated unsuccessful.
**/
BOOLEAN
EFIAPI
HashApiUpdate (
  IN HASH_API_CONTEXT  HashContext,
  IN VOID              *DataToHash,
  IN UINTN             DataToHashLen
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1Update (HashContext, DataToHash, DataToHashLen);
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256Update (HashContext, DataToHash, DataToHashLen);
      break;

    case HASH_ALG_SHA384:
      return Sha384Update (HashContext, DataToHash, DataToHashLen);
      break;

    case HASH_ALG_SHA512:
      return Sha512Update (HashContext, DataToHash, DataToHashLen);
      break;

    case HASH_ALG_SM3_256:
      return Sm3Update (HashContext, DataToHash, DataToHashLen);
      break;

    default:
      ASSERT (FALSE);
      return FALSE;
      break;
  }
}

/**
  Hash complete.

  @param[in]  HashContext  Hash context.
  @param[out] Digest       Hash Digest.

  @retval TRUE         Hash complete and Digest is returned.
  @retval FALSE        Hash complete unsuccessful.
**/
BOOLEAN
EFIAPI
HashApiFinal (
  IN  HASH_API_CONTEXT  HashContext,
  OUT UINT8             *Digest
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1Final (HashContext, Digest);
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256Final (HashContext, Digest);
      break;

    case HASH_ALG_SHA384:
      return Sha384Final (HashContext, Digest);
      break;

    case HASH_ALG_SHA512:
      return Sha512Final (HashContext, Digest);
      break;

    case HASH_ALG_SM3_256:
      return Sm3Final (HashContext, Digest);
      break;

    default:
      ASSERT (FALSE);
      return FALSE;
      break;
  }
}

/**
  Computes hash message digest of a input data buffer.

  @param[in]  DataToHash     Data to be hashed.
  @param[in]  DataToHashLen  Data size.
  @param[out] Digest         Hash Digest.

  @retval TRUE   Hash digest computation succeeded.
  @retval FALSE  Hash digest computation failed.
**/
BOOLEAN
EFIAPI
HashApiHashAll (
  IN  CONST VOID  *DataToHash,
  IN  UINTN       DataToHashLen,
  OUT UINT8       *Digest
  )
{
  switch (PcdGet32 (PcdHashApiLibPolicy)) {
 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
    case HASH_ALG_SHA1:
      return Sha1HashAll (DataToHash, DataToHashLen, Digest);
      break;
 #endif

    case HASH_ALG_SHA256:
      return Sha256HashAll (DataToHash, DataToHashLen, Digest);
      break;

    case HASH_ALG_SHA384:
      return Sha384HashAll (DataToHash, DataToHashLen, Digest);
      break;

    case HASH_ALG_SHA512:
      return Sha512HashAll (DataToHash, DataToHashLen, Digest);
      break;

    case HASH_ALG_SM3_256:
      return Sm3HashAll (DataToHash, DataToHashLen, Digest);
      break;

    default:
      ASSERT (FALSE);
      return FALSE;
      break;
  }
}