]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg: Add new hash algorithm ParallelHash256HashAll in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Include / Library / BaseCryptLib.h
1 /** @file
2 Defines base cryptographic library APIs.
3 The Base Cryptographic Library provides implementations of basic cryptography
4 primitives (Hash Serials, HMAC, RSA, Diffie-Hellman, etc) for UEFI security
5 functionality enabling.
6
7 Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #ifndef __BASE_CRYPT_LIB_H__
13 #define __BASE_CRYPT_LIB_H__
14
15 #include <Uefi/UefiBaseType.h>
16
17 ///
18 /// MD5 digest size in bytes
19 ///
20 #define MD5_DIGEST_SIZE 16
21
22 ///
23 /// SHA-1 digest size in bytes.
24 ///
25 #define SHA1_DIGEST_SIZE 20
26
27 ///
28 /// SHA-256 digest size in bytes
29 ///
30 #define SHA256_DIGEST_SIZE 32
31
32 ///
33 /// SHA-384 digest size in bytes
34 ///
35 #define SHA384_DIGEST_SIZE 48
36
37 ///
38 /// SHA-512 digest size in bytes
39 ///
40 #define SHA512_DIGEST_SIZE 64
41
42 ///
43 /// SM3 digest size in bytes
44 ///
45 #define SM3_256_DIGEST_SIZE 32
46
47 ///
48 /// TDES block size in bytes
49 ///
50 #define TDES_BLOCK_SIZE 8
51
52 ///
53 /// AES block size in bytes
54 ///
55 #define AES_BLOCK_SIZE 16
56
57 ///
58 /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
59 ///
60 typedef enum {
61 RsaKeyN, ///< RSA public Modulus (N)
62 RsaKeyE, ///< RSA Public exponent (e)
63 RsaKeyD, ///< RSA Private exponent (d)
64 RsaKeyP, ///< RSA secret prime factor of Modulus (p)
65 RsaKeyQ, ///< RSA secret prime factor of Modules (q)
66 RsaKeyDp, ///< p's CRT exponent (== d mod (p - 1))
67 RsaKeyDq, ///< q's CRT exponent (== d mod (q - 1))
68 RsaKeyQInv ///< The CRT coefficient (== 1/q mod p)
69 } RSA_KEY_TAG;
70
71 // =====================================================================================
72 // One-Way Cryptographic Hash Primitives
73 // =====================================================================================
74
75 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES
76
77 /**
78 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
79
80 If this interface is not supported, then return zero.
81
82 @return The size, in bytes, of the context buffer required for MD5 hash operations.
83 @retval 0 This interface is not supported.
84
85 **/
86 UINTN
87 EFIAPI
88 Md5GetContextSize (
89 VOID
90 );
91
92 /**
93 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
94 subsequent use.
95
96 If Md5Context is NULL, then return FALSE.
97 If this interface is not supported, then return FALSE.
98
99 @param[out] Md5Context Pointer to MD5 context being initialized.
100
101 @retval TRUE MD5 context initialization succeeded.
102 @retval FALSE MD5 context initialization failed.
103 @retval FALSE This interface is not supported.
104
105 **/
106 BOOLEAN
107 EFIAPI
108 Md5Init (
109 OUT VOID *Md5Context
110 );
111
112 /**
113 Makes a copy of an existing MD5 context.
114
115 If Md5Context is NULL, then return FALSE.
116 If NewMd5Context is NULL, then return FALSE.
117 If this interface is not supported, then return FALSE.
118
119 @param[in] Md5Context Pointer to MD5 context being copied.
120 @param[out] NewMd5Context Pointer to new MD5 context.
121
122 @retval TRUE MD5 context copy succeeded.
123 @retval FALSE MD5 context copy failed.
124 @retval FALSE This interface is not supported.
125
126 **/
127 BOOLEAN
128 EFIAPI
129 Md5Duplicate (
130 IN CONST VOID *Md5Context,
131 OUT VOID *NewMd5Context
132 );
133
134 /**
135 Digests the input data and updates MD5 context.
136
137 This function performs MD5 digest on a data buffer of the specified size.
138 It can be called multiple times to compute the digest of long or discontinuous data streams.
139 MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
140 by Md5Final(). Behavior with invalid context is undefined.
141
142 If Md5Context is NULL, then return FALSE.
143 If this interface is not supported, then return FALSE.
144
145 @param[in, out] Md5Context Pointer to the MD5 context.
146 @param[in] Data Pointer to the buffer containing the data to be hashed.
147 @param[in] DataSize Size of Data buffer in bytes.
148
149 @retval TRUE MD5 data digest succeeded.
150 @retval FALSE MD5 data digest failed.
151 @retval FALSE This interface is not supported.
152
153 **/
154 BOOLEAN
155 EFIAPI
156 Md5Update (
157 IN OUT VOID *Md5Context,
158 IN CONST VOID *Data,
159 IN UINTN DataSize
160 );
161
162 /**
163 Completes computation of the MD5 digest value.
164
165 This function completes MD5 hash computation and retrieves the digest value into
166 the specified memory. After this function has been called, the MD5 context cannot
167 be used again.
168 MD5 context should be already correctly initialized by Md5Init(), and should not be
169 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
170
171 If Md5Context is NULL, then return FALSE.
172 If HashValue is NULL, then return FALSE.
173 If this interface is not supported, then return FALSE.
174
175 @param[in, out] Md5Context Pointer to the MD5 context.
176 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
177 value (16 bytes).
178
179 @retval TRUE MD5 digest computation succeeded.
180 @retval FALSE MD5 digest computation failed.
181 @retval FALSE This interface is not supported.
182
183 **/
184 BOOLEAN
185 EFIAPI
186 Md5Final (
187 IN OUT VOID *Md5Context,
188 OUT UINT8 *HashValue
189 );
190
191 /**
192 Computes the MD5 message digest of a input data buffer.
193
194 This function performs the MD5 message digest of a given data buffer, and places
195 the digest value into the specified memory.
196
197 If this interface is not supported, then return FALSE.
198
199 @param[in] Data Pointer to the buffer containing the data to be hashed.
200 @param[in] DataSize Size of Data buffer in bytes.
201 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
202 value (16 bytes).
203
204 @retval TRUE MD5 digest computation succeeded.
205 @retval FALSE MD5 digest computation failed.
206 @retval FALSE This interface is not supported.
207
208 **/
209 BOOLEAN
210 EFIAPI
211 Md5HashAll (
212 IN CONST VOID *Data,
213 IN UINTN DataSize,
214 OUT UINT8 *HashValue
215 );
216
217 #endif
218
219 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
220
221 /**
222 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
223
224 If this interface is not supported, then return zero.
225
226 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
227 @retval 0 This interface is not supported.
228
229 **/
230 UINTN
231 EFIAPI
232 Sha1GetContextSize (
233 VOID
234 );
235
236 /**
237 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
238 subsequent use.
239
240 If Sha1Context is NULL, then return FALSE.
241 If this interface is not supported, then return FALSE.
242
243 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
244
245 @retval TRUE SHA-1 context initialization succeeded.
246 @retval FALSE SHA-1 context initialization failed.
247 @retval FALSE This interface is not supported.
248
249 **/
250 BOOLEAN
251 EFIAPI
252 Sha1Init (
253 OUT VOID *Sha1Context
254 );
255
256 /**
257 Makes a copy of an existing SHA-1 context.
258
259 If Sha1Context is NULL, then return FALSE.
260 If NewSha1Context is NULL, then return FALSE.
261 If this interface is not supported, then return FALSE.
262
263 @param[in] Sha1Context Pointer to SHA-1 context being copied.
264 @param[out] NewSha1Context Pointer to new SHA-1 context.
265
266 @retval TRUE SHA-1 context copy succeeded.
267 @retval FALSE SHA-1 context copy failed.
268 @retval FALSE This interface is not supported.
269
270 **/
271 BOOLEAN
272 EFIAPI
273 Sha1Duplicate (
274 IN CONST VOID *Sha1Context,
275 OUT VOID *NewSha1Context
276 );
277
278 /**
279 Digests the input data and updates SHA-1 context.
280
281 This function performs SHA-1 digest on a data buffer of the specified size.
282 It can be called multiple times to compute the digest of long or discontinuous data streams.
283 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
284 by Sha1Final(). Behavior with invalid context is undefined.
285
286 If Sha1Context is NULL, then return FALSE.
287 If this interface is not supported, then return FALSE.
288
289 @param[in, out] Sha1Context Pointer to the SHA-1 context.
290 @param[in] Data Pointer to the buffer containing the data to be hashed.
291 @param[in] DataSize Size of Data buffer in bytes.
292
293 @retval TRUE SHA-1 data digest succeeded.
294 @retval FALSE SHA-1 data digest failed.
295 @retval FALSE This interface is not supported.
296
297 **/
298 BOOLEAN
299 EFIAPI
300 Sha1Update (
301 IN OUT VOID *Sha1Context,
302 IN CONST VOID *Data,
303 IN UINTN DataSize
304 );
305
306 /**
307 Completes computation of the SHA-1 digest value.
308
309 This function completes SHA-1 hash computation and retrieves the digest value into
310 the specified memory. After this function has been called, the SHA-1 context cannot
311 be used again.
312 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
313 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
314
315 If Sha1Context is NULL, then return FALSE.
316 If HashValue is NULL, then return FALSE.
317 If this interface is not supported, then return FALSE.
318
319 @param[in, out] Sha1Context Pointer to the SHA-1 context.
320 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
321 value (20 bytes).
322
323 @retval TRUE SHA-1 digest computation succeeded.
324 @retval FALSE SHA-1 digest computation failed.
325 @retval FALSE This interface is not supported.
326
327 **/
328 BOOLEAN
329 EFIAPI
330 Sha1Final (
331 IN OUT VOID *Sha1Context,
332 OUT UINT8 *HashValue
333 );
334
335 /**
336 Computes the SHA-1 message digest of a input data buffer.
337
338 This function performs the SHA-1 message digest of a given data buffer, and places
339 the digest value into the specified memory.
340
341 If this interface is not supported, then return FALSE.
342
343 @param[in] Data Pointer to the buffer containing the data to be hashed.
344 @param[in] DataSize Size of Data buffer in bytes.
345 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
346 value (20 bytes).
347
348 @retval TRUE SHA-1 digest computation succeeded.
349 @retval FALSE SHA-1 digest computation failed.
350 @retval FALSE This interface is not supported.
351
352 **/
353 BOOLEAN
354 EFIAPI
355 Sha1HashAll (
356 IN CONST VOID *Data,
357 IN UINTN DataSize,
358 OUT UINT8 *HashValue
359 );
360
361 #endif
362
363 /**
364 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
365
366 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
367
368 **/
369 UINTN
370 EFIAPI
371 Sha256GetContextSize (
372 VOID
373 );
374
375 /**
376 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
377 subsequent use.
378
379 If Sha256Context is NULL, then return FALSE.
380
381 @param[out] Sha256Context Pointer to SHA-256 context being initialized.
382
383 @retval TRUE SHA-256 context initialization succeeded.
384 @retval FALSE SHA-256 context initialization failed.
385
386 **/
387 BOOLEAN
388 EFIAPI
389 Sha256Init (
390 OUT VOID *Sha256Context
391 );
392
393 /**
394 Makes a copy of an existing SHA-256 context.
395
396 If Sha256Context is NULL, then return FALSE.
397 If NewSha256Context is NULL, then return FALSE.
398 If this interface is not supported, then return FALSE.
399
400 @param[in] Sha256Context Pointer to SHA-256 context being copied.
401 @param[out] NewSha256Context Pointer to new SHA-256 context.
402
403 @retval TRUE SHA-256 context copy succeeded.
404 @retval FALSE SHA-256 context copy failed.
405 @retval FALSE This interface is not supported.
406
407 **/
408 BOOLEAN
409 EFIAPI
410 Sha256Duplicate (
411 IN CONST VOID *Sha256Context,
412 OUT VOID *NewSha256Context
413 );
414
415 /**
416 Digests the input data and updates SHA-256 context.
417
418 This function performs SHA-256 digest on a data buffer of the specified size.
419 It can be called multiple times to compute the digest of long or discontinuous data streams.
420 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
421 by Sha256Final(). Behavior with invalid context is undefined.
422
423 If Sha256Context is NULL, then return FALSE.
424
425 @param[in, out] Sha256Context Pointer to the SHA-256 context.
426 @param[in] Data Pointer to the buffer containing the data to be hashed.
427 @param[in] DataSize Size of Data buffer in bytes.
428
429 @retval TRUE SHA-256 data digest succeeded.
430 @retval FALSE SHA-256 data digest failed.
431
432 **/
433 BOOLEAN
434 EFIAPI
435 Sha256Update (
436 IN OUT VOID *Sha256Context,
437 IN CONST VOID *Data,
438 IN UINTN DataSize
439 );
440
441 /**
442 Completes computation of the SHA-256 digest value.
443
444 This function completes SHA-256 hash computation and retrieves the digest value into
445 the specified memory. After this function has been called, the SHA-256 context cannot
446 be used again.
447 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
448 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
449
450 If Sha256Context is NULL, then return FALSE.
451 If HashValue is NULL, then return FALSE.
452
453 @param[in, out] Sha256Context Pointer to the SHA-256 context.
454 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
455 value (32 bytes).
456
457 @retval TRUE SHA-256 digest computation succeeded.
458 @retval FALSE SHA-256 digest computation failed.
459
460 **/
461 BOOLEAN
462 EFIAPI
463 Sha256Final (
464 IN OUT VOID *Sha256Context,
465 OUT UINT8 *HashValue
466 );
467
468 /**
469 Computes the SHA-256 message digest of a input data buffer.
470
471 This function performs the SHA-256 message digest of a given data buffer, and places
472 the digest value into the specified memory.
473
474 If this interface is not supported, then return FALSE.
475
476 @param[in] Data Pointer to the buffer containing the data to be hashed.
477 @param[in] DataSize Size of Data buffer in bytes.
478 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
479 value (32 bytes).
480
481 @retval TRUE SHA-256 digest computation succeeded.
482 @retval FALSE SHA-256 digest computation failed.
483 @retval FALSE This interface is not supported.
484
485 **/
486 BOOLEAN
487 EFIAPI
488 Sha256HashAll (
489 IN CONST VOID *Data,
490 IN UINTN DataSize,
491 OUT UINT8 *HashValue
492 );
493
494 /**
495 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
496
497 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.
498
499 **/
500 UINTN
501 EFIAPI
502 Sha384GetContextSize (
503 VOID
504 );
505
506 /**
507 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
508 subsequent use.
509
510 If Sha384Context is NULL, then return FALSE.
511
512 @param[out] Sha384Context Pointer to SHA-384 context being initialized.
513
514 @retval TRUE SHA-384 context initialization succeeded.
515 @retval FALSE SHA-384 context initialization failed.
516
517 **/
518 BOOLEAN
519 EFIAPI
520 Sha384Init (
521 OUT VOID *Sha384Context
522 );
523
524 /**
525 Makes a copy of an existing SHA-384 context.
526
527 If Sha384Context is NULL, then return FALSE.
528 If NewSha384Context is NULL, then return FALSE.
529 If this interface is not supported, then return FALSE.
530
531 @param[in] Sha384Context Pointer to SHA-384 context being copied.
532 @param[out] NewSha384Context Pointer to new SHA-384 context.
533
534 @retval TRUE SHA-384 context copy succeeded.
535 @retval FALSE SHA-384 context copy failed.
536 @retval FALSE This interface is not supported.
537
538 **/
539 BOOLEAN
540 EFIAPI
541 Sha384Duplicate (
542 IN CONST VOID *Sha384Context,
543 OUT VOID *NewSha384Context
544 );
545
546 /**
547 Digests the input data and updates SHA-384 context.
548
549 This function performs SHA-384 digest on a data buffer of the specified size.
550 It can be called multiple times to compute the digest of long or discontinuous data streams.
551 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized
552 by Sha384Final(). Behavior with invalid context is undefined.
553
554 If Sha384Context is NULL, then return FALSE.
555
556 @param[in, out] Sha384Context Pointer to the SHA-384 context.
557 @param[in] Data Pointer to the buffer containing the data to be hashed.
558 @param[in] DataSize Size of Data buffer in bytes.
559
560 @retval TRUE SHA-384 data digest succeeded.
561 @retval FALSE SHA-384 data digest failed.
562
563 **/
564 BOOLEAN
565 EFIAPI
566 Sha384Update (
567 IN OUT VOID *Sha384Context,
568 IN CONST VOID *Data,
569 IN UINTN DataSize
570 );
571
572 /**
573 Completes computation of the SHA-384 digest value.
574
575 This function completes SHA-384 hash computation and retrieves the digest value into
576 the specified memory. After this function has been called, the SHA-384 context cannot
577 be used again.
578 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be
579 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
580
581 If Sha384Context is NULL, then return FALSE.
582 If HashValue is NULL, then return FALSE.
583
584 @param[in, out] Sha384Context Pointer to the SHA-384 context.
585 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
586 value (48 bytes).
587
588 @retval TRUE SHA-384 digest computation succeeded.
589 @retval FALSE SHA-384 digest computation failed.
590
591 **/
592 BOOLEAN
593 EFIAPI
594 Sha384Final (
595 IN OUT VOID *Sha384Context,
596 OUT UINT8 *HashValue
597 );
598
599 /**
600 Computes the SHA-384 message digest of a input data buffer.
601
602 This function performs the SHA-384 message digest of a given data buffer, and places
603 the digest value into the specified memory.
604
605 If this interface is not supported, then return FALSE.
606
607 @param[in] Data Pointer to the buffer containing the data to be hashed.
608 @param[in] DataSize Size of Data buffer in bytes.
609 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
610 value (48 bytes).
611
612 @retval TRUE SHA-384 digest computation succeeded.
613 @retval FALSE SHA-384 digest computation failed.
614 @retval FALSE This interface is not supported.
615
616 **/
617 BOOLEAN
618 EFIAPI
619 Sha384HashAll (
620 IN CONST VOID *Data,
621 IN UINTN DataSize,
622 OUT UINT8 *HashValue
623 );
624
625 /**
626 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
627
628 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.
629
630 **/
631 UINTN
632 EFIAPI
633 Sha512GetContextSize (
634 VOID
635 );
636
637 /**
638 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
639 subsequent use.
640
641 If Sha512Context is NULL, then return FALSE.
642
643 @param[out] Sha512Context Pointer to SHA-512 context being initialized.
644
645 @retval TRUE SHA-512 context initialization succeeded.
646 @retval FALSE SHA-512 context initialization failed.
647
648 **/
649 BOOLEAN
650 EFIAPI
651 Sha512Init (
652 OUT VOID *Sha512Context
653 );
654
655 /**
656 Makes a copy of an existing SHA-512 context.
657
658 If Sha512Context is NULL, then return FALSE.
659 If NewSha512Context is NULL, then return FALSE.
660 If this interface is not supported, then return FALSE.
661
662 @param[in] Sha512Context Pointer to SHA-512 context being copied.
663 @param[out] NewSha512Context Pointer to new SHA-512 context.
664
665 @retval TRUE SHA-512 context copy succeeded.
666 @retval FALSE SHA-512 context copy failed.
667 @retval FALSE This interface is not supported.
668
669 **/
670 BOOLEAN
671 EFIAPI
672 Sha512Duplicate (
673 IN CONST VOID *Sha512Context,
674 OUT VOID *NewSha512Context
675 );
676
677 /**
678 Digests the input data and updates SHA-512 context.
679
680 This function performs SHA-512 digest on a data buffer of the specified size.
681 It can be called multiple times to compute the digest of long or discontinuous data streams.
682 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized
683 by Sha512Final(). Behavior with invalid context is undefined.
684
685 If Sha512Context is NULL, then return FALSE.
686
687 @param[in, out] Sha512Context Pointer to the SHA-512 context.
688 @param[in] Data Pointer to the buffer containing the data to be hashed.
689 @param[in] DataSize Size of Data buffer in bytes.
690
691 @retval TRUE SHA-512 data digest succeeded.
692 @retval FALSE SHA-512 data digest failed.
693
694 **/
695 BOOLEAN
696 EFIAPI
697 Sha512Update (
698 IN OUT VOID *Sha512Context,
699 IN CONST VOID *Data,
700 IN UINTN DataSize
701 );
702
703 /**
704 Completes computation of the SHA-512 digest value.
705
706 This function completes SHA-512 hash computation and retrieves the digest value into
707 the specified memory. After this function has been called, the SHA-512 context cannot
708 be used again.
709 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be
710 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
711
712 If Sha512Context is NULL, then return FALSE.
713 If HashValue is NULL, then return FALSE.
714
715 @param[in, out] Sha512Context Pointer to the SHA-512 context.
716 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
717 value (64 bytes).
718
719 @retval TRUE SHA-512 digest computation succeeded.
720 @retval FALSE SHA-512 digest computation failed.
721
722 **/
723 BOOLEAN
724 EFIAPI
725 Sha512Final (
726 IN OUT VOID *Sha512Context,
727 OUT UINT8 *HashValue
728 );
729
730 /**
731 Computes the SHA-512 message digest of a input data buffer.
732
733 This function performs the SHA-512 message digest of a given data buffer, and places
734 the digest value into the specified memory.
735
736 If this interface is not supported, then return FALSE.
737
738 @param[in] Data Pointer to the buffer containing the data to be hashed.
739 @param[in] DataSize Size of Data buffer in bytes.
740 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
741 value (64 bytes).
742
743 @retval TRUE SHA-512 digest computation succeeded.
744 @retval FALSE SHA-512 digest computation failed.
745 @retval FALSE This interface is not supported.
746
747 **/
748 BOOLEAN
749 EFIAPI
750 Sha512HashAll (
751 IN CONST VOID *Data,
752 IN UINTN DataSize,
753 OUT UINT8 *HashValue
754 );
755
756 /**
757 Parallel hash function ParallelHash256, as defined in NIST's Special Publication 800-185,
758 published December 2016.
759
760 @param[in] Input Pointer to the input message (X).
761 @param[in] InputByteLen The number(>0) of input bytes provided for the input data.
762 @param[in] BlockSize The size of each block (B).
763 @param[out] Output Pointer to the output buffer.
764 @param[in] OutputByteLen The desired number of output bytes (L).
765 @param[in] Customization Pointer to the customization string (S).
766 @param[in] CustomByteLen The length of the customization string in bytes.
767
768 @retval TRUE ParallelHash256 digest computation succeeded.
769 @retval FALSE ParallelHash256 digest computation failed.
770 @retval FALSE This interface is not supported.
771
772 **/
773 BOOLEAN
774 EFIAPI
775 ParallelHash256HashAll (
776 IN CONST VOID *Input,
777 IN UINTN InputByteLen,
778 IN UINTN BlockSize,
779 OUT VOID *Output,
780 IN UINTN OutputByteLen,
781 IN CONST VOID *Customization,
782 IN UINTN CustomByteLen
783 );
784
785 /**
786 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
787
788 @return The size, in bytes, of the context buffer required for SM3 hash operations.
789
790 **/
791 UINTN
792 EFIAPI
793 Sm3GetContextSize (
794 VOID
795 );
796
797 /**
798 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
799 subsequent use.
800
801 If Sm3Context is NULL, then return FALSE.
802
803 @param[out] Sm3Context Pointer to SM3 context being initialized.
804
805 @retval TRUE SM3 context initialization succeeded.
806 @retval FALSE SM3 context initialization failed.
807
808 **/
809 BOOLEAN
810 EFIAPI
811 Sm3Init (
812 OUT VOID *Sm3Context
813 );
814
815 /**
816 Makes a copy of an existing SM3 context.
817
818 If Sm3Context is NULL, then return FALSE.
819 If NewSm3Context is NULL, then return FALSE.
820 If this interface is not supported, then return FALSE.
821
822 @param[in] Sm3Context Pointer to SM3 context being copied.
823 @param[out] NewSm3Context Pointer to new SM3 context.
824
825 @retval TRUE SM3 context copy succeeded.
826 @retval FALSE SM3 context copy failed.
827 @retval FALSE This interface is not supported.
828
829 **/
830 BOOLEAN
831 EFIAPI
832 Sm3Duplicate (
833 IN CONST VOID *Sm3Context,
834 OUT VOID *NewSm3Context
835 );
836
837 /**
838 Digests the input data and updates SM3 context.
839
840 This function performs SM3 digest on a data buffer of the specified size.
841 It can be called multiple times to compute the digest of long or discontinuous data streams.
842 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
843 by Sm3Final(). Behavior with invalid context is undefined.
844
845 If Sm3Context is NULL, then return FALSE.
846
847 @param[in, out] Sm3Context Pointer to the SM3 context.
848 @param[in] Data Pointer to the buffer containing the data to be hashed.
849 @param[in] DataSize Size of Data buffer in bytes.
850
851 @retval TRUE SM3 data digest succeeded.
852 @retval FALSE SM3 data digest failed.
853
854 **/
855 BOOLEAN
856 EFIAPI
857 Sm3Update (
858 IN OUT VOID *Sm3Context,
859 IN CONST VOID *Data,
860 IN UINTN DataSize
861 );
862
863 /**
864 Completes computation of the SM3 digest value.
865
866 This function completes SM3 hash computation and retrieves the digest value into
867 the specified memory. After this function has been called, the SM3 context cannot
868 be used again.
869 SM3 context should be already correctly initialized by Sm3Init(), and should not be
870 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
871
872 If Sm3Context is NULL, then return FALSE.
873 If HashValue is NULL, then return FALSE.
874
875 @param[in, out] Sm3Context Pointer to the SM3 context.
876 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
877 value (32 bytes).
878
879 @retval TRUE SM3 digest computation succeeded.
880 @retval FALSE SM3 digest computation failed.
881
882 **/
883 BOOLEAN
884 EFIAPI
885 Sm3Final (
886 IN OUT VOID *Sm3Context,
887 OUT UINT8 *HashValue
888 );
889
890 /**
891 Computes the SM3 message digest of a input data buffer.
892
893 This function performs the SM3 message digest of a given data buffer, and places
894 the digest value into the specified memory.
895
896 If this interface is not supported, then return FALSE.
897
898 @param[in] Data Pointer to the buffer containing the data to be hashed.
899 @param[in] DataSize Size of Data buffer in bytes.
900 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
901 value (32 bytes).
902
903 @retval TRUE SM3 digest computation succeeded.
904 @retval FALSE SM3 digest computation failed.
905 @retval FALSE This interface is not supported.
906
907 **/
908 BOOLEAN
909 EFIAPI
910 Sm3HashAll (
911 IN CONST VOID *Data,
912 IN UINTN DataSize,
913 OUT UINT8 *HashValue
914 );
915
916 // =====================================================================================
917 // MAC (Message Authentication Code) Primitive
918 // =====================================================================================
919
920 /**
921 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
922
923 @return Pointer to the HMAC_CTX context that has been initialized.
924 If the allocations fails, HmacSha256New() returns NULL.
925
926 **/
927 VOID *
928 EFIAPI
929 HmacSha256New (
930 VOID
931 );
932
933 /**
934 Release the specified HMAC_CTX context.
935
936 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
937
938 **/
939 VOID
940 EFIAPI
941 HmacSha256Free (
942 IN VOID *HmacSha256Ctx
943 );
944
945 /**
946 Set user-supplied key for subsequent use. It must be done before any
947 calling to HmacSha256Update().
948
949 If HmacSha256Context is NULL, then return FALSE.
950 If this interface is not supported, then return FALSE.
951
952 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
953 @param[in] Key Pointer to the user-supplied key.
954 @param[in] KeySize Key size in bytes.
955
956 @retval TRUE The Key is set successfully.
957 @retval FALSE The Key is set unsuccessfully.
958 @retval FALSE This interface is not supported.
959
960 **/
961 BOOLEAN
962 EFIAPI
963 HmacSha256SetKey (
964 OUT VOID *HmacSha256Context,
965 IN CONST UINT8 *Key,
966 IN UINTN KeySize
967 );
968
969 /**
970 Makes a copy of an existing HMAC-SHA256 context.
971
972 If HmacSha256Context is NULL, then return FALSE.
973 If NewHmacSha256Context is NULL, then return FALSE.
974 If this interface is not supported, then return FALSE.
975
976 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
977 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
978
979 @retval TRUE HMAC-SHA256 context copy succeeded.
980 @retval FALSE HMAC-SHA256 context copy failed.
981 @retval FALSE This interface is not supported.
982
983 **/
984 BOOLEAN
985 EFIAPI
986 HmacSha256Duplicate (
987 IN CONST VOID *HmacSha256Context,
988 OUT VOID *NewHmacSha256Context
989 );
990
991 /**
992 Digests the input data and updates HMAC-SHA256 context.
993
994 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
995 It can be called multiple times to compute the digest of long or discontinuous data streams.
996 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
997 by HmacSha256Final(). Behavior with invalid context is undefined.
998
999 If HmacSha256Context is NULL, then return FALSE.
1000 If this interface is not supported, then return FALSE.
1001
1002 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1003 @param[in] Data Pointer to the buffer containing the data to be digested.
1004 @param[in] DataSize Size of Data buffer in bytes.
1005
1006 @retval TRUE HMAC-SHA256 data digest succeeded.
1007 @retval FALSE HMAC-SHA256 data digest failed.
1008 @retval FALSE This interface is not supported.
1009
1010 **/
1011 BOOLEAN
1012 EFIAPI
1013 HmacSha256Update (
1014 IN OUT VOID *HmacSha256Context,
1015 IN CONST VOID *Data,
1016 IN UINTN DataSize
1017 );
1018
1019 /**
1020 Completes computation of the HMAC-SHA256 digest value.
1021
1022 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
1023 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
1024 be used again.
1025 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
1026 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
1027
1028 If HmacSha256Context is NULL, then return FALSE.
1029 If HmacValue is NULL, then return FALSE.
1030 If this interface is not supported, then return FALSE.
1031
1032 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1033 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
1034 value (32 bytes).
1035
1036 @retval TRUE HMAC-SHA256 digest computation succeeded.
1037 @retval FALSE HMAC-SHA256 digest computation failed.
1038 @retval FALSE This interface is not supported.
1039
1040 **/
1041 BOOLEAN
1042 EFIAPI
1043 HmacSha256Final (
1044 IN OUT VOID *HmacSha256Context,
1045 OUT UINT8 *HmacValue
1046 );
1047
1048 // =====================================================================================
1049 // Symmetric Cryptography Primitive
1050 // =====================================================================================
1051
1052 /**
1053 Retrieves the size, in bytes, of the context buffer required for AES operations.
1054
1055 If this interface is not supported, then return zero.
1056
1057 @return The size, in bytes, of the context buffer required for AES operations.
1058 @retval 0 This interface is not supported.
1059
1060 **/
1061 UINTN
1062 EFIAPI
1063 AesGetContextSize (
1064 VOID
1065 );
1066
1067 /**
1068 Initializes user-supplied memory as AES context for subsequent use.
1069
1070 This function initializes user-supplied memory pointed by AesContext as AES context.
1071 In addition, it sets up all AES key materials for subsequent encryption and decryption
1072 operations.
1073 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1074
1075 If AesContext is NULL, then return FALSE.
1076 If Key is NULL, then return FALSE.
1077 If KeyLength is not valid, then return FALSE.
1078 If this interface is not supported, then return FALSE.
1079
1080 @param[out] AesContext Pointer to AES context being initialized.
1081 @param[in] Key Pointer to the user-supplied AES key.
1082 @param[in] KeyLength Length of AES key in bits.
1083
1084 @retval TRUE AES context initialization succeeded.
1085 @retval FALSE AES context initialization failed.
1086 @retval FALSE This interface is not supported.
1087
1088 **/
1089 BOOLEAN
1090 EFIAPI
1091 AesInit (
1092 OUT VOID *AesContext,
1093 IN CONST UINT8 *Key,
1094 IN UINTN KeyLength
1095 );
1096
1097 /**
1098 Performs AES encryption on a data buffer of the specified size in CBC mode.
1099
1100 This function performs AES encryption on data buffer pointed by Input, of specified
1101 size of InputSize, in CBC mode.
1102 InputSize must be multiple of block size (16 bytes). This function does not perform
1103 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1104 Initialization vector should be one block size (16 bytes).
1105 AesContext should be already correctly initialized by AesInit(). Behavior with
1106 invalid AES context is undefined.
1107
1108 If AesContext is NULL, then return FALSE.
1109 If Input is NULL, then return FALSE.
1110 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1111 If Ivec is NULL, then return FALSE.
1112 If Output is NULL, then return FALSE.
1113 If this interface is not supported, then return FALSE.
1114
1115 @param[in] AesContext Pointer to the AES context.
1116 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1117 @param[in] InputSize Size of the Input buffer in bytes.
1118 @param[in] Ivec Pointer to initialization vector.
1119 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1120
1121 @retval TRUE AES encryption succeeded.
1122 @retval FALSE AES encryption failed.
1123 @retval FALSE This interface is not supported.
1124
1125 **/
1126 BOOLEAN
1127 EFIAPI
1128 AesCbcEncrypt (
1129 IN VOID *AesContext,
1130 IN CONST UINT8 *Input,
1131 IN UINTN InputSize,
1132 IN CONST UINT8 *Ivec,
1133 OUT UINT8 *Output
1134 );
1135
1136 /**
1137 Performs AES decryption on a data buffer of the specified size in CBC mode.
1138
1139 This function performs AES decryption on data buffer pointed by Input, of specified
1140 size of InputSize, in CBC mode.
1141 InputSize must be multiple of block size (16 bytes). This function does not perform
1142 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1143 Initialization vector should be one block size (16 bytes).
1144 AesContext should be already correctly initialized by AesInit(). Behavior with
1145 invalid AES context is undefined.
1146
1147 If AesContext is NULL, then return FALSE.
1148 If Input is NULL, then return FALSE.
1149 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1150 If Ivec is NULL, then return FALSE.
1151 If Output is NULL, then return FALSE.
1152 If this interface is not supported, then return FALSE.
1153
1154 @param[in] AesContext Pointer to the AES context.
1155 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1156 @param[in] InputSize Size of the Input buffer in bytes.
1157 @param[in] Ivec Pointer to initialization vector.
1158 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1159
1160 @retval TRUE AES decryption succeeded.
1161 @retval FALSE AES decryption failed.
1162 @retval FALSE This interface is not supported.
1163
1164 **/
1165 BOOLEAN
1166 EFIAPI
1167 AesCbcDecrypt (
1168 IN VOID *AesContext,
1169 IN CONST UINT8 *Input,
1170 IN UINTN InputSize,
1171 IN CONST UINT8 *Ivec,
1172 OUT UINT8 *Output
1173 );
1174
1175 // =====================================================================================
1176 // Asymmetric Cryptography Primitive
1177 // =====================================================================================
1178
1179 /**
1180 Allocates and initializes one RSA context for subsequent use.
1181
1182 @return Pointer to the RSA context that has been initialized.
1183 If the allocations fails, RsaNew() returns NULL.
1184
1185 **/
1186 VOID *
1187 EFIAPI
1188 RsaNew (
1189 VOID
1190 );
1191
1192 /**
1193 Release the specified RSA context.
1194
1195 If RsaContext is NULL, then return FALSE.
1196
1197 @param[in] RsaContext Pointer to the RSA context to be released.
1198
1199 **/
1200 VOID
1201 EFIAPI
1202 RsaFree (
1203 IN VOID *RsaContext
1204 );
1205
1206 /**
1207 Sets the tag-designated key component into the established RSA context.
1208
1209 This function sets the tag-designated RSA key component into the established
1210 RSA context from the user-specified non-negative integer (octet string format
1211 represented in RSA PKCS#1).
1212 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1213
1214 If RsaContext is NULL, then return FALSE.
1215
1216 @param[in, out] RsaContext Pointer to RSA context being set.
1217 @param[in] KeyTag Tag of RSA key component being set.
1218 @param[in] BigNumber Pointer to octet integer buffer.
1219 If NULL, then the specified key component in RSA
1220 context is cleared.
1221 @param[in] BnSize Size of big number buffer in bytes.
1222 If BigNumber is NULL, then it is ignored.
1223
1224 @retval TRUE RSA key component was set successfully.
1225 @retval FALSE Invalid RSA key component tag.
1226
1227 **/
1228 BOOLEAN
1229 EFIAPI
1230 RsaSetKey (
1231 IN OUT VOID *RsaContext,
1232 IN RSA_KEY_TAG KeyTag,
1233 IN CONST UINT8 *BigNumber,
1234 IN UINTN BnSize
1235 );
1236
1237 /**
1238 Gets the tag-designated RSA key component from the established RSA context.
1239
1240 This function retrieves the tag-designated RSA key component from the
1241 established RSA context as a non-negative integer (octet string format
1242 represented in RSA PKCS#1).
1243 If specified key component has not been set or has been cleared, then returned
1244 BnSize is set to 0.
1245 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1246 is returned and BnSize is set to the required buffer size to obtain the key.
1247
1248 If RsaContext is NULL, then return FALSE.
1249 If BnSize is NULL, then return FALSE.
1250 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1251 If this interface is not supported, then return FALSE.
1252
1253 @param[in, out] RsaContext Pointer to RSA context being set.
1254 @param[in] KeyTag Tag of RSA key component being set.
1255 @param[out] BigNumber Pointer to octet integer buffer.
1256 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1257 On output, the size of data returned in big number buffer in bytes.
1258
1259 @retval TRUE RSA key component was retrieved successfully.
1260 @retval FALSE Invalid RSA key component tag.
1261 @retval FALSE BnSize is too small.
1262 @retval FALSE This interface is not supported.
1263
1264 **/
1265 BOOLEAN
1266 EFIAPI
1267 RsaGetKey (
1268 IN OUT VOID *RsaContext,
1269 IN RSA_KEY_TAG KeyTag,
1270 OUT UINT8 *BigNumber,
1271 IN OUT UINTN *BnSize
1272 );
1273
1274 /**
1275 Generates RSA key components.
1276
1277 This function generates RSA key components. It takes RSA public exponent E and
1278 length in bits of RSA modulus N as input, and generates all key components.
1279 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1280
1281 Before this function can be invoked, pseudorandom number generator must be correctly
1282 initialized by RandomSeed().
1283
1284 If RsaContext is NULL, then return FALSE.
1285 If this interface is not supported, then return FALSE.
1286
1287 @param[in, out] RsaContext Pointer to RSA context being set.
1288 @param[in] ModulusLength Length of RSA modulus N in bits.
1289 @param[in] PublicExponent Pointer to RSA public exponent.
1290 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1291
1292 @retval TRUE RSA key component was generated successfully.
1293 @retval FALSE Invalid RSA key component tag.
1294 @retval FALSE This interface is not supported.
1295
1296 **/
1297 BOOLEAN
1298 EFIAPI
1299 RsaGenerateKey (
1300 IN OUT VOID *RsaContext,
1301 IN UINTN ModulusLength,
1302 IN CONST UINT8 *PublicExponent,
1303 IN UINTN PublicExponentSize
1304 );
1305
1306 /**
1307 Validates key components of RSA context.
1308 NOTE: This function performs integrity checks on all the RSA key material, so
1309 the RSA key structure must contain all the private key data.
1310
1311 This function validates key components of RSA context in following aspects:
1312 - Whether p is a prime
1313 - Whether q is a prime
1314 - Whether n = p * q
1315 - Whether d*e = 1 mod lcm(p-1,q-1)
1316
1317 If RsaContext is NULL, then return FALSE.
1318 If this interface is not supported, then return FALSE.
1319
1320 @param[in] RsaContext Pointer to RSA context to check.
1321
1322 @retval TRUE RSA key components are valid.
1323 @retval FALSE RSA key components are not valid.
1324 @retval FALSE This interface is not supported.
1325
1326 **/
1327 BOOLEAN
1328 EFIAPI
1329 RsaCheckKey (
1330 IN VOID *RsaContext
1331 );
1332
1333 /**
1334 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1335
1336 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1337 RSA PKCS#1.
1338 If the Signature buffer is too small to hold the contents of signature, FALSE
1339 is returned and SigSize is set to the required buffer size to obtain the signature.
1340
1341 If RsaContext is NULL, then return FALSE.
1342 If MessageHash is NULL, then return FALSE.
1343 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1344 If SigSize is large enough but Signature is NULL, then return FALSE.
1345 If this interface is not supported, then return FALSE.
1346
1347 @param[in] RsaContext Pointer to RSA context for signature generation.
1348 @param[in] MessageHash Pointer to octet message hash to be signed.
1349 @param[in] HashSize Size of the message hash in bytes.
1350 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1351 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1352 On output, the size of data returned in Signature buffer in bytes.
1353
1354 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1355 @retval FALSE Signature generation failed.
1356 @retval FALSE SigSize is too small.
1357 @retval FALSE This interface is not supported.
1358
1359 **/
1360 BOOLEAN
1361 EFIAPI
1362 RsaPkcs1Sign (
1363 IN VOID *RsaContext,
1364 IN CONST UINT8 *MessageHash,
1365 IN UINTN HashSize,
1366 OUT UINT8 *Signature,
1367 IN OUT UINTN *SigSize
1368 );
1369
1370 /**
1371 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1372 RSA PKCS#1.
1373
1374 If RsaContext is NULL, then return FALSE.
1375 If MessageHash is NULL, then return FALSE.
1376 If Signature is NULL, then return FALSE.
1377 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1378
1379 @param[in] RsaContext Pointer to RSA context for signature verification.
1380 @param[in] MessageHash Pointer to octet message hash to be checked.
1381 @param[in] HashSize Size of the message hash in bytes.
1382 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1383 @param[in] SigSize Size of signature in bytes.
1384
1385 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1386 @retval FALSE Invalid signature or invalid RSA context.
1387
1388 **/
1389 BOOLEAN
1390 EFIAPI
1391 RsaPkcs1Verify (
1392 IN VOID *RsaContext,
1393 IN CONST UINT8 *MessageHash,
1394 IN UINTN HashSize,
1395 IN CONST UINT8 *Signature,
1396 IN UINTN SigSize
1397 );
1398
1399 /**
1400 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1401
1402 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1403 RFC 8017.
1404 Mask generation function is the same as the message digest algorithm.
1405 If the Signature buffer is too small to hold the contents of signature, FALSE
1406 is returned and SigSize is set to the required buffer size to obtain the signature.
1407
1408 If RsaContext is NULL, then return FALSE.
1409 If Message is NULL, then return FALSE.
1410 If MsgSize is zero or > INT_MAX, then return FALSE.
1411 If DigestLen is NOT 32, 48 or 64, return FALSE.
1412 If SaltLen is not equal to DigestLen, then return FALSE.
1413 If SigSize is large enough but Signature is NULL, then return FALSE.
1414 If this interface is not supported, then return FALSE.
1415
1416 @param[in] RsaContext Pointer to RSA context for signature generation.
1417 @param[in] Message Pointer to octet message to be signed.
1418 @param[in] MsgSize Size of the message in bytes.
1419 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1420 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1421 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1422 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1423 On output, the size of data returned in Signature buffer in bytes.
1424
1425 @retval TRUE Signature successfully generated in RSASSA-PSS.
1426 @retval FALSE Signature generation failed.
1427 @retval FALSE SigSize is too small.
1428 @retval FALSE This interface is not supported.
1429
1430 **/
1431 BOOLEAN
1432 EFIAPI
1433 RsaPssSign (
1434 IN VOID *RsaContext,
1435 IN CONST UINT8 *Message,
1436 IN UINTN MsgSize,
1437 IN UINT16 DigestLen,
1438 IN UINT16 SaltLen,
1439 OUT UINT8 *Signature,
1440 IN OUT UINTN *SigSize
1441 );
1442
1443 /**
1444 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1445 Implementation determines salt length automatically from the signature encoding.
1446 Mask generation function is the same as the message digest algorithm.
1447 Salt length should be equal to digest length.
1448
1449 @param[in] RsaContext Pointer to RSA context for signature verification.
1450 @param[in] Message Pointer to octet message to be verified.
1451 @param[in] MsgSize Size of the message in bytes.
1452 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1453 @param[in] SigSize Size of signature in bytes.
1454 @param[in] DigestLen Length of digest for RSA operation.
1455 @param[in] SaltLen Salt length for PSS encoding.
1456
1457 @retval TRUE Valid signature encoded in RSASSA-PSS.
1458 @retval FALSE Invalid signature or invalid RSA context.
1459
1460 **/
1461 BOOLEAN
1462 EFIAPI
1463 RsaPssVerify (
1464 IN VOID *RsaContext,
1465 IN CONST UINT8 *Message,
1466 IN UINTN MsgSize,
1467 IN CONST UINT8 *Signature,
1468 IN UINTN SigSize,
1469 IN UINT16 DigestLen,
1470 IN UINT16 SaltLen
1471 );
1472
1473 /**
1474 Retrieve the RSA Private Key from the password-protected PEM key data.
1475
1476 If PemData is NULL, then return FALSE.
1477 If RsaContext is NULL, then return FALSE.
1478 If this interface is not supported, then return FALSE.
1479
1480 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1481 @param[in] PemSize Size of the PEM key data in bytes.
1482 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1483 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1484 RSA private key component. Use RsaFree() function to free the
1485 resource.
1486
1487 @retval TRUE RSA Private Key was retrieved successfully.
1488 @retval FALSE Invalid PEM key data or incorrect password.
1489 @retval FALSE This interface is not supported.
1490
1491 **/
1492 BOOLEAN
1493 EFIAPI
1494 RsaGetPrivateKeyFromPem (
1495 IN CONST UINT8 *PemData,
1496 IN UINTN PemSize,
1497 IN CONST CHAR8 *Password,
1498 OUT VOID **RsaContext
1499 );
1500
1501 /**
1502 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1503
1504 If Cert is NULL, then return FALSE.
1505 If RsaContext is NULL, then return FALSE.
1506 If this interface is not supported, then return FALSE.
1507
1508 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1509 @param[in] CertSize Size of the X509 certificate in bytes.
1510 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1511 RSA public key component. Use RsaFree() function to free the
1512 resource.
1513
1514 @retval TRUE RSA Public Key was retrieved successfully.
1515 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1516 @retval FALSE This interface is not supported.
1517
1518 **/
1519 BOOLEAN
1520 EFIAPI
1521 RsaGetPublicKeyFromX509 (
1522 IN CONST UINT8 *Cert,
1523 IN UINTN CertSize,
1524 OUT VOID **RsaContext
1525 );
1526
1527 /**
1528 Retrieve the subject bytes from one X.509 certificate.
1529
1530 If Cert is NULL, then return FALSE.
1531 If SubjectSize is NULL, then return FALSE.
1532 If this interface is not supported, then return FALSE.
1533
1534 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1535 @param[in] CertSize Size of the X509 certificate in bytes.
1536 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1537 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1538 and the size of buffer returned CertSubject on output.
1539
1540 @retval TRUE The certificate subject retrieved successfully.
1541 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1542 The SubjectSize will be updated with the required size.
1543 @retval FALSE This interface is not supported.
1544
1545 **/
1546 BOOLEAN
1547 EFIAPI
1548 X509GetSubjectName (
1549 IN CONST UINT8 *Cert,
1550 IN UINTN CertSize,
1551 OUT UINT8 *CertSubject,
1552 IN OUT UINTN *SubjectSize
1553 );
1554
1555 /**
1556 Retrieve the common name (CN) string from one X.509 certificate.
1557
1558 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1559 @param[in] CertSize Size of the X509 certificate in bytes.
1560 @param[out] CommonName Buffer to contain the retrieved certificate common
1561 name string (UTF8). At most CommonNameSize bytes will be
1562 written and the string will be null terminated. May be
1563 NULL in order to determine the size buffer needed.
1564 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1565 and the size of buffer returned CommonName on output.
1566 If CommonName is NULL then the amount of space needed
1567 in buffer (including the final null) is returned.
1568
1569 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1570 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1571 If CommonNameSize is NULL.
1572 If CommonName is not NULL and *CommonNameSize is 0.
1573 If Certificate is invalid.
1574 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1575 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1576 (including the final null) is returned in the
1577 CommonNameSize parameter.
1578 @retval RETURN_UNSUPPORTED The operation is not supported.
1579
1580 **/
1581 RETURN_STATUS
1582 EFIAPI
1583 X509GetCommonName (
1584 IN CONST UINT8 *Cert,
1585 IN UINTN CertSize,
1586 OUT CHAR8 *CommonName OPTIONAL,
1587 IN OUT UINTN *CommonNameSize
1588 );
1589
1590 /**
1591 Retrieve the organization name (O) string from one X.509 certificate.
1592
1593 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1594 @param[in] CertSize Size of the X509 certificate in bytes.
1595 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1596 name string. At most NameBufferSize bytes will be
1597 written and the string will be null terminated. May be
1598 NULL in order to determine the size buffer needed.
1599 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1600 and the size of buffer returned Name on output.
1601 If NameBuffer is NULL then the amount of space needed
1602 in buffer (including the final null) is returned.
1603
1604 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1605 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1606 If NameBufferSize is NULL.
1607 If NameBuffer is not NULL and *CommonNameSize is 0.
1608 If Certificate is invalid.
1609 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1610 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1611 (including the final null) is returned in the
1612 CommonNameSize parameter.
1613 @retval RETURN_UNSUPPORTED The operation is not supported.
1614
1615 **/
1616 RETURN_STATUS
1617 EFIAPI
1618 X509GetOrganizationName (
1619 IN CONST UINT8 *Cert,
1620 IN UINTN CertSize,
1621 OUT CHAR8 *NameBuffer OPTIONAL,
1622 IN OUT UINTN *NameBufferSize
1623 );
1624
1625 /**
1626 Verify one X509 certificate was issued by the trusted CA.
1627
1628 If Cert is NULL, then return FALSE.
1629 If CACert is NULL, then return FALSE.
1630 If this interface is not supported, then return FALSE.
1631
1632 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1633 @param[in] CertSize Size of the X509 certificate in bytes.
1634 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1635 @param[in] CACertSize Size of the CA Certificate in bytes.
1636
1637 @retval TRUE The certificate was issued by the trusted CA.
1638 @retval FALSE Invalid certificate or the certificate was not issued by the given
1639 trusted CA.
1640 @retval FALSE This interface is not supported.
1641
1642 **/
1643 BOOLEAN
1644 EFIAPI
1645 X509VerifyCert (
1646 IN CONST UINT8 *Cert,
1647 IN UINTN CertSize,
1648 IN CONST UINT8 *CACert,
1649 IN UINTN CACertSize
1650 );
1651
1652 /**
1653 Construct a X509 object from DER-encoded certificate data.
1654
1655 If Cert is NULL, then return FALSE.
1656 If SingleX509Cert is NULL, then return FALSE.
1657 If this interface is not supported, then return FALSE.
1658
1659 @param[in] Cert Pointer to the DER-encoded certificate data.
1660 @param[in] CertSize The size of certificate data in bytes.
1661 @param[out] SingleX509Cert The generated X509 object.
1662
1663 @retval TRUE The X509 object generation succeeded.
1664 @retval FALSE The operation failed.
1665 @retval FALSE This interface is not supported.
1666
1667 **/
1668 BOOLEAN
1669 EFIAPI
1670 X509ConstructCertificate (
1671 IN CONST UINT8 *Cert,
1672 IN UINTN CertSize,
1673 OUT UINT8 **SingleX509Cert
1674 );
1675
1676 /**
1677 Construct a X509 stack object from a list of DER-encoded certificate data.
1678
1679 If X509Stack is NULL, then return FALSE.
1680 If this interface is not supported, then return FALSE.
1681
1682 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1683 On output, pointer to the X509 stack object with new
1684 inserted X509 certificate.
1685 @param[in] Args VA_LIST marker for the variable argument list.
1686 A list of DER-encoded single certificate data followed
1687 by certificate size. A NULL terminates the list. The
1688 pairs are the arguments to X509ConstructCertificate().
1689
1690 @retval TRUE The X509 stack construction succeeded.
1691 @retval FALSE The construction operation failed.
1692 @retval FALSE This interface is not supported.
1693
1694 **/
1695 BOOLEAN
1696 EFIAPI
1697 X509ConstructCertificateStackV (
1698 IN OUT UINT8 **X509Stack,
1699 IN VA_LIST Args
1700 );
1701
1702 /**
1703 Construct a X509 stack object from a list of DER-encoded certificate data.
1704
1705 If X509Stack is NULL, then return FALSE.
1706 If this interface is not supported, then return FALSE.
1707
1708 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1709 On output, pointer to the X509 stack object with new
1710 inserted X509 certificate.
1711 @param ... A list of DER-encoded single certificate data followed
1712 by certificate size. A NULL terminates the list. The
1713 pairs are the arguments to X509ConstructCertificate().
1714
1715 @retval TRUE The X509 stack construction succeeded.
1716 @retval FALSE The construction operation failed.
1717 @retval FALSE This interface is not supported.
1718
1719 **/
1720 BOOLEAN
1721 EFIAPI
1722 X509ConstructCertificateStack (
1723 IN OUT UINT8 **X509Stack,
1724 ...
1725 );
1726
1727 /**
1728 Release the specified X509 object.
1729
1730 If the interface is not supported, then ASSERT().
1731
1732 @param[in] X509Cert Pointer to the X509 object to be released.
1733
1734 **/
1735 VOID
1736 EFIAPI
1737 X509Free (
1738 IN VOID *X509Cert
1739 );
1740
1741 /**
1742 Release the specified X509 stack object.
1743
1744 If the interface is not supported, then ASSERT().
1745
1746 @param[in] X509Stack Pointer to the X509 stack object to be released.
1747
1748 **/
1749 VOID
1750 EFIAPI
1751 X509StackFree (
1752 IN VOID *X509Stack
1753 );
1754
1755 /**
1756 Retrieve the TBSCertificate from one given X.509 certificate.
1757
1758 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
1759 @param[in] CertSize Size of the X509 certificate in bytes.
1760 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
1761 @param[out] TBSCertSize Size of the TBS certificate in bytes.
1762
1763 If Cert is NULL, then return FALSE.
1764 If TBSCert is NULL, then return FALSE.
1765 If TBSCertSize is NULL, then return FALSE.
1766 If this interface is not supported, then return FALSE.
1767
1768 @retval TRUE The TBSCertificate was retrieved successfully.
1769 @retval FALSE Invalid X.509 certificate.
1770
1771 **/
1772 BOOLEAN
1773 EFIAPI
1774 X509GetTBSCert (
1775 IN CONST UINT8 *Cert,
1776 IN UINTN CertSize,
1777 OUT UINT8 **TBSCert,
1778 OUT UINTN *TBSCertSize
1779 );
1780
1781 /**
1782 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
1783 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
1784
1785 If Password or Salt or OutKey is NULL, then return FALSE.
1786 If the hash algorithm could not be determined, then return FALSE.
1787 If this interface is not supported, then return FALSE.
1788
1789 @param[in] PasswordLength Length of input password in bytes.
1790 @param[in] Password Pointer to the array for the password.
1791 @param[in] SaltLength Size of the Salt in bytes.
1792 @param[in] Salt Pointer to the Salt.
1793 @param[in] IterationCount Number of iterations to perform. Its value should be
1794 greater than or equal to 1.
1795 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
1796 NOTE: DigestSize will be used to determine the hash algorithm.
1797 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
1798 @param[in] KeyLength Size of the derived key buffer in bytes.
1799 @param[out] OutKey Pointer to the output derived key buffer.
1800
1801 @retval TRUE A key was derived successfully.
1802 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
1803 @retval FALSE The hash algorithm could not be determined from the digest size.
1804 @retval FALSE The key derivation operation failed.
1805 @retval FALSE This interface is not supported.
1806
1807 **/
1808 BOOLEAN
1809 EFIAPI
1810 Pkcs5HashPassword (
1811 IN UINTN PasswordLength,
1812 IN CONST CHAR8 *Password,
1813 IN UINTN SaltLength,
1814 IN CONST UINT8 *Salt,
1815 IN UINTN IterationCount,
1816 IN UINTN DigestSize,
1817 IN UINTN KeyLength,
1818 OUT UINT8 *OutKey
1819 );
1820
1821 /**
1822 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
1823 encrypted message in a newly allocated buffer.
1824
1825 Things that can cause a failure include:
1826 - X509 key size does not match any known key size.
1827 - Fail to parse X509 certificate.
1828 - Fail to allocate an intermediate buffer.
1829 - Null pointer provided for a non-optional parameter.
1830 - Data size is too large for the provided key size (max size is a function of key size
1831 and hash digest size).
1832
1833 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
1834 will be used to encrypt the data.
1835 @param[in] PublicKeySize Size of the X509 cert buffer.
1836 @param[in] InData Data to be encrypted.
1837 @param[in] InDataSize Size of the data buffer.
1838 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
1839 to be used when initializing the PRNG. NULL otherwise.
1840 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
1841 0 otherwise.
1842 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
1843 message.
1844 @param[out] EncryptedDataSize Size of the encrypted message buffer.
1845
1846 @retval TRUE Encryption was successful.
1847 @retval FALSE Encryption failed.
1848
1849 **/
1850 BOOLEAN
1851 EFIAPI
1852 Pkcs1v2Encrypt (
1853 IN CONST UINT8 *PublicKey,
1854 IN UINTN PublicKeySize,
1855 IN UINT8 *InData,
1856 IN UINTN InDataSize,
1857 IN CONST UINT8 *PrngSeed OPTIONAL,
1858 IN UINTN PrngSeedSize OPTIONAL,
1859 OUT UINT8 **EncryptedData,
1860 OUT UINTN *EncryptedDataSize
1861 );
1862
1863 /**
1864 The 3rd parameter of Pkcs7GetSigners will return all embedded
1865 X.509 certificate in one given PKCS7 signature. The format is:
1866 //
1867 // UINT8 CertNumber;
1868 // UINT32 Cert1Length;
1869 // UINT8 Cert1[];
1870 // UINT32 Cert2Length;
1871 // UINT8 Cert2[];
1872 // ...
1873 // UINT32 CertnLength;
1874 // UINT8 Certn[];
1875 //
1876
1877 The two following C-structure are used for parsing CertStack more clearly.
1878 **/
1879 #pragma pack(1)
1880
1881 typedef struct {
1882 UINT32 CertDataLength; // The length in bytes of X.509 certificate.
1883 UINT8 CertDataBuffer[0]; // The X.509 certificate content (DER).
1884 } EFI_CERT_DATA;
1885
1886 typedef struct {
1887 UINT8 CertNumber; // Number of X.509 certificate.
1888 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
1889 } EFI_CERT_STACK;
1890
1891 #pragma pack()
1892
1893 /**
1894 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
1895 Cryptographic Message Syntax Standard". The input signed data could be wrapped
1896 in a ContentInfo structure.
1897
1898 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
1899 return FALSE. If P7Length overflow, then return FALSE.
1900 If this interface is not supported, then return FALSE.
1901
1902 @param[in] P7Data Pointer to the PKCS#7 message to verify.
1903 @param[in] P7Length Length of the PKCS#7 message in bytes.
1904 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
1905 It's caller's responsibility to free the buffer with
1906 Pkcs7FreeSigners().
1907 This data structure is EFI_CERT_STACK type.
1908 @param[out] StackLength Length of signer's certificates in bytes.
1909 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
1910 It's caller's responsibility to free the buffer with
1911 Pkcs7FreeSigners().
1912 @param[out] CertLength Length of the trusted certificate in bytes.
1913
1914 @retval TRUE The operation is finished successfully.
1915 @retval FALSE Error occurs during the operation.
1916 @retval FALSE This interface is not supported.
1917
1918 **/
1919 BOOLEAN
1920 EFIAPI
1921 Pkcs7GetSigners (
1922 IN CONST UINT8 *P7Data,
1923 IN UINTN P7Length,
1924 OUT UINT8 **CertStack,
1925 OUT UINTN *StackLength,
1926 OUT UINT8 **TrustedCert,
1927 OUT UINTN *CertLength
1928 );
1929
1930 /**
1931 Wrap function to use free() to free allocated memory for certificates.
1932
1933 If this interface is not supported, then ASSERT().
1934
1935 @param[in] Certs Pointer to the certificates to be freed.
1936
1937 **/
1938 VOID
1939 EFIAPI
1940 Pkcs7FreeSigners (
1941 IN UINT8 *Certs
1942 );
1943
1944 /**
1945 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
1946 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
1947 unchained to the signer's certificates.
1948 The input signed data could be wrapped in a ContentInfo structure.
1949
1950 @param[in] P7Data Pointer to the PKCS#7 message.
1951 @param[in] P7Length Length of the PKCS#7 message in bytes.
1952 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
1953 certificate. It's caller's responsibility to free the buffer
1954 with Pkcs7FreeSigners().
1955 This data structure is EFI_CERT_STACK type.
1956 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
1957 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
1958 responsibility to free the buffer with Pkcs7FreeSigners().
1959 This data structure is EFI_CERT_STACK type.
1960 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
1961
1962 @retval TRUE The operation is finished successfully.
1963 @retval FALSE Error occurs during the operation.
1964
1965 **/
1966 BOOLEAN
1967 EFIAPI
1968 Pkcs7GetCertificatesList (
1969 IN CONST UINT8 *P7Data,
1970 IN UINTN P7Length,
1971 OUT UINT8 **SignerChainCerts,
1972 OUT UINTN *ChainLength,
1973 OUT UINT8 **UnchainCerts,
1974 OUT UINTN *UnchainLength
1975 );
1976
1977 /**
1978 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
1979 Syntax Standard, version 1.5". This interface is only intended to be used for
1980 application to perform PKCS#7 functionality validation.
1981
1982 If this interface is not supported, then return FALSE.
1983
1984 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
1985 data signing.
1986 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
1987 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
1988 key data.
1989 @param[in] InData Pointer to the content to be signed.
1990 @param[in] InDataSize Size of InData in bytes.
1991 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
1992 @param[in] OtherCerts Pointer to an optional additional set of certificates to
1993 include in the PKCS#7 signedData (e.g. any intermediate
1994 CAs in the chain).
1995 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
1996 responsibility to free the buffer with FreePool().
1997 @param[out] SignedDataSize Size of SignedData in bytes.
1998
1999 @retval TRUE PKCS#7 data signing succeeded.
2000 @retval FALSE PKCS#7 data signing failed.
2001 @retval FALSE This interface is not supported.
2002
2003 **/
2004 BOOLEAN
2005 EFIAPI
2006 Pkcs7Sign (
2007 IN CONST UINT8 *PrivateKey,
2008 IN UINTN PrivateKeySize,
2009 IN CONST UINT8 *KeyPassword,
2010 IN UINT8 *InData,
2011 IN UINTN InDataSize,
2012 IN UINT8 *SignCert,
2013 IN UINT8 *OtherCerts OPTIONAL,
2014 OUT UINT8 **SignedData,
2015 OUT UINTN *SignedDataSize
2016 );
2017
2018 /**
2019 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
2020 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2021 in a ContentInfo structure.
2022
2023 If P7Data, TrustedCert or InData is NULL, then return FALSE.
2024 If P7Length, CertLength or DataLength overflow, then return FALSE.
2025 If this interface is not supported, then return FALSE.
2026
2027 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2028 @param[in] P7Length Length of the PKCS#7 message in bytes.
2029 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2030 is used for certificate chain verification.
2031 @param[in] CertLength Length of the trusted certificate in bytes.
2032 @param[in] InData Pointer to the content to be verified.
2033 @param[in] DataLength Length of InData in bytes.
2034
2035 @retval TRUE The specified PKCS#7 signed data is valid.
2036 @retval FALSE Invalid PKCS#7 signed data.
2037 @retval FALSE This interface is not supported.
2038
2039 **/
2040 BOOLEAN
2041 EFIAPI
2042 Pkcs7Verify (
2043 IN CONST UINT8 *P7Data,
2044 IN UINTN P7Length,
2045 IN CONST UINT8 *TrustedCert,
2046 IN UINTN CertLength,
2047 IN CONST UINT8 *InData,
2048 IN UINTN DataLength
2049 );
2050
2051 /**
2052 This function receives a PKCS7 formatted signature, and then verifies that
2053 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2054 leaf signing certificate.
2055 Note that this function does not validate the certificate chain.
2056
2057 Applications for custom EKU's are quite flexible. For example, a policy EKU
2058 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2059 certificate issued might also contain this EKU, thus constraining the
2060 sub-ordinate certificate. Other applications might allow a certificate
2061 embedded in a device to specify that other Object Identifiers (OIDs) are
2062 present which contains binary data specifying custom capabilities that
2063 the device is able to do.
2064
2065 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2066 containing the content block with both the signature,
2067 the signer's certificate, and any necessary intermediate
2068 certificates.
2069 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2070 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2071 required EKUs that must be present in the signature.
2072 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2073 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2074 must be present in the leaf signer. If it is
2075 FALSE, then we will succeed if we find any
2076 of the specified EKU's.
2077
2078 @retval EFI_SUCCESS The required EKUs were found in the signature.
2079 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2080 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2081
2082 **/
2083 RETURN_STATUS
2084 EFIAPI
2085 VerifyEKUsInPkcs7Signature (
2086 IN CONST UINT8 *Pkcs7Signature,
2087 IN CONST UINT32 SignatureSize,
2088 IN CONST CHAR8 *RequiredEKUs[],
2089 IN CONST UINT32 RequiredEKUsSize,
2090 IN BOOLEAN RequireAllPresent
2091 );
2092
2093 /**
2094 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2095 data could be wrapped in a ContentInfo structure.
2096
2097 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2098 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2099
2100 Caution: This function may receive untrusted input. So this function will do
2101 basic check for PKCS#7 data structure.
2102
2103 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2104 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2105 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2106 It's caller's responsibility to free the buffer with FreePool().
2107 @param[out] ContentSize The size of the extracted content in bytes.
2108
2109 @retval TRUE The P7Data was correctly formatted for processing.
2110 @retval FALSE The P7Data was not correctly formatted for processing.
2111
2112 **/
2113 BOOLEAN
2114 EFIAPI
2115 Pkcs7GetAttachedContent (
2116 IN CONST UINT8 *P7Data,
2117 IN UINTN P7Length,
2118 OUT VOID **Content,
2119 OUT UINTN *ContentSize
2120 );
2121
2122 /**
2123 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2124 Authenticode Portable Executable Signature Format".
2125
2126 If AuthData is NULL, then return FALSE.
2127 If ImageHash is NULL, then return FALSE.
2128 If this interface is not supported, then return FALSE.
2129
2130 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2131 PE/COFF image to be verified.
2132 @param[in] DataSize Size of the Authenticode Signature in bytes.
2133 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2134 is used for certificate chain verification.
2135 @param[in] CertSize Size of the trusted certificate in bytes.
2136 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2137 for calculating the image hash value is described in Authenticode
2138 specification.
2139 @param[in] HashSize Size of Image hash value in bytes.
2140
2141 @retval TRUE The specified Authenticode Signature is valid.
2142 @retval FALSE Invalid Authenticode Signature.
2143 @retval FALSE This interface is not supported.
2144
2145 **/
2146 BOOLEAN
2147 EFIAPI
2148 AuthenticodeVerify (
2149 IN CONST UINT8 *AuthData,
2150 IN UINTN DataSize,
2151 IN CONST UINT8 *TrustedCert,
2152 IN UINTN CertSize,
2153 IN CONST UINT8 *ImageHash,
2154 IN UINTN HashSize
2155 );
2156
2157 /**
2158 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2159 signature.
2160
2161 If AuthData is NULL, then return FALSE.
2162 If this interface is not supported, then return FALSE.
2163
2164 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2165 PE/COFF image to be verified.
2166 @param[in] DataSize Size of the Authenticode Signature in bytes.
2167 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2168 is used for TSA certificate chain verification.
2169 @param[in] CertSize Size of the trusted certificate in bytes.
2170 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2171 signature is valid.
2172
2173 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2174 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2175
2176 **/
2177 BOOLEAN
2178 EFIAPI
2179 ImageTimestampVerify (
2180 IN CONST UINT8 *AuthData,
2181 IN UINTN DataSize,
2182 IN CONST UINT8 *TsaCert,
2183 IN UINTN CertSize,
2184 OUT EFI_TIME *SigningTime
2185 );
2186
2187 // =====================================================================================
2188 // DH Key Exchange Primitive
2189 // =====================================================================================
2190
2191 /**
2192 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2193
2194 @return Pointer to the Diffie-Hellman Context that has been initialized.
2195 If the allocations fails, DhNew() returns NULL.
2196 If the interface is not supported, DhNew() returns NULL.
2197
2198 **/
2199 VOID *
2200 EFIAPI
2201 DhNew (
2202 VOID
2203 );
2204
2205 /**
2206 Release the specified DH context.
2207
2208 If the interface is not supported, then ASSERT().
2209
2210 @param[in] DhContext Pointer to the DH context to be released.
2211
2212 **/
2213 VOID
2214 EFIAPI
2215 DhFree (
2216 IN VOID *DhContext
2217 );
2218
2219 /**
2220 Generates DH parameter.
2221
2222 Given generator g, and length of prime number p in bits, this function generates p,
2223 and sets DH context according to value of g and p.
2224
2225 Before this function can be invoked, pseudorandom number generator must be correctly
2226 initialized by RandomSeed().
2227
2228 If DhContext is NULL, then return FALSE.
2229 If Prime is NULL, then return FALSE.
2230 If this interface is not supported, then return FALSE.
2231
2232 @param[in, out] DhContext Pointer to the DH context.
2233 @param[in] Generator Value of generator.
2234 @param[in] PrimeLength Length in bits of prime to be generated.
2235 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2236
2237 @retval TRUE DH parameter generation succeeded.
2238 @retval FALSE Value of Generator is not supported.
2239 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2240 @retval FALSE This interface is not supported.
2241
2242 **/
2243 BOOLEAN
2244 EFIAPI
2245 DhGenerateParameter (
2246 IN OUT VOID *DhContext,
2247 IN UINTN Generator,
2248 IN UINTN PrimeLength,
2249 OUT UINT8 *Prime
2250 );
2251
2252 /**
2253 Sets generator and prime parameters for DH.
2254
2255 Given generator g, and prime number p, this function and sets DH
2256 context accordingly.
2257
2258 If DhContext is NULL, then return FALSE.
2259 If Prime is NULL, then return FALSE.
2260 If this interface is not supported, then return FALSE.
2261
2262 @param[in, out] DhContext Pointer to the DH context.
2263 @param[in] Generator Value of generator.
2264 @param[in] PrimeLength Length in bits of prime to be generated.
2265 @param[in] Prime Pointer to the prime number.
2266
2267 @retval TRUE DH parameter setting succeeded.
2268 @retval FALSE Value of Generator is not supported.
2269 @retval FALSE Value of Generator is not suitable for the Prime.
2270 @retval FALSE Value of Prime is not a prime number.
2271 @retval FALSE Value of Prime is not a safe prime number.
2272 @retval FALSE This interface is not supported.
2273
2274 **/
2275 BOOLEAN
2276 EFIAPI
2277 DhSetParameter (
2278 IN OUT VOID *DhContext,
2279 IN UINTN Generator,
2280 IN UINTN PrimeLength,
2281 IN CONST UINT8 *Prime
2282 );
2283
2284 /**
2285 Generates DH public key.
2286
2287 This function generates random secret exponent, and computes the public key, which is
2288 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2289 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2290 PublicKeySize is set to the required buffer size to obtain the public key.
2291
2292 If DhContext is NULL, then return FALSE.
2293 If PublicKeySize is NULL, then return FALSE.
2294 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2295 If this interface is not supported, then return FALSE.
2296
2297 @param[in, out] DhContext Pointer to the DH context.
2298 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2299 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2300 On output, the size of data returned in PublicKey buffer in bytes.
2301
2302 @retval TRUE DH public key generation succeeded.
2303 @retval FALSE DH public key generation failed.
2304 @retval FALSE PublicKeySize is not large enough.
2305 @retval FALSE This interface is not supported.
2306
2307 **/
2308 BOOLEAN
2309 EFIAPI
2310 DhGenerateKey (
2311 IN OUT VOID *DhContext,
2312 OUT UINT8 *PublicKey,
2313 IN OUT UINTN *PublicKeySize
2314 );
2315
2316 /**
2317 Computes exchanged common key.
2318
2319 Given peer's public key, this function computes the exchanged common key, based on its own
2320 context including value of prime modulus and random secret exponent.
2321
2322 If DhContext is NULL, then return FALSE.
2323 If PeerPublicKey is NULL, then return FALSE.
2324 If KeySize is NULL, then return FALSE.
2325 If Key is NULL, then return FALSE.
2326 If KeySize is not large enough, then return FALSE.
2327 If this interface is not supported, then return FALSE.
2328
2329 @param[in, out] DhContext Pointer to the DH context.
2330 @param[in] PeerPublicKey Pointer to the peer's public key.
2331 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2332 @param[out] Key Pointer to the buffer to receive generated key.
2333 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2334 On output, the size of data returned in Key buffer in bytes.
2335
2336 @retval TRUE DH exchanged key generation succeeded.
2337 @retval FALSE DH exchanged key generation failed.
2338 @retval FALSE KeySize is not large enough.
2339 @retval FALSE This interface is not supported.
2340
2341 **/
2342 BOOLEAN
2343 EFIAPI
2344 DhComputeKey (
2345 IN OUT VOID *DhContext,
2346 IN CONST UINT8 *PeerPublicKey,
2347 IN UINTN PeerPublicKeySize,
2348 OUT UINT8 *Key,
2349 IN OUT UINTN *KeySize
2350 );
2351
2352 // =====================================================================================
2353 // Pseudo-Random Generation Primitive
2354 // =====================================================================================
2355
2356 /**
2357 Sets up the seed value for the pseudorandom number generator.
2358
2359 This function sets up the seed value for the pseudorandom number generator.
2360 If Seed is not NULL, then the seed passed in is used.
2361 If Seed is NULL, then default seed is used.
2362 If this interface is not supported, then return FALSE.
2363
2364 @param[in] Seed Pointer to seed value.
2365 If NULL, default seed is used.
2366 @param[in] SeedSize Size of seed value.
2367 If Seed is NULL, this parameter is ignored.
2368
2369 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2370 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2371 @retval FALSE This interface is not supported.
2372
2373 **/
2374 BOOLEAN
2375 EFIAPI
2376 RandomSeed (
2377 IN CONST UINT8 *Seed OPTIONAL,
2378 IN UINTN SeedSize
2379 );
2380
2381 /**
2382 Generates a pseudorandom byte stream of the specified size.
2383
2384 If Output is NULL, then return FALSE.
2385 If this interface is not supported, then return FALSE.
2386
2387 @param[out] Output Pointer to buffer to receive random value.
2388 @param[in] Size Size of random bytes to generate.
2389
2390 @retval TRUE Pseudorandom byte stream generated successfully.
2391 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2392 @retval FALSE This interface is not supported.
2393
2394 **/
2395 BOOLEAN
2396 EFIAPI
2397 RandomBytes (
2398 OUT UINT8 *Output,
2399 IN UINTN Size
2400 );
2401
2402 // =====================================================================================
2403 // Key Derivation Function Primitive
2404 // =====================================================================================
2405
2406 /**
2407 Derive key data using HMAC-SHA256 based KDF.
2408
2409 @param[in] Key Pointer to the user-supplied key.
2410 @param[in] KeySize Key size in bytes.
2411 @param[in] Salt Pointer to the salt(non-secret) value.
2412 @param[in] SaltSize Salt size in bytes.
2413 @param[in] Info Pointer to the application specific info.
2414 @param[in] InfoSize Info size in bytes.
2415 @param[out] Out Pointer to buffer to receive hkdf value.
2416 @param[in] OutSize Size of hkdf bytes to generate.
2417
2418 @retval TRUE Hkdf generated successfully.
2419 @retval FALSE Hkdf generation failed.
2420
2421 **/
2422 BOOLEAN
2423 EFIAPI
2424 HkdfSha256ExtractAndExpand (
2425 IN CONST UINT8 *Key,
2426 IN UINTN KeySize,
2427 IN CONST UINT8 *Salt,
2428 IN UINTN SaltSize,
2429 IN CONST UINT8 *Info,
2430 IN UINTN InfoSize,
2431 OUT UINT8 *Out,
2432 IN UINTN OutSize
2433 );
2434
2435 #endif // __BASE_CRYPT_LIB_H__