]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Hash2DxeCrypto/Hash2DxeCrypto.c
1498d5e9eb2986e592b1167d34a6852d5d28342f
[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
276 return NULL;
277 }
278
279 /**
280 Returns the size of the hash which results from a specific algorithm.
281
282 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
283 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
284 @param[out] HashSize Holds the returned size of the algorithm's hash.
285
286 @retval EFI_SUCCESS Hash size returned successfully.
287 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
288 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
289 or HashAlgorithm is null.
290
291 **/
292 EFI_STATUS
293 EFIAPI
294 BaseCrypto2GetHashSize (
295 IN CONST EFI_HASH2_PROTOCOL *This,
296 IN CONST EFI_GUID *HashAlgorithm,
297 OUT UINTN *HashSize
298 )
299 {
300 EFI_HASH_INFO *HashInfo;
301
302 if ((This == NULL) || (HashSize == NULL)) {
303 return EFI_INVALID_PARAMETER;
304 }
305
306 if (HashAlgorithm == NULL) {
307 return EFI_UNSUPPORTED;
308 }
309
310 HashInfo = GetHashInfo (HashAlgorithm);
311 if (HashInfo == NULL) {
312 return EFI_UNSUPPORTED;
313 }
314
315 *HashSize = HashInfo->HashSize;
316 return EFI_SUCCESS;
317 }
318
319 /**
320 Creates a hash for the specified message text. The hash is not extendable.
321 The output is final with any algorithm-required padding added by the function.
322
323 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
324 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
325 @param[in] Message Points to the start of the message.
326 @param[in] MessageSize The size of Message, in bytes.
327 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
328 returned by GetHashSize() for the specified HashAlgorithm.
329 On output, the buffer holds the resulting hash computed from the message.
330
331 @retval EFI_SUCCESS Hash returned successfully.
332 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
333 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
334 or HashAlgorithm is Null.
335 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
336 or MessageSize is greater than platform maximum.
337
338 **/
339 EFI_STATUS
340 EFIAPI
341 BaseCrypto2Hash (
342 IN CONST EFI_HASH2_PROTOCOL *This,
343 IN CONST EFI_GUID *HashAlgorithm,
344 IN CONST UINT8 *Message,
345 IN UINTN MessageSize,
346 IN OUT EFI_HASH2_OUTPUT *Hash
347 )
348 {
349 EFI_HASH_INFO *HashInfo;
350 VOID *HashCtx;
351 UINTN CtxSize;
352 BOOLEAN Ret;
353 EFI_STATUS Status;
354 HASH2_INSTANCE_DATA *Instance;
355
356 Status = EFI_SUCCESS;
357
358 if ((This == NULL) || (Hash == NULL)) {
359 return EFI_INVALID_PARAMETER;
360 }
361
362 if (HashAlgorithm == NULL) {
363 return EFI_UNSUPPORTED;
364 }
365
366 HashInfo = GetHashInfo (HashAlgorithm);
367 if (HashInfo == NULL) {
368 return EFI_UNSUPPORTED;
369 }
370
371 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
372 if (Instance->HashContext != NULL) {
373 FreePool (Instance->HashContext);
374 }
375
376 Instance->HashInfoContext = NULL;
377 Instance->HashContext = NULL;
378
379 //
380 // Start hash sequence
381 //
382 CtxSize = HashInfo->GetContextSize ();
383 if (CtxSize == 0) {
384 return EFI_UNSUPPORTED;
385 }
386
387 HashCtx = AllocatePool (CtxSize);
388 if (HashCtx == NULL) {
389 return EFI_OUT_OF_RESOURCES;
390 }
391
392 Ret = HashInfo->Init (HashCtx);
393 if (!Ret) {
394 Status = EFI_OUT_OF_RESOURCES;
395 goto Done;
396 }
397
398 //
399 // Setup the context
400 //
401 Instance->HashContext = HashCtx;
402 Instance->HashInfoContext = HashInfo;
403
404 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
405 if (!Ret) {
406 Status = EFI_OUT_OF_RESOURCES;
407 goto Done;
408 }
409
410 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
411 if (!Ret) {
412 Status = EFI_OUT_OF_RESOURCES;
413 goto Done;
414 }
415
416 Done:
417 //
418 // Cleanup the context
419 //
420 FreePool (HashCtx);
421 Instance->HashInfoContext = NULL;
422 Instance->HashContext = NULL;
423 return Status;
424 }
425
426 /**
427 This function must be called to initialize a digest calculation to be subsequently performed using the
428 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
429
430 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
431 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
432
433 @retval EFI_SUCCESS Initialized successfully.
434 @retval EFI_INVALID_PARAMETER This is NULL.
435 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
436 or HashAlgorithm is Null.
437 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
438 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
439 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 BaseCrypto2HashInit (
445 IN CONST EFI_HASH2_PROTOCOL *This,
446 IN CONST EFI_GUID *HashAlgorithm
447 )
448 {
449 EFI_HASH_INFO *HashInfo;
450 VOID *HashCtx;
451 UINTN CtxSize;
452 BOOLEAN Ret;
453 HASH2_INSTANCE_DATA *Instance;
454
455 if (This == NULL) {
456 return EFI_INVALID_PARAMETER;
457 }
458
459 if (HashAlgorithm == NULL) {
460 return EFI_UNSUPPORTED;
461 }
462
463 HashInfo = GetHashInfo (HashAlgorithm);
464 if (HashInfo == NULL) {
465 return EFI_UNSUPPORTED;
466 }
467
468 //
469 // Consistency Check
470 //
471 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
472 if ((Instance->HashContext != NULL) || (Instance->HashInfoContext != NULL)) {
473 return EFI_ALREADY_STARTED;
474 }
475
476 //
477 // Start hash sequence
478 //
479 CtxSize = HashInfo->GetContextSize ();
480 if (CtxSize == 0) {
481 return EFI_UNSUPPORTED;
482 }
483
484 HashCtx = AllocatePool (CtxSize);
485 if (HashCtx == NULL) {
486 return EFI_OUT_OF_RESOURCES;
487 }
488
489 Ret = HashInfo->Init (HashCtx);
490 if (!Ret) {
491 FreePool (HashCtx);
492 return EFI_OUT_OF_RESOURCES;
493 }
494
495 //
496 // Setup the context
497 //
498 Instance->HashContext = HashCtx;
499 Instance->HashInfoContext = HashInfo;
500 Instance->Updated = FALSE;
501
502 return EFI_SUCCESS;
503 }
504
505 /**
506 Updates the hash of a computation in progress by adding a message text.
507
508 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
509 @param[in] Message Points to the start of the message.
510 @param[in] MessageSize The size of Message, in bytes.
511
512 @retval EFI_SUCCESS Digest in progress updated successfully.
513 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
514 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
515 or MessageSize is greater than platform maximum.
516 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
517 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
518
519 **/
520 EFI_STATUS
521 EFIAPI
522 BaseCrypto2HashUpdate (
523 IN CONST EFI_HASH2_PROTOCOL *This,
524 IN CONST UINT8 *Message,
525 IN UINTN MessageSize
526 )
527 {
528 EFI_HASH_INFO *HashInfo;
529 VOID *HashCtx;
530 BOOLEAN Ret;
531 HASH2_INSTANCE_DATA *Instance;
532
533 if (This == NULL) {
534 return EFI_INVALID_PARAMETER;
535 }
536
537 //
538 // Consistency Check
539 //
540 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
541 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL)) {
542 return EFI_NOT_READY;
543 }
544
545 HashInfo = Instance->HashInfoContext;
546 HashCtx = Instance->HashContext;
547
548 Ret = HashInfo->Update (HashCtx, Message, MessageSize);
549 if (!Ret) {
550 return EFI_OUT_OF_RESOURCES;
551 }
552
553 Instance->Updated = TRUE;
554
555 return EFI_SUCCESS;
556 }
557
558 /**
559 Finalizes a hash operation in progress and returns calculation result.
560 The output is final with any necessary padding added by the function.
561 The hash may not be further updated or extended after HashFinal().
562
563 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
564 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
565 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
566 On output, the buffer holds the resulting hash computed from the message.
567
568 @retval EFI_SUCCESS Hash returned successfully.
569 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
570 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
571 or the operation in progress was canceled by a call to Hash() on the same instance.
572
573 **/
574 EFI_STATUS
575 EFIAPI
576 BaseCrypto2HashFinal (
577 IN CONST EFI_HASH2_PROTOCOL *This,
578 IN OUT EFI_HASH2_OUTPUT *Hash
579 )
580 {
581 EFI_HASH_INFO *HashInfo;
582 VOID *HashCtx;
583 BOOLEAN Ret;
584 HASH2_INSTANCE_DATA *Instance;
585
586 if ((This == NULL) || (Hash == NULL)) {
587 return EFI_INVALID_PARAMETER;
588 }
589
590 //
591 // Consistency Check
592 //
593 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);
594 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL) ||
595 (!Instance->Updated))
596 {
597 return EFI_NOT_READY;
598 }
599
600 HashInfo = Instance->HashInfoContext;
601 HashCtx = Instance->HashContext;
602
603 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);
604
605 //
606 // Cleanup the context
607 //
608 FreePool (HashCtx);
609 Instance->HashInfoContext = NULL;
610 Instance->HashContext = NULL;
611 Instance->Updated = FALSE;
612
613 if (!Ret) {
614 return EFI_OUT_OF_RESOURCES;
615 }
616
617 return EFI_SUCCESS;
618 }