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