]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Hash2DxeCrypto/Hash2DxeCrypto.c
c1c0470be9939134a9d5197faec591d95c40bd75
[mirror_edk2.git] / SecurityPkg / Hash2DxeCrypto / Hash2DxeCrypto.c
1 /** @file
2 This module implements Hash2 Protocol.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Protocol/Hash2.h>
12 #include <Library/BaseLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseCryptLib.h>
18
19 #include "Driver.h"
20
21 /**
22 Retrieves the size, in bytes, of the context buffer required for hash operations.
23
24 If this interface is not supported, then return zero.
25
26 @return The size, in bytes, of the context buffer required for hash operations.
27 @retval 0 This interface is not supported.
28
29 **/
30 typedef
31 UINTN
32 (EFIAPI *EFI_HASH_GET_CONTEXT_SIZE) (
33 VOID
34 );
35
36 /**
37 Initializes user-supplied memory pointed by Sha1Context as hash context for
38 subsequent use.
39
40 If HashContext is NULL, then return FALSE.
41 If this interface is not supported, then return FALSE.
42
43 @param[out] HashContext Pointer to Hashcontext being initialized.
44
45 @retval TRUE Hash context initialization succeeded.
46 @retval FALSE Hash context initialization failed.
47 @retval FALSE This interface is not supported.
48
49 **/
50 typedef
51 BOOLEAN
52 (EFIAPI *EFI_HASH_INIT) (
53 OUT VOID *HashContext
54 );
55
56 /**
57 Digests the input data and updates Hash context.
58
59 This function performs Hash digest on a data buffer of the specified size.
60 It can be called multiple times to compute the digest of long or discontinuous data streams.
61 Hash context should be already correctly initialized by HashInit(), and should not be finalized
62 by HashFinal(). Behavior with invalid context is undefined.
63
64 If HashContext is NULL, then return FALSE.
65 If this interface is not supported, then return FALSE.
66
67 @param[in, out] HashContext Pointer to the Hash context.
68 @param[in] Data Pointer to the buffer containing the data to be hashed.
69 @param[in] DataSize Size of Data buffer in bytes.
70
71 @retval TRUE SHA-1 data digest succeeded.
72 @retval FALSE SHA-1 data digest failed.
73 @retval FALSE This interface is not supported.
74
75 **/
76 typedef
77 BOOLEAN
78 (EFIAPI *EFI_HASH_UPDATE) (
79 IN OUT VOID *HashContext,
80 IN CONST VOID *Data,
81 IN UINTN DataSize
82 );
83
84 /**
85 Completes computation of the Hash digest value.
86
87 This function completes hash computation and retrieves the digest value into
88 the specified memory. After this function has been called, the Hash context cannot
89 be used again.
90 Hash context should be already correctly initialized by HashInit(), and should not be
91 finalized by HashFinal(). Behavior with invalid Hash context is undefined.
92
93 If HashContext is NULL, then return FALSE.
94 If HashValue is NULL, then return FALSE.
95 If this interface is not supported, then return FALSE.
96
97 @param[in, out] HashContext Pointer to the Hash context.
98 @param[out] HashValue Pointer to a buffer that receives the Hash digest
99 value.
100
101 @retval TRUE Hash digest computation succeeded.
102 @retval FALSE Hash digest computation failed.
103 @retval FALSE This interface is not supported.
104
105 **/
106 typedef
107 BOOLEAN
108 (EFIAPI *EFI_HASH_FINAL) (
109 IN OUT VOID *HashContext,
110 OUT UINT8 *HashValue
111 );
112
113 typedef struct {
114 EFI_GUID *Guid;
115 UINT32 HashSize;
116 EFI_HASH_GET_CONTEXT_SIZE GetContextSize;
117 EFI_HASH_INIT Init;
118 EFI_HASH_UPDATE Update;
119 EFI_HASH_FINAL Final;
120 } EFI_HASH_INFO;
121
122 EFI_HASH_INFO mHashInfo[] = {
123 {&gEfiHashAlgorithmSha256Guid, sizeof(EFI_SHA256_HASH2), Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final },
124 {&gEfiHashAlgorithmSha384Guid, sizeof(EFI_SHA384_HASH2), Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final },
125 {&gEfiHashAlgorithmSha512Guid, sizeof(EFI_SHA512_HASH2), Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final },
126 };
127
128 /**
129 Returns the size of the hash which results from a specific algorithm.
130
131 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
132 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
133 @param[out] HashSize Holds the returned size of the algorithm's hash.
134
135 @retval EFI_SUCCESS Hash size returned successfully.
136 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
137 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
138 or HashAlgorithm is null.
139
140 **/
141 EFI_STATUS
142 EFIAPI
143 BaseCrypto2GetHashSize (
144 IN CONST EFI_HASH2_PROTOCOL *This,
145 IN CONST EFI_GUID *HashAlgorithm,
146 OUT UINTN *HashSize
147 );
148
149 /**
150 Creates a hash for the specified message text. The hash is not extendable.
151 The output is final with any algorithm-required padding added by the function.
152
153 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
154 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
155 @param[in] Message Points to the start of the message.
156 @param[in] MessageSize The size of Message, in bytes.
157 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
158 returned by GetHashSize() for the specified HashAlgorithm.
159 On output, the buffer holds the resulting hash computed from the message.
160
161 @retval EFI_SUCCESS Hash returned successfully.
162 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
163 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
164 or HashAlgorithm is Null.
165 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
166 or MessageSize is greater than platform maximum.
167
168 **/
169 EFI_STATUS
170 EFIAPI
171 BaseCrypto2Hash (
172 IN CONST EFI_HASH2_PROTOCOL *This,
173 IN CONST EFI_GUID *HashAlgorithm,
174 IN CONST UINT8 *Message,
175 IN UINTN MessageSize,
176 IN OUT EFI_HASH2_OUTPUT *Hash
177 );
178
179 /**
180 This function must be called to initialize a digest calculation to be subsequently performed using the
181 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
182
183 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
184 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
185
186 @retval EFI_SUCCESS Initialized successfully.
187 @retval EFI_INVALID_PARAMETER This is NULL.
188 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
189 or HashAlgorithm is Null.
190 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
191 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
192 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
193
194 **/
195 EFI_STATUS
196 EFIAPI
197 BaseCrypto2HashInit (
198 IN CONST EFI_HASH2_PROTOCOL *This,
199 IN CONST EFI_GUID *HashAlgorithm
200 );
201
202 /**
203 Updates the hash of a computation in progress by adding a message text.
204
205 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
206 @param[in] Message Points to the start of the message.
207 @param[in] MessageSize The size of Message, in bytes.
208
209 @retval EFI_SUCCESS Digest in progress updated successfully.
210 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
211 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
212 or MessageSize is greater than platform maximum.
213 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
214 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
215
216 **/
217 EFI_STATUS
218 EFIAPI
219 BaseCrypto2HashUpdate (
220 IN CONST EFI_HASH2_PROTOCOL *This,
221 IN CONST UINT8 *Message,
222 IN UINTN MessageSize
223 );
224
225 /**
226 Finalizes a hash operation in progress and returns calculation result.
227 The output is final with any necessary padding added by the function.
228 The hash may not be further updated or extended after HashFinal().
229
230 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
231 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
232 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
233 On output, the buffer holds the resulting hash computed from the message.
234
235 @retval EFI_SUCCESS Hash returned successfully.
236 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
237 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
238 or the operation in progress was canceled by a call to Hash() on the same instance.
239
240 **/
241 EFI_STATUS
242 EFIAPI
243 BaseCrypto2HashFinal (
244 IN CONST EFI_HASH2_PROTOCOL *This,
245 IN OUT EFI_HASH2_OUTPUT *Hash
246 );
247
248 EFI_HASH2_PROTOCOL mHash2Protocol = {
249 BaseCrypto2GetHashSize,
250 BaseCrypto2Hash,
251 BaseCrypto2HashInit,
252 BaseCrypto2HashUpdate,
253 BaseCrypto2HashFinal,
254 };
255
256 /**
257 Returns hash information.
258
259 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
260
261 @return Hash information.
262 **/
263 EFI_HASH_INFO *
264 GetHashInfo (
265 IN CONST EFI_GUID *HashAlgorithm
266 )
267 {
268 UINTN Index;
269
270 for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {
271 if (CompareGuid (HashAlgorithm, mHashInfo[Index].Guid)) {
272 return &mHashInfo[Index];
273 }
274 }
275 return NULL;
276 }
277
278 /**
279 Returns the size of the hash which results from a specific algorithm.
280
281 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
282 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
283 @param[out] HashSize Holds the returned size of the algorithm's hash.
284
285 @retval EFI_SUCCESS Hash size returned successfully.
286 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
287 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
288 or HashAlgorithm is null.
289
290 **/
291 EFI_STATUS
292 EFIAPI
293 BaseCrypto2GetHashSize (
294 IN CONST EFI_HASH2_PROTOCOL *This,
295 IN CONST EFI_GUID *HashAlgorithm,
296 OUT UINTN *HashSize
297 )
298 {
299 EFI_HASH_INFO *HashInfo;
300
301 if ((This == NULL) || (HashSize == NULL)) {
302 return EFI_INVALID_PARAMETER;
303 }
304
305 if (HashAlgorithm == NULL) {
306 return EFI_UNSUPPORTED;
307 }
308
309 HashInfo = GetHashInfo (HashAlgorithm);
310 if (HashInfo == NULL) {
311 return EFI_UNSUPPORTED;
312 }
313
314 *HashSize = HashInfo->HashSize;
315 return EFI_SUCCESS;
316 }
317
318 /**
319 Creates a hash for the specified message text. The hash is not extendable.
320 The output is final with any algorithm-required padding added by the function.
321
322 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
323 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
324 @param[in] Message Points to the start of the message.
325 @param[in] MessageSize The size of Message, in bytes.
326 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
327 returned by GetHashSize() for the specified HashAlgorithm.
328 On output, the buffer holds the resulting hash computed from the message.
329
330 @retval EFI_SUCCESS Hash returned successfully.
331 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
332 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
333 or HashAlgorithm is Null.
334 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
335 or MessageSize is greater than platform maximum.
336
337 **/
338 EFI_STATUS
339 EFIAPI
340 BaseCrypto2Hash (
341 IN CONST EFI_HASH2_PROTOCOL *This,
342 IN CONST EFI_GUID *HashAlgorithm,
343 IN CONST UINT8 *Message,
344 IN UINTN MessageSize,
345 IN OUT EFI_HASH2_OUTPUT *Hash
346 )
347 {
348 EFI_HASH_INFO *HashInfo;
349 VOID *HashCtx;
350 UINTN CtxSize;
351 BOOLEAN Ret;
352 EFI_STATUS Status;
353 HASH2_INSTANCE_DATA *Instance;
354
355 Status = EFI_SUCCESS;
356
357 if ((This == NULL) || (Hash == NULL)) {
358 return EFI_INVALID_PARAMETER;
359 }
360
361 if (HashAlgorithm == NULL) {
362 return EFI_UNSUPPORTED;
363 }
364
365 HashInfo = GetHashInfo (HashAlgorithm);
366 if (HashInfo == NULL) {
367 return EFI_UNSUPPORTED;
368 }
369
370 Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);
371 if (Instance->HashContext != NULL) {
372 FreePool (Instance->HashContext);
373 }
374 Instance->HashInfoContext = NULL;
375 Instance->HashContext = NULL;
376
377 //
378 // Start hash sequence
379 //
380 CtxSize = HashInfo->GetContextSize ();
381 if (CtxSize == 0) {
382 return EFI_UNSUPPORTED;
383 }
384 HashCtx = AllocatePool (CtxSize);
385 if (HashCtx == NULL) {
386 return EFI_OUT_OF_RESOURCES;
387 }
388
389 Ret = HashInfo->Init (HashCtx);
390 if (!Ret) {
391 Status = EFI_OUT_OF_RESOURCES;
392 goto Done;
393 }
394
395 //
396 // Setup the context
397 //
398 Instance->HashContext = HashCtx;
399 Instance->HashInfoContext = HashInfo;
400
401 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
402 if (!Ret) {
403 Status = EFI_OUT_OF_RESOURCES;
404 goto Done;
405 }
406
407 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
408 if (!Ret) {
409 Status = EFI_OUT_OF_RESOURCES;
410 goto Done;
411 }
412 Done:
413 //
414 // Cleanup the context
415 //
416 FreePool (HashCtx);
417 Instance->HashInfoContext = NULL;
418 Instance->HashContext = NULL;
419 return Status;
420 }
421
422 /**
423 This function must be called to initialize a digest calculation to be subsequently performed using the
424 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
425
426 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
427 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
428
429 @retval EFI_SUCCESS Initialized successfully.
430 @retval EFI_INVALID_PARAMETER This is NULL.
431 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
432 or HashAlgorithm is Null.
433 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
434 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
435 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
436
437 **/
438 EFI_STATUS
439 EFIAPI
440 BaseCrypto2HashInit (
441 IN CONST EFI_HASH2_PROTOCOL *This,
442 IN CONST EFI_GUID *HashAlgorithm
443 )
444 {
445 EFI_HASH_INFO *HashInfo;
446 VOID *HashCtx;
447 UINTN CtxSize;
448 BOOLEAN Ret;
449 HASH2_INSTANCE_DATA *Instance;
450
451 if (This == NULL) {
452 return EFI_INVALID_PARAMETER;
453 }
454
455 if (HashAlgorithm == NULL) {
456 return EFI_UNSUPPORTED;
457 }
458
459 HashInfo = GetHashInfo (HashAlgorithm);
460 if (HashInfo == NULL) {
461 return EFI_UNSUPPORTED;
462 }
463
464 //
465 // Consistency Check
466 //
467 Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);
468 if ((Instance->HashContext != NULL) || (Instance->HashInfoContext != NULL)) {
469 return EFI_ALREADY_STARTED;
470 }
471
472 //
473 // Start hash sequence
474 //
475 CtxSize = HashInfo->GetContextSize ();
476 if (CtxSize == 0) {
477 return EFI_UNSUPPORTED;
478 }
479 HashCtx = AllocatePool (CtxSize);
480 if (HashCtx == NULL) {
481 return EFI_OUT_OF_RESOURCES;
482 }
483
484 Ret = HashInfo->Init (HashCtx);
485 if (!Ret) {
486 FreePool (HashCtx);
487 return EFI_OUT_OF_RESOURCES;
488 }
489
490 //
491 // Setup the context
492 //
493 Instance->HashContext = HashCtx;
494 Instance->HashInfoContext = HashInfo;
495 Instance->Updated = FALSE;
496
497 return EFI_SUCCESS;
498 }
499
500 /**
501 Updates the hash of a computation in progress by adding a message text.
502
503 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
504 @param[in] Message Points to the start of the message.
505 @param[in] MessageSize The size of Message, in bytes.
506
507 @retval EFI_SUCCESS Digest in progress updated successfully.
508 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
509 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
510 or MessageSize is greater than platform maximum.
511 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
512 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
513
514 **/
515 EFI_STATUS
516 EFIAPI
517 BaseCrypto2HashUpdate (
518 IN CONST EFI_HASH2_PROTOCOL *This,
519 IN CONST UINT8 *Message,
520 IN UINTN MessageSize
521 )
522 {
523 EFI_HASH_INFO *HashInfo;
524 VOID *HashCtx;
525 BOOLEAN Ret;
526 HASH2_INSTANCE_DATA *Instance;
527
528 if (This == NULL) {
529 return EFI_INVALID_PARAMETER;
530 }
531
532 //
533 // Consistency Check
534 //
535 Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);
536 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL)) {
537 return EFI_NOT_READY;
538 }
539 HashInfo = Instance->HashInfoContext;
540 HashCtx = Instance->HashContext;
541
542 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
543 if (!Ret) {
544 return EFI_OUT_OF_RESOURCES;
545 }
546
547 Instance->Updated = TRUE;
548
549 return EFI_SUCCESS;
550 }
551
552 /**
553 Finalizes a hash operation in progress and returns calculation result.
554 The output is final with any necessary padding added by the function.
555 The hash may not be further updated or extended after HashFinal().
556
557 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
558 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
559 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
560 On output, the buffer holds the resulting hash computed from the message.
561
562 @retval EFI_SUCCESS Hash returned successfully.
563 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
564 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
565 or the operation in progress was canceled by a call to Hash() on the same instance.
566
567 **/
568 EFI_STATUS
569 EFIAPI
570 BaseCrypto2HashFinal (
571 IN CONST EFI_HASH2_PROTOCOL *This,
572 IN OUT EFI_HASH2_OUTPUT *Hash
573 )
574 {
575 EFI_HASH_INFO *HashInfo;
576 VOID *HashCtx;
577 BOOLEAN Ret;
578 HASH2_INSTANCE_DATA *Instance;
579
580 if ((This == NULL) || (Hash == NULL)) {
581 return EFI_INVALID_PARAMETER;
582 }
583
584 //
585 // Consistency Check
586 //
587 Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);
588 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL) ||
589 (!Instance->Updated)) {
590 return EFI_NOT_READY;
591 }
592 HashInfo = Instance->HashInfoContext;
593 HashCtx = Instance->HashContext;
594
595 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
596
597 //
598 // Cleanup the context
599 //
600 FreePool (HashCtx);
601 Instance->HashInfoContext = NULL;
602 Instance->HashContext = NULL;
603 Instance->Updated = FALSE;
604
605 if (!Ret) {
606 return EFI_OUT_OF_RESOURCES;
607 }
608
609 return EFI_SUCCESS;
610 }