]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg: Apply uncrustify changes
[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 - 2020, 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 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
758
759 @return The size, in bytes, of the context buffer required for SM3 hash operations.
760
761 **/
762 UINTN
763 EFIAPI
764 Sm3GetContextSize (
765 VOID
766 );
767
768 /**
769 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
770 subsequent use.
771
772 If Sm3Context is NULL, then return FALSE.
773
774 @param[out] Sm3Context Pointer to SM3 context being initialized.
775
776 @retval TRUE SM3 context initialization succeeded.
777 @retval FALSE SM3 context initialization failed.
778
779 **/
780 BOOLEAN
781 EFIAPI
782 Sm3Init (
783 OUT VOID *Sm3Context
784 );
785
786 /**
787 Makes a copy of an existing SM3 context.
788
789 If Sm3Context is NULL, then return FALSE.
790 If NewSm3Context is NULL, then return FALSE.
791 If this interface is not supported, then return FALSE.
792
793 @param[in] Sm3Context Pointer to SM3 context being copied.
794 @param[out] NewSm3Context Pointer to new SM3 context.
795
796 @retval TRUE SM3 context copy succeeded.
797 @retval FALSE SM3 context copy failed.
798 @retval FALSE This interface is not supported.
799
800 **/
801 BOOLEAN
802 EFIAPI
803 Sm3Duplicate (
804 IN CONST VOID *Sm3Context,
805 OUT VOID *NewSm3Context
806 );
807
808 /**
809 Digests the input data and updates SM3 context.
810
811 This function performs SM3 digest on a data buffer of the specified size.
812 It can be called multiple times to compute the digest of long or discontinuous data streams.
813 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
814 by Sm3Final(). Behavior with invalid context is undefined.
815
816 If Sm3Context is NULL, then return FALSE.
817
818 @param[in, out] Sm3Context Pointer to the SM3 context.
819 @param[in] Data Pointer to the buffer containing the data to be hashed.
820 @param[in] DataSize Size of Data buffer in bytes.
821
822 @retval TRUE SM3 data digest succeeded.
823 @retval FALSE SM3 data digest failed.
824
825 **/
826 BOOLEAN
827 EFIAPI
828 Sm3Update (
829 IN OUT VOID *Sm3Context,
830 IN CONST VOID *Data,
831 IN UINTN DataSize
832 );
833
834 /**
835 Completes computation of the SM3 digest value.
836
837 This function completes SM3 hash computation and retrieves the digest value into
838 the specified memory. After this function has been called, the SM3 context cannot
839 be used again.
840 SM3 context should be already correctly initialized by Sm3Init(), and should not be
841 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
842
843 If Sm3Context is NULL, then return FALSE.
844 If HashValue is NULL, then return FALSE.
845
846 @param[in, out] Sm3Context Pointer to the SM3 context.
847 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
848 value (32 bytes).
849
850 @retval TRUE SM3 digest computation succeeded.
851 @retval FALSE SM3 digest computation failed.
852
853 **/
854 BOOLEAN
855 EFIAPI
856 Sm3Final (
857 IN OUT VOID *Sm3Context,
858 OUT UINT8 *HashValue
859 );
860
861 /**
862 Computes the SM3 message digest of a input data buffer.
863
864 This function performs the SM3 message digest of a given data buffer, and places
865 the digest value into the specified memory.
866
867 If this interface is not supported, then return FALSE.
868
869 @param[in] Data Pointer to the buffer containing the data to be hashed.
870 @param[in] DataSize Size of Data buffer in bytes.
871 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
872 value (32 bytes).
873
874 @retval TRUE SM3 digest computation succeeded.
875 @retval FALSE SM3 digest computation failed.
876 @retval FALSE This interface is not supported.
877
878 **/
879 BOOLEAN
880 EFIAPI
881 Sm3HashAll (
882 IN CONST VOID *Data,
883 IN UINTN DataSize,
884 OUT UINT8 *HashValue
885 );
886
887 // =====================================================================================
888 // MAC (Message Authentication Code) Primitive
889 // =====================================================================================
890
891 /**
892 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
893
894 @return Pointer to the HMAC_CTX context that has been initialized.
895 If the allocations fails, HmacSha256New() returns NULL.
896
897 **/
898 VOID *
899 EFIAPI
900 HmacSha256New (
901 VOID
902 );
903
904 /**
905 Release the specified HMAC_CTX context.
906
907 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
908
909 **/
910 VOID
911 EFIAPI
912 HmacSha256Free (
913 IN VOID *HmacSha256Ctx
914 );
915
916 /**
917 Set user-supplied key for subsequent use. It must be done before any
918 calling to HmacSha256Update().
919
920 If HmacSha256Context is NULL, then return FALSE.
921 If this interface is not supported, then return FALSE.
922
923 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
924 @param[in] Key Pointer to the user-supplied key.
925 @param[in] KeySize Key size in bytes.
926
927 @retval TRUE The Key is set successfully.
928 @retval FALSE The Key is set unsuccessfully.
929 @retval FALSE This interface is not supported.
930
931 **/
932 BOOLEAN
933 EFIAPI
934 HmacSha256SetKey (
935 OUT VOID *HmacSha256Context,
936 IN CONST UINT8 *Key,
937 IN UINTN KeySize
938 );
939
940 /**
941 Makes a copy of an existing HMAC-SHA256 context.
942
943 If HmacSha256Context is NULL, then return FALSE.
944 If NewHmacSha256Context is NULL, then return FALSE.
945 If this interface is not supported, then return FALSE.
946
947 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
948 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
949
950 @retval TRUE HMAC-SHA256 context copy succeeded.
951 @retval FALSE HMAC-SHA256 context copy failed.
952 @retval FALSE This interface is not supported.
953
954 **/
955 BOOLEAN
956 EFIAPI
957 HmacSha256Duplicate (
958 IN CONST VOID *HmacSha256Context,
959 OUT VOID *NewHmacSha256Context
960 );
961
962 /**
963 Digests the input data and updates HMAC-SHA256 context.
964
965 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
966 It can be called multiple times to compute the digest of long or discontinuous data streams.
967 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
968 by HmacSha256Final(). Behavior with invalid context is undefined.
969
970 If HmacSha256Context is NULL, then return FALSE.
971 If this interface is not supported, then return FALSE.
972
973 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
974 @param[in] Data Pointer to the buffer containing the data to be digested.
975 @param[in] DataSize Size of Data buffer in bytes.
976
977 @retval TRUE HMAC-SHA256 data digest succeeded.
978 @retval FALSE HMAC-SHA256 data digest failed.
979 @retval FALSE This interface is not supported.
980
981 **/
982 BOOLEAN
983 EFIAPI
984 HmacSha256Update (
985 IN OUT VOID *HmacSha256Context,
986 IN CONST VOID *Data,
987 IN UINTN DataSize
988 );
989
990 /**
991 Completes computation of the HMAC-SHA256 digest value.
992
993 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
994 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
995 be used again.
996 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
997 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
998
999 If HmacSha256Context is NULL, then return FALSE.
1000 If HmacValue is NULL, then return FALSE.
1001 If this interface is not supported, then return FALSE.
1002
1003 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1004 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
1005 value (32 bytes).
1006
1007 @retval TRUE HMAC-SHA256 digest computation succeeded.
1008 @retval FALSE HMAC-SHA256 digest computation failed.
1009 @retval FALSE This interface is not supported.
1010
1011 **/
1012 BOOLEAN
1013 EFIAPI
1014 HmacSha256Final (
1015 IN OUT VOID *HmacSha256Context,
1016 OUT UINT8 *HmacValue
1017 );
1018
1019 // =====================================================================================
1020 // Symmetric Cryptography Primitive
1021 // =====================================================================================
1022
1023 /**
1024 Retrieves the size, in bytes, of the context buffer required for AES operations.
1025
1026 If this interface is not supported, then return zero.
1027
1028 @return The size, in bytes, of the context buffer required for AES operations.
1029 @retval 0 This interface is not supported.
1030
1031 **/
1032 UINTN
1033 EFIAPI
1034 AesGetContextSize (
1035 VOID
1036 );
1037
1038 /**
1039 Initializes user-supplied memory as AES context for subsequent use.
1040
1041 This function initializes user-supplied memory pointed by AesContext as AES context.
1042 In addition, it sets up all AES key materials for subsequent encryption and decryption
1043 operations.
1044 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1045
1046 If AesContext is NULL, then return FALSE.
1047 If Key is NULL, then return FALSE.
1048 If KeyLength is not valid, then return FALSE.
1049 If this interface is not supported, then return FALSE.
1050
1051 @param[out] AesContext Pointer to AES context being initialized.
1052 @param[in] Key Pointer to the user-supplied AES key.
1053 @param[in] KeyLength Length of AES key in bits.
1054
1055 @retval TRUE AES context initialization succeeded.
1056 @retval FALSE AES context initialization failed.
1057 @retval FALSE This interface is not supported.
1058
1059 **/
1060 BOOLEAN
1061 EFIAPI
1062 AesInit (
1063 OUT VOID *AesContext,
1064 IN CONST UINT8 *Key,
1065 IN UINTN KeyLength
1066 );
1067
1068 /**
1069 Performs AES encryption on a data buffer of the specified size in CBC mode.
1070
1071 This function performs AES encryption on data buffer pointed by Input, of specified
1072 size of InputSize, in CBC mode.
1073 InputSize must be multiple of block size (16 bytes). This function does not perform
1074 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1075 Initialization vector should be one block size (16 bytes).
1076 AesContext should be already correctly initialized by AesInit(). Behavior with
1077 invalid AES context is undefined.
1078
1079 If AesContext is NULL, then return FALSE.
1080 If Input is NULL, then return FALSE.
1081 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1082 If Ivec is NULL, then return FALSE.
1083 If Output is NULL, then return FALSE.
1084 If this interface is not supported, then return FALSE.
1085
1086 @param[in] AesContext Pointer to the AES context.
1087 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1088 @param[in] InputSize Size of the Input buffer in bytes.
1089 @param[in] Ivec Pointer to initialization vector.
1090 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1091
1092 @retval TRUE AES encryption succeeded.
1093 @retval FALSE AES encryption failed.
1094 @retval FALSE This interface is not supported.
1095
1096 **/
1097 BOOLEAN
1098 EFIAPI
1099 AesCbcEncrypt (
1100 IN VOID *AesContext,
1101 IN CONST UINT8 *Input,
1102 IN UINTN InputSize,
1103 IN CONST UINT8 *Ivec,
1104 OUT UINT8 *Output
1105 );
1106
1107 /**
1108 Performs AES decryption on a data buffer of the specified size in CBC mode.
1109
1110 This function performs AES decryption on data buffer pointed by Input, of specified
1111 size of InputSize, in CBC mode.
1112 InputSize must be multiple of block size (16 bytes). This function does not perform
1113 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1114 Initialization vector should be one block size (16 bytes).
1115 AesContext should be already correctly initialized by AesInit(). Behavior with
1116 invalid AES context is undefined.
1117
1118 If AesContext is NULL, then return FALSE.
1119 If Input is NULL, then return FALSE.
1120 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1121 If Ivec is NULL, then return FALSE.
1122 If Output is NULL, then return FALSE.
1123 If this interface is not supported, then return FALSE.
1124
1125 @param[in] AesContext Pointer to the AES context.
1126 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1127 @param[in] InputSize Size of the Input buffer in bytes.
1128 @param[in] Ivec Pointer to initialization vector.
1129 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1130
1131 @retval TRUE AES decryption succeeded.
1132 @retval FALSE AES decryption failed.
1133 @retval FALSE This interface is not supported.
1134
1135 **/
1136 BOOLEAN
1137 EFIAPI
1138 AesCbcDecrypt (
1139 IN VOID *AesContext,
1140 IN CONST UINT8 *Input,
1141 IN UINTN InputSize,
1142 IN CONST UINT8 *Ivec,
1143 OUT UINT8 *Output
1144 );
1145
1146 // =====================================================================================
1147 // Asymmetric Cryptography Primitive
1148 // =====================================================================================
1149
1150 /**
1151 Allocates and initializes one RSA context for subsequent use.
1152
1153 @return Pointer to the RSA context that has been initialized.
1154 If the allocations fails, RsaNew() returns NULL.
1155
1156 **/
1157 VOID *
1158 EFIAPI
1159 RsaNew (
1160 VOID
1161 );
1162
1163 /**
1164 Release the specified RSA context.
1165
1166 If RsaContext is NULL, then return FALSE.
1167
1168 @param[in] RsaContext Pointer to the RSA context to be released.
1169
1170 **/
1171 VOID
1172 EFIAPI
1173 RsaFree (
1174 IN VOID *RsaContext
1175 );
1176
1177 /**
1178 Sets the tag-designated key component into the established RSA context.
1179
1180 This function sets the tag-designated RSA key component into the established
1181 RSA context from the user-specified non-negative integer (octet string format
1182 represented in RSA PKCS#1).
1183 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1184
1185 If RsaContext is NULL, then return FALSE.
1186
1187 @param[in, out] RsaContext Pointer to RSA context being set.
1188 @param[in] KeyTag Tag of RSA key component being set.
1189 @param[in] BigNumber Pointer to octet integer buffer.
1190 If NULL, then the specified key component in RSA
1191 context is cleared.
1192 @param[in] BnSize Size of big number buffer in bytes.
1193 If BigNumber is NULL, then it is ignored.
1194
1195 @retval TRUE RSA key component was set successfully.
1196 @retval FALSE Invalid RSA key component tag.
1197
1198 **/
1199 BOOLEAN
1200 EFIAPI
1201 RsaSetKey (
1202 IN OUT VOID *RsaContext,
1203 IN RSA_KEY_TAG KeyTag,
1204 IN CONST UINT8 *BigNumber,
1205 IN UINTN BnSize
1206 );
1207
1208 /**
1209 Gets the tag-designated RSA key component from the established RSA context.
1210
1211 This function retrieves the tag-designated RSA key component from the
1212 established RSA context as a non-negative integer (octet string format
1213 represented in RSA PKCS#1).
1214 If specified key component has not been set or has been cleared, then returned
1215 BnSize is set to 0.
1216 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1217 is returned and BnSize is set to the required buffer size to obtain the key.
1218
1219 If RsaContext is NULL, then return FALSE.
1220 If BnSize is NULL, then return FALSE.
1221 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1222 If this interface is not supported, then return FALSE.
1223
1224 @param[in, out] RsaContext Pointer to RSA context being set.
1225 @param[in] KeyTag Tag of RSA key component being set.
1226 @param[out] BigNumber Pointer to octet integer buffer.
1227 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1228 On output, the size of data returned in big number buffer in bytes.
1229
1230 @retval TRUE RSA key component was retrieved successfully.
1231 @retval FALSE Invalid RSA key component tag.
1232 @retval FALSE BnSize is too small.
1233 @retval FALSE This interface is not supported.
1234
1235 **/
1236 BOOLEAN
1237 EFIAPI
1238 RsaGetKey (
1239 IN OUT VOID *RsaContext,
1240 IN RSA_KEY_TAG KeyTag,
1241 OUT UINT8 *BigNumber,
1242 IN OUT UINTN *BnSize
1243 );
1244
1245 /**
1246 Generates RSA key components.
1247
1248 This function generates RSA key components. It takes RSA public exponent E and
1249 length in bits of RSA modulus N as input, and generates all key components.
1250 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1251
1252 Before this function can be invoked, pseudorandom number generator must be correctly
1253 initialized by RandomSeed().
1254
1255 If RsaContext is NULL, then return FALSE.
1256 If this interface is not supported, then return FALSE.
1257
1258 @param[in, out] RsaContext Pointer to RSA context being set.
1259 @param[in] ModulusLength Length of RSA modulus N in bits.
1260 @param[in] PublicExponent Pointer to RSA public exponent.
1261 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1262
1263 @retval TRUE RSA key component was generated successfully.
1264 @retval FALSE Invalid RSA key component tag.
1265 @retval FALSE This interface is not supported.
1266
1267 **/
1268 BOOLEAN
1269 EFIAPI
1270 RsaGenerateKey (
1271 IN OUT VOID *RsaContext,
1272 IN UINTN ModulusLength,
1273 IN CONST UINT8 *PublicExponent,
1274 IN UINTN PublicExponentSize
1275 );
1276
1277 /**
1278 Validates key components of RSA context.
1279 NOTE: This function performs integrity checks on all the RSA key material, so
1280 the RSA key structure must contain all the private key data.
1281
1282 This function validates key components of RSA context in following aspects:
1283 - Whether p is a prime
1284 - Whether q is a prime
1285 - Whether n = p * q
1286 - Whether d*e = 1 mod lcm(p-1,q-1)
1287
1288 If RsaContext is NULL, then return FALSE.
1289 If this interface is not supported, then return FALSE.
1290
1291 @param[in] RsaContext Pointer to RSA context to check.
1292
1293 @retval TRUE RSA key components are valid.
1294 @retval FALSE RSA key components are not valid.
1295 @retval FALSE This interface is not supported.
1296
1297 **/
1298 BOOLEAN
1299 EFIAPI
1300 RsaCheckKey (
1301 IN VOID *RsaContext
1302 );
1303
1304 /**
1305 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1306
1307 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1308 RSA PKCS#1.
1309 If the Signature buffer is too small to hold the contents of signature, FALSE
1310 is returned and SigSize is set to the required buffer size to obtain the signature.
1311
1312 If RsaContext is NULL, then return FALSE.
1313 If MessageHash is NULL, then return FALSE.
1314 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1315 If SigSize is large enough but Signature is NULL, then return FALSE.
1316 If this interface is not supported, then return FALSE.
1317
1318 @param[in] RsaContext Pointer to RSA context for signature generation.
1319 @param[in] MessageHash Pointer to octet message hash to be signed.
1320 @param[in] HashSize Size of the message hash in bytes.
1321 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1322 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1323 On output, the size of data returned in Signature buffer in bytes.
1324
1325 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1326 @retval FALSE Signature generation failed.
1327 @retval FALSE SigSize is too small.
1328 @retval FALSE This interface is not supported.
1329
1330 **/
1331 BOOLEAN
1332 EFIAPI
1333 RsaPkcs1Sign (
1334 IN VOID *RsaContext,
1335 IN CONST UINT8 *MessageHash,
1336 IN UINTN HashSize,
1337 OUT UINT8 *Signature,
1338 IN OUT UINTN *SigSize
1339 );
1340
1341 /**
1342 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1343 RSA PKCS#1.
1344
1345 If RsaContext is NULL, then return FALSE.
1346 If MessageHash is NULL, then return FALSE.
1347 If Signature is NULL, then return FALSE.
1348 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1349
1350 @param[in] RsaContext Pointer to RSA context for signature verification.
1351 @param[in] MessageHash Pointer to octet message hash to be checked.
1352 @param[in] HashSize Size of the message hash in bytes.
1353 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1354 @param[in] SigSize Size of signature in bytes.
1355
1356 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1357 @retval FALSE Invalid signature or invalid RSA context.
1358
1359 **/
1360 BOOLEAN
1361 EFIAPI
1362 RsaPkcs1Verify (
1363 IN VOID *RsaContext,
1364 IN CONST UINT8 *MessageHash,
1365 IN UINTN HashSize,
1366 IN CONST UINT8 *Signature,
1367 IN UINTN SigSize
1368 );
1369
1370 /**
1371 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1372
1373 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1374 RFC 8017.
1375 Mask generation function is the same as the message digest algorithm.
1376 If the Signature buffer is too small to hold the contents of signature, FALSE
1377 is returned and SigSize is set to the required buffer size to obtain the signature.
1378
1379 If RsaContext is NULL, then return FALSE.
1380 If Message is NULL, then return FALSE.
1381 If MsgSize is zero or > INT_MAX, then return FALSE.
1382 If DigestLen is NOT 32, 48 or 64, return FALSE.
1383 If SaltLen is not equal to DigestLen, then return FALSE.
1384 If SigSize is large enough but Signature is NULL, then return FALSE.
1385 If this interface is not supported, then return FALSE.
1386
1387 @param[in] RsaContext Pointer to RSA context for signature generation.
1388 @param[in] Message Pointer to octet message to be signed.
1389 @param[in] MsgSize Size of the message in bytes.
1390 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1391 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1392 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1393 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1394 On output, the size of data returned in Signature buffer in bytes.
1395
1396 @retval TRUE Signature successfully generated in RSASSA-PSS.
1397 @retval FALSE Signature generation failed.
1398 @retval FALSE SigSize is too small.
1399 @retval FALSE This interface is not supported.
1400
1401 **/
1402 BOOLEAN
1403 EFIAPI
1404 RsaPssSign (
1405 IN VOID *RsaContext,
1406 IN CONST UINT8 *Message,
1407 IN UINTN MsgSize,
1408 IN UINT16 DigestLen,
1409 IN UINT16 SaltLen,
1410 OUT UINT8 *Signature,
1411 IN OUT UINTN *SigSize
1412 );
1413
1414 /**
1415 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1416 Implementation determines salt length automatically from the signature encoding.
1417 Mask generation function is the same as the message digest algorithm.
1418 Salt length should be equal to digest length.
1419
1420 @param[in] RsaContext Pointer to RSA context for signature verification.
1421 @param[in] Message Pointer to octet message to be verified.
1422 @param[in] MsgSize Size of the message in bytes.
1423 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1424 @param[in] SigSize Size of signature in bytes.
1425 @param[in] DigestLen Length of digest for RSA operation.
1426 @param[in] SaltLen Salt length for PSS encoding.
1427
1428 @retval TRUE Valid signature encoded in RSASSA-PSS.
1429 @retval FALSE Invalid signature or invalid RSA context.
1430
1431 **/
1432 BOOLEAN
1433 EFIAPI
1434 RsaPssVerify (
1435 IN VOID *RsaContext,
1436 IN CONST UINT8 *Message,
1437 IN UINTN MsgSize,
1438 IN CONST UINT8 *Signature,
1439 IN UINTN SigSize,
1440 IN UINT16 DigestLen,
1441 IN UINT16 SaltLen
1442 );
1443
1444 /**
1445 Retrieve the RSA Private Key from the password-protected PEM key data.
1446
1447 If PemData is NULL, then return FALSE.
1448 If RsaContext is NULL, then return FALSE.
1449 If this interface is not supported, then return FALSE.
1450
1451 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1452 @param[in] PemSize Size of the PEM key data in bytes.
1453 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1454 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1455 RSA private key component. Use RsaFree() function to free the
1456 resource.
1457
1458 @retval TRUE RSA Private Key was retrieved successfully.
1459 @retval FALSE Invalid PEM key data or incorrect password.
1460 @retval FALSE This interface is not supported.
1461
1462 **/
1463 BOOLEAN
1464 EFIAPI
1465 RsaGetPrivateKeyFromPem (
1466 IN CONST UINT8 *PemData,
1467 IN UINTN PemSize,
1468 IN CONST CHAR8 *Password,
1469 OUT VOID **RsaContext
1470 );
1471
1472 /**
1473 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1474
1475 If Cert is NULL, then return FALSE.
1476 If RsaContext is NULL, then return FALSE.
1477 If this interface is not supported, then return FALSE.
1478
1479 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1480 @param[in] CertSize Size of the X509 certificate in bytes.
1481 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1482 RSA public key component. Use RsaFree() function to free the
1483 resource.
1484
1485 @retval TRUE RSA Public Key was retrieved successfully.
1486 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1487 @retval FALSE This interface is not supported.
1488
1489 **/
1490 BOOLEAN
1491 EFIAPI
1492 RsaGetPublicKeyFromX509 (
1493 IN CONST UINT8 *Cert,
1494 IN UINTN CertSize,
1495 OUT VOID **RsaContext
1496 );
1497
1498 /**
1499 Retrieve the subject bytes from one X.509 certificate.
1500
1501 If Cert is NULL, then return FALSE.
1502 If SubjectSize is NULL, then return FALSE.
1503 If this interface is not supported, then return FALSE.
1504
1505 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1506 @param[in] CertSize Size of the X509 certificate in bytes.
1507 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1508 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1509 and the size of buffer returned CertSubject on output.
1510
1511 @retval TRUE The certificate subject retrieved successfully.
1512 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1513 The SubjectSize will be updated with the required size.
1514 @retval FALSE This interface is not supported.
1515
1516 **/
1517 BOOLEAN
1518 EFIAPI
1519 X509GetSubjectName (
1520 IN CONST UINT8 *Cert,
1521 IN UINTN CertSize,
1522 OUT UINT8 *CertSubject,
1523 IN OUT UINTN *SubjectSize
1524 );
1525
1526 /**
1527 Retrieve the common name (CN) string from one X.509 certificate.
1528
1529 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1530 @param[in] CertSize Size of the X509 certificate in bytes.
1531 @param[out] CommonName Buffer to contain the retrieved certificate common
1532 name string (UTF8). At most CommonNameSize bytes will be
1533 written and the string will be null terminated. May be
1534 NULL in order to determine the size buffer needed.
1535 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1536 and the size of buffer returned CommonName on output.
1537 If CommonName is NULL then the amount of space needed
1538 in buffer (including the final null) is returned.
1539
1540 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1541 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1542 If CommonNameSize is NULL.
1543 If CommonName is not NULL and *CommonNameSize is 0.
1544 If Certificate is invalid.
1545 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1546 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1547 (including the final null) is returned in the
1548 CommonNameSize parameter.
1549 @retval RETURN_UNSUPPORTED The operation is not supported.
1550
1551 **/
1552 RETURN_STATUS
1553 EFIAPI
1554 X509GetCommonName (
1555 IN CONST UINT8 *Cert,
1556 IN UINTN CertSize,
1557 OUT CHAR8 *CommonName OPTIONAL,
1558 IN OUT UINTN *CommonNameSize
1559 );
1560
1561 /**
1562 Retrieve the organization name (O) string from one X.509 certificate.
1563
1564 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1565 @param[in] CertSize Size of the X509 certificate in bytes.
1566 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1567 name string. At most NameBufferSize bytes will be
1568 written and the string will be null terminated. May be
1569 NULL in order to determine the size buffer needed.
1570 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1571 and the size of buffer returned Name on output.
1572 If NameBuffer is NULL then the amount of space needed
1573 in buffer (including the final null) is returned.
1574
1575 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1576 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1577 If NameBufferSize is NULL.
1578 If NameBuffer is not NULL and *CommonNameSize is 0.
1579 If Certificate is invalid.
1580 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1581 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1582 (including the final null) is returned in the
1583 CommonNameSize parameter.
1584 @retval RETURN_UNSUPPORTED The operation is not supported.
1585
1586 **/
1587 RETURN_STATUS
1588 EFIAPI
1589 X509GetOrganizationName (
1590 IN CONST UINT8 *Cert,
1591 IN UINTN CertSize,
1592 OUT CHAR8 *NameBuffer OPTIONAL,
1593 IN OUT UINTN *NameBufferSize
1594 );
1595
1596 /**
1597 Verify one X509 certificate was issued by the trusted CA.
1598
1599 If Cert is NULL, then return FALSE.
1600 If CACert is NULL, then return FALSE.
1601 If this interface is not supported, then return FALSE.
1602
1603 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1604 @param[in] CertSize Size of the X509 certificate in bytes.
1605 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1606 @param[in] CACertSize Size of the CA Certificate in bytes.
1607
1608 @retval TRUE The certificate was issued by the trusted CA.
1609 @retval FALSE Invalid certificate or the certificate was not issued by the given
1610 trusted CA.
1611 @retval FALSE This interface is not supported.
1612
1613 **/
1614 BOOLEAN
1615 EFIAPI
1616 X509VerifyCert (
1617 IN CONST UINT8 *Cert,
1618 IN UINTN CertSize,
1619 IN CONST UINT8 *CACert,
1620 IN UINTN CACertSize
1621 );
1622
1623 /**
1624 Construct a X509 object from DER-encoded certificate data.
1625
1626 If Cert is NULL, then return FALSE.
1627 If SingleX509Cert is NULL, then return FALSE.
1628 If this interface is not supported, then return FALSE.
1629
1630 @param[in] Cert Pointer to the DER-encoded certificate data.
1631 @param[in] CertSize The size of certificate data in bytes.
1632 @param[out] SingleX509Cert The generated X509 object.
1633
1634 @retval TRUE The X509 object generation succeeded.
1635 @retval FALSE The operation failed.
1636 @retval FALSE This interface is not supported.
1637
1638 **/
1639 BOOLEAN
1640 EFIAPI
1641 X509ConstructCertificate (
1642 IN CONST UINT8 *Cert,
1643 IN UINTN CertSize,
1644 OUT UINT8 **SingleX509Cert
1645 );
1646
1647 /**
1648 Construct a X509 stack object from a list of DER-encoded certificate data.
1649
1650 If X509Stack is NULL, then return FALSE.
1651 If this interface is not supported, then return FALSE.
1652
1653 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1654 On output, pointer to the X509 stack object with new
1655 inserted X509 certificate.
1656 @param[in] Args VA_LIST marker for the variable argument list.
1657 A list of DER-encoded single certificate data followed
1658 by certificate size. A NULL terminates the list. The
1659 pairs are the arguments to X509ConstructCertificate().
1660
1661 @retval TRUE The X509 stack construction succeeded.
1662 @retval FALSE The construction operation failed.
1663 @retval FALSE This interface is not supported.
1664
1665 **/
1666 BOOLEAN
1667 EFIAPI
1668 X509ConstructCertificateStackV (
1669 IN OUT UINT8 **X509Stack,
1670 IN VA_LIST Args
1671 );
1672
1673 /**
1674 Construct a X509 stack object from a list of DER-encoded certificate data.
1675
1676 If X509Stack is NULL, then return FALSE.
1677 If this interface is not supported, then return FALSE.
1678
1679 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1680 On output, pointer to the X509 stack object with new
1681 inserted X509 certificate.
1682 @param ... A list of DER-encoded single certificate data followed
1683 by certificate size. A NULL terminates the list. The
1684 pairs are the arguments to X509ConstructCertificate().
1685
1686 @retval TRUE The X509 stack construction succeeded.
1687 @retval FALSE The construction operation failed.
1688 @retval FALSE This interface is not supported.
1689
1690 **/
1691 BOOLEAN
1692 EFIAPI
1693 X509ConstructCertificateStack (
1694 IN OUT UINT8 **X509Stack,
1695 ...
1696 );
1697
1698 /**
1699 Release the specified X509 object.
1700
1701 If the interface is not supported, then ASSERT().
1702
1703 @param[in] X509Cert Pointer to the X509 object to be released.
1704
1705 **/
1706 VOID
1707 EFIAPI
1708 X509Free (
1709 IN VOID *X509Cert
1710 );
1711
1712 /**
1713 Release the specified X509 stack object.
1714
1715 If the interface is not supported, then ASSERT().
1716
1717 @param[in] X509Stack Pointer to the X509 stack object to be released.
1718
1719 **/
1720 VOID
1721 EFIAPI
1722 X509StackFree (
1723 IN VOID *X509Stack
1724 );
1725
1726 /**
1727 Retrieve the TBSCertificate from one given X.509 certificate.
1728
1729 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
1730 @param[in] CertSize Size of the X509 certificate in bytes.
1731 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
1732 @param[out] TBSCertSize Size of the TBS certificate in bytes.
1733
1734 If Cert is NULL, then return FALSE.
1735 If TBSCert is NULL, then return FALSE.
1736 If TBSCertSize is NULL, then return FALSE.
1737 If this interface is not supported, then return FALSE.
1738
1739 @retval TRUE The TBSCertificate was retrieved successfully.
1740 @retval FALSE Invalid X.509 certificate.
1741
1742 **/
1743 BOOLEAN
1744 EFIAPI
1745 X509GetTBSCert (
1746 IN CONST UINT8 *Cert,
1747 IN UINTN CertSize,
1748 OUT UINT8 **TBSCert,
1749 OUT UINTN *TBSCertSize
1750 );
1751
1752 /**
1753 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
1754 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
1755
1756 If Password or Salt or OutKey is NULL, then return FALSE.
1757 If the hash algorithm could not be determined, then return FALSE.
1758 If this interface is not supported, then return FALSE.
1759
1760 @param[in] PasswordLength Length of input password in bytes.
1761 @param[in] Password Pointer to the array for the password.
1762 @param[in] SaltLength Size of the Salt in bytes.
1763 @param[in] Salt Pointer to the Salt.
1764 @param[in] IterationCount Number of iterations to perform. Its value should be
1765 greater than or equal to 1.
1766 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
1767 NOTE: DigestSize will be used to determine the hash algorithm.
1768 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
1769 @param[in] KeyLength Size of the derived key buffer in bytes.
1770 @param[out] OutKey Pointer to the output derived key buffer.
1771
1772 @retval TRUE A key was derived successfully.
1773 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
1774 @retval FALSE The hash algorithm could not be determined from the digest size.
1775 @retval FALSE The key derivation operation failed.
1776 @retval FALSE This interface is not supported.
1777
1778 **/
1779 BOOLEAN
1780 EFIAPI
1781 Pkcs5HashPassword (
1782 IN UINTN PasswordLength,
1783 IN CONST CHAR8 *Password,
1784 IN UINTN SaltLength,
1785 IN CONST UINT8 *Salt,
1786 IN UINTN IterationCount,
1787 IN UINTN DigestSize,
1788 IN UINTN KeyLength,
1789 OUT UINT8 *OutKey
1790 );
1791
1792 /**
1793 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
1794 encrypted message in a newly allocated buffer.
1795
1796 Things that can cause a failure include:
1797 - X509 key size does not match any known key size.
1798 - Fail to parse X509 certificate.
1799 - Fail to allocate an intermediate buffer.
1800 - Null pointer provided for a non-optional parameter.
1801 - Data size is too large for the provided key size (max size is a function of key size
1802 and hash digest size).
1803
1804 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
1805 will be used to encrypt the data.
1806 @param[in] PublicKeySize Size of the X509 cert buffer.
1807 @param[in] InData Data to be encrypted.
1808 @param[in] InDataSize Size of the data buffer.
1809 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
1810 to be used when initializing the PRNG. NULL otherwise.
1811 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
1812 0 otherwise.
1813 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
1814 message.
1815 @param[out] EncryptedDataSize Size of the encrypted message buffer.
1816
1817 @retval TRUE Encryption was successful.
1818 @retval FALSE Encryption failed.
1819
1820 **/
1821 BOOLEAN
1822 EFIAPI
1823 Pkcs1v2Encrypt (
1824 IN CONST UINT8 *PublicKey,
1825 IN UINTN PublicKeySize,
1826 IN UINT8 *InData,
1827 IN UINTN InDataSize,
1828 IN CONST UINT8 *PrngSeed OPTIONAL,
1829 IN UINTN PrngSeedSize OPTIONAL,
1830 OUT UINT8 **EncryptedData,
1831 OUT UINTN *EncryptedDataSize
1832 );
1833
1834 /**
1835 The 3rd parameter of Pkcs7GetSigners will return all embedded
1836 X.509 certificate in one given PKCS7 signature. The format is:
1837 //
1838 // UINT8 CertNumber;
1839 // UINT32 Cert1Length;
1840 // UINT8 Cert1[];
1841 // UINT32 Cert2Length;
1842 // UINT8 Cert2[];
1843 // ...
1844 // UINT32 CertnLength;
1845 // UINT8 Certn[];
1846 //
1847
1848 The two following C-structure are used for parsing CertStack more clearly.
1849 **/
1850 #pragma pack(1)
1851
1852 typedef struct {
1853 UINT32 CertDataLength; // The length in bytes of X.509 certificate.
1854 UINT8 CertDataBuffer[0]; // The X.509 certificate content (DER).
1855 } EFI_CERT_DATA;
1856
1857 typedef struct {
1858 UINT8 CertNumber; // Number of X.509 certificate.
1859 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
1860 } EFI_CERT_STACK;
1861
1862 #pragma pack()
1863
1864 /**
1865 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
1866 Cryptographic Message Syntax Standard". The input signed data could be wrapped
1867 in a ContentInfo structure.
1868
1869 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
1870 return FALSE. If P7Length overflow, then return FALSE.
1871 If this interface is not supported, then return FALSE.
1872
1873 @param[in] P7Data Pointer to the PKCS#7 message to verify.
1874 @param[in] P7Length Length of the PKCS#7 message in bytes.
1875 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
1876 It's caller's responsibility to free the buffer with
1877 Pkcs7FreeSigners().
1878 This data structure is EFI_CERT_STACK type.
1879 @param[out] StackLength Length of signer's certificates in bytes.
1880 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
1881 It's caller's responsibility to free the buffer with
1882 Pkcs7FreeSigners().
1883 @param[out] CertLength Length of the trusted certificate in bytes.
1884
1885 @retval TRUE The operation is finished successfully.
1886 @retval FALSE Error occurs during the operation.
1887 @retval FALSE This interface is not supported.
1888
1889 **/
1890 BOOLEAN
1891 EFIAPI
1892 Pkcs7GetSigners (
1893 IN CONST UINT8 *P7Data,
1894 IN UINTN P7Length,
1895 OUT UINT8 **CertStack,
1896 OUT UINTN *StackLength,
1897 OUT UINT8 **TrustedCert,
1898 OUT UINTN *CertLength
1899 );
1900
1901 /**
1902 Wrap function to use free() to free allocated memory for certificates.
1903
1904 If this interface is not supported, then ASSERT().
1905
1906 @param[in] Certs Pointer to the certificates to be freed.
1907
1908 **/
1909 VOID
1910 EFIAPI
1911 Pkcs7FreeSigners (
1912 IN UINT8 *Certs
1913 );
1914
1915 /**
1916 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
1917 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
1918 unchained to the signer's certificates.
1919 The input signed data could be wrapped in a ContentInfo structure.
1920
1921 @param[in] P7Data Pointer to the PKCS#7 message.
1922 @param[in] P7Length Length of the PKCS#7 message in bytes.
1923 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
1924 certificate. It's caller's responsibility to free the buffer
1925 with Pkcs7FreeSigners().
1926 This data structure is EFI_CERT_STACK type.
1927 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
1928 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
1929 responsibility to free the buffer with Pkcs7FreeSigners().
1930 This data structure is EFI_CERT_STACK type.
1931 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
1932
1933 @retval TRUE The operation is finished successfully.
1934 @retval FALSE Error occurs during the operation.
1935
1936 **/
1937 BOOLEAN
1938 EFIAPI
1939 Pkcs7GetCertificatesList (
1940 IN CONST UINT8 *P7Data,
1941 IN UINTN P7Length,
1942 OUT UINT8 **SignerChainCerts,
1943 OUT UINTN *ChainLength,
1944 OUT UINT8 **UnchainCerts,
1945 OUT UINTN *UnchainLength
1946 );
1947
1948 /**
1949 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
1950 Syntax Standard, version 1.5". This interface is only intended to be used for
1951 application to perform PKCS#7 functionality validation.
1952
1953 If this interface is not supported, then return FALSE.
1954
1955 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
1956 data signing.
1957 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
1958 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
1959 key data.
1960 @param[in] InData Pointer to the content to be signed.
1961 @param[in] InDataSize Size of InData in bytes.
1962 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
1963 @param[in] OtherCerts Pointer to an optional additional set of certificates to
1964 include in the PKCS#7 signedData (e.g. any intermediate
1965 CAs in the chain).
1966 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
1967 responsibility to free the buffer with FreePool().
1968 @param[out] SignedDataSize Size of SignedData in bytes.
1969
1970 @retval TRUE PKCS#7 data signing succeeded.
1971 @retval FALSE PKCS#7 data signing failed.
1972 @retval FALSE This interface is not supported.
1973
1974 **/
1975 BOOLEAN
1976 EFIAPI
1977 Pkcs7Sign (
1978 IN CONST UINT8 *PrivateKey,
1979 IN UINTN PrivateKeySize,
1980 IN CONST UINT8 *KeyPassword,
1981 IN UINT8 *InData,
1982 IN UINTN InDataSize,
1983 IN UINT8 *SignCert,
1984 IN UINT8 *OtherCerts OPTIONAL,
1985 OUT UINT8 **SignedData,
1986 OUT UINTN *SignedDataSize
1987 );
1988
1989 /**
1990 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
1991 Cryptographic Message Syntax Standard". The input signed data could be wrapped
1992 in a ContentInfo structure.
1993
1994 If P7Data, TrustedCert or InData is NULL, then return FALSE.
1995 If P7Length, CertLength or DataLength overflow, then return FALSE.
1996 If this interface is not supported, then return FALSE.
1997
1998 @param[in] P7Data Pointer to the PKCS#7 message to verify.
1999 @param[in] P7Length Length of the PKCS#7 message in bytes.
2000 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2001 is used for certificate chain verification.
2002 @param[in] CertLength Length of the trusted certificate in bytes.
2003 @param[in] InData Pointer to the content to be verified.
2004 @param[in] DataLength Length of InData in bytes.
2005
2006 @retval TRUE The specified PKCS#7 signed data is valid.
2007 @retval FALSE Invalid PKCS#7 signed data.
2008 @retval FALSE This interface is not supported.
2009
2010 **/
2011 BOOLEAN
2012 EFIAPI
2013 Pkcs7Verify (
2014 IN CONST UINT8 *P7Data,
2015 IN UINTN P7Length,
2016 IN CONST UINT8 *TrustedCert,
2017 IN UINTN CertLength,
2018 IN CONST UINT8 *InData,
2019 IN UINTN DataLength
2020 );
2021
2022 /**
2023 This function receives a PKCS7 formatted signature, and then verifies that
2024 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2025 leaf signing certificate.
2026 Note that this function does not validate the certificate chain.
2027
2028 Applications for custom EKU's are quite flexible. For example, a policy EKU
2029 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2030 certificate issued might also contain this EKU, thus constraining the
2031 sub-ordinate certificate. Other applications might allow a certificate
2032 embedded in a device to specify that other Object Identifiers (OIDs) are
2033 present which contains binary data specifying custom capabilities that
2034 the device is able to do.
2035
2036 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2037 containing the content block with both the signature,
2038 the signer's certificate, and any necessary intermediate
2039 certificates.
2040 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2041 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2042 required EKUs that must be present in the signature.
2043 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2044 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2045 must be present in the leaf signer. If it is
2046 FALSE, then we will succeed if we find any
2047 of the specified EKU's.
2048
2049 @retval EFI_SUCCESS The required EKUs were found in the signature.
2050 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2051 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2052
2053 **/
2054 RETURN_STATUS
2055 EFIAPI
2056 VerifyEKUsInPkcs7Signature (
2057 IN CONST UINT8 *Pkcs7Signature,
2058 IN CONST UINT32 SignatureSize,
2059 IN CONST CHAR8 *RequiredEKUs[],
2060 IN CONST UINT32 RequiredEKUsSize,
2061 IN BOOLEAN RequireAllPresent
2062 );
2063
2064 /**
2065 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2066 data could be wrapped in a ContentInfo structure.
2067
2068 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2069 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2070
2071 Caution: This function may receive untrusted input. So this function will do
2072 basic check for PKCS#7 data structure.
2073
2074 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2075 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2076 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2077 It's caller's responsibility to free the buffer with FreePool().
2078 @param[out] ContentSize The size of the extracted content in bytes.
2079
2080 @retval TRUE The P7Data was correctly formatted for processing.
2081 @retval FALSE The P7Data was not correctly formatted for processing.
2082
2083 **/
2084 BOOLEAN
2085 EFIAPI
2086 Pkcs7GetAttachedContent (
2087 IN CONST UINT8 *P7Data,
2088 IN UINTN P7Length,
2089 OUT VOID **Content,
2090 OUT UINTN *ContentSize
2091 );
2092
2093 /**
2094 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2095 Authenticode Portable Executable Signature Format".
2096
2097 If AuthData is NULL, then return FALSE.
2098 If ImageHash is NULL, then return FALSE.
2099 If this interface is not supported, then return FALSE.
2100
2101 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2102 PE/COFF image to be verified.
2103 @param[in] DataSize Size of the Authenticode Signature in bytes.
2104 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2105 is used for certificate chain verification.
2106 @param[in] CertSize Size of the trusted certificate in bytes.
2107 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2108 for calculating the image hash value is described in Authenticode
2109 specification.
2110 @param[in] HashSize Size of Image hash value in bytes.
2111
2112 @retval TRUE The specified Authenticode Signature is valid.
2113 @retval FALSE Invalid Authenticode Signature.
2114 @retval FALSE This interface is not supported.
2115
2116 **/
2117 BOOLEAN
2118 EFIAPI
2119 AuthenticodeVerify (
2120 IN CONST UINT8 *AuthData,
2121 IN UINTN DataSize,
2122 IN CONST UINT8 *TrustedCert,
2123 IN UINTN CertSize,
2124 IN CONST UINT8 *ImageHash,
2125 IN UINTN HashSize
2126 );
2127
2128 /**
2129 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2130 signature.
2131
2132 If AuthData is NULL, then return FALSE.
2133 If this interface is not supported, then return FALSE.
2134
2135 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2136 PE/COFF image to be verified.
2137 @param[in] DataSize Size of the Authenticode Signature in bytes.
2138 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2139 is used for TSA certificate chain verification.
2140 @param[in] CertSize Size of the trusted certificate in bytes.
2141 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2142 signature is valid.
2143
2144 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2145 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2146
2147 **/
2148 BOOLEAN
2149 EFIAPI
2150 ImageTimestampVerify (
2151 IN CONST UINT8 *AuthData,
2152 IN UINTN DataSize,
2153 IN CONST UINT8 *TsaCert,
2154 IN UINTN CertSize,
2155 OUT EFI_TIME *SigningTime
2156 );
2157
2158 // =====================================================================================
2159 // DH Key Exchange Primitive
2160 // =====================================================================================
2161
2162 /**
2163 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2164
2165 @return Pointer to the Diffie-Hellman Context that has been initialized.
2166 If the allocations fails, DhNew() returns NULL.
2167 If the interface is not supported, DhNew() returns NULL.
2168
2169 **/
2170 VOID *
2171 EFIAPI
2172 DhNew (
2173 VOID
2174 );
2175
2176 /**
2177 Release the specified DH context.
2178
2179 If the interface is not supported, then ASSERT().
2180
2181 @param[in] DhContext Pointer to the DH context to be released.
2182
2183 **/
2184 VOID
2185 EFIAPI
2186 DhFree (
2187 IN VOID *DhContext
2188 );
2189
2190 /**
2191 Generates DH parameter.
2192
2193 Given generator g, and length of prime number p in bits, this function generates p,
2194 and sets DH context according to value of g and p.
2195
2196 Before this function can be invoked, pseudorandom number generator must be correctly
2197 initialized by RandomSeed().
2198
2199 If DhContext is NULL, then return FALSE.
2200 If Prime is NULL, then return FALSE.
2201 If this interface is not supported, then return FALSE.
2202
2203 @param[in, out] DhContext Pointer to the DH context.
2204 @param[in] Generator Value of generator.
2205 @param[in] PrimeLength Length in bits of prime to be generated.
2206 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2207
2208 @retval TRUE DH parameter generation succeeded.
2209 @retval FALSE Value of Generator is not supported.
2210 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2211 @retval FALSE This interface is not supported.
2212
2213 **/
2214 BOOLEAN
2215 EFIAPI
2216 DhGenerateParameter (
2217 IN OUT VOID *DhContext,
2218 IN UINTN Generator,
2219 IN UINTN PrimeLength,
2220 OUT UINT8 *Prime
2221 );
2222
2223 /**
2224 Sets generator and prime parameters for DH.
2225
2226 Given generator g, and prime number p, this function and sets DH
2227 context accordingly.
2228
2229 If DhContext is NULL, then return FALSE.
2230 If Prime is NULL, then return FALSE.
2231 If this interface is not supported, then return FALSE.
2232
2233 @param[in, out] DhContext Pointer to the DH context.
2234 @param[in] Generator Value of generator.
2235 @param[in] PrimeLength Length in bits of prime to be generated.
2236 @param[in] Prime Pointer to the prime number.
2237
2238 @retval TRUE DH parameter setting succeeded.
2239 @retval FALSE Value of Generator is not supported.
2240 @retval FALSE Value of Generator is not suitable for the Prime.
2241 @retval FALSE Value of Prime is not a prime number.
2242 @retval FALSE Value of Prime is not a safe prime number.
2243 @retval FALSE This interface is not supported.
2244
2245 **/
2246 BOOLEAN
2247 EFIAPI
2248 DhSetParameter (
2249 IN OUT VOID *DhContext,
2250 IN UINTN Generator,
2251 IN UINTN PrimeLength,
2252 IN CONST UINT8 *Prime
2253 );
2254
2255 /**
2256 Generates DH public key.
2257
2258 This function generates random secret exponent, and computes the public key, which is
2259 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2260 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2261 PublicKeySize is set to the required buffer size to obtain the public key.
2262
2263 If DhContext is NULL, then return FALSE.
2264 If PublicKeySize is NULL, then return FALSE.
2265 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2266 If this interface is not supported, then return FALSE.
2267
2268 @param[in, out] DhContext Pointer to the DH context.
2269 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2270 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2271 On output, the size of data returned in PublicKey buffer in bytes.
2272
2273 @retval TRUE DH public key generation succeeded.
2274 @retval FALSE DH public key generation failed.
2275 @retval FALSE PublicKeySize is not large enough.
2276 @retval FALSE This interface is not supported.
2277
2278 **/
2279 BOOLEAN
2280 EFIAPI
2281 DhGenerateKey (
2282 IN OUT VOID *DhContext,
2283 OUT UINT8 *PublicKey,
2284 IN OUT UINTN *PublicKeySize
2285 );
2286
2287 /**
2288 Computes exchanged common key.
2289
2290 Given peer's public key, this function computes the exchanged common key, based on its own
2291 context including value of prime modulus and random secret exponent.
2292
2293 If DhContext is NULL, then return FALSE.
2294 If PeerPublicKey is NULL, then return FALSE.
2295 If KeySize is NULL, then return FALSE.
2296 If Key is NULL, then return FALSE.
2297 If KeySize is not large enough, then return FALSE.
2298 If this interface is not supported, then return FALSE.
2299
2300 @param[in, out] DhContext Pointer to the DH context.
2301 @param[in] PeerPublicKey Pointer to the peer's public key.
2302 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2303 @param[out] Key Pointer to the buffer to receive generated key.
2304 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2305 On output, the size of data returned in Key buffer in bytes.
2306
2307 @retval TRUE DH exchanged key generation succeeded.
2308 @retval FALSE DH exchanged key generation failed.
2309 @retval FALSE KeySize is not large enough.
2310 @retval FALSE This interface is not supported.
2311
2312 **/
2313 BOOLEAN
2314 EFIAPI
2315 DhComputeKey (
2316 IN OUT VOID *DhContext,
2317 IN CONST UINT8 *PeerPublicKey,
2318 IN UINTN PeerPublicKeySize,
2319 OUT UINT8 *Key,
2320 IN OUT UINTN *KeySize
2321 );
2322
2323 // =====================================================================================
2324 // Pseudo-Random Generation Primitive
2325 // =====================================================================================
2326
2327 /**
2328 Sets up the seed value for the pseudorandom number generator.
2329
2330 This function sets up the seed value for the pseudorandom number generator.
2331 If Seed is not NULL, then the seed passed in is used.
2332 If Seed is NULL, then default seed is used.
2333 If this interface is not supported, then return FALSE.
2334
2335 @param[in] Seed Pointer to seed value.
2336 If NULL, default seed is used.
2337 @param[in] SeedSize Size of seed value.
2338 If Seed is NULL, this parameter is ignored.
2339
2340 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2341 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2342 @retval FALSE This interface is not supported.
2343
2344 **/
2345 BOOLEAN
2346 EFIAPI
2347 RandomSeed (
2348 IN CONST UINT8 *Seed OPTIONAL,
2349 IN UINTN SeedSize
2350 );
2351
2352 /**
2353 Generates a pseudorandom byte stream of the specified size.
2354
2355 If Output is NULL, then return FALSE.
2356 If this interface is not supported, then return FALSE.
2357
2358 @param[out] Output Pointer to buffer to receive random value.
2359 @param[in] Size Size of random bytes to generate.
2360
2361 @retval TRUE Pseudorandom byte stream generated successfully.
2362 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2363 @retval FALSE This interface is not supported.
2364
2365 **/
2366 BOOLEAN
2367 EFIAPI
2368 RandomBytes (
2369 OUT UINT8 *Output,
2370 IN UINTN Size
2371 );
2372
2373 // =====================================================================================
2374 // Key Derivation Function Primitive
2375 // =====================================================================================
2376
2377 /**
2378 Derive key data using HMAC-SHA256 based KDF.
2379
2380 @param[in] Key Pointer to the user-supplied key.
2381 @param[in] KeySize Key size in bytes.
2382 @param[in] Salt Pointer to the salt(non-secret) value.
2383 @param[in] SaltSize Salt size in bytes.
2384 @param[in] Info Pointer to the application specific info.
2385 @param[in] InfoSize Info size in bytes.
2386 @param[out] Out Pointer to buffer to receive hkdf value.
2387 @param[in] OutSize Size of hkdf bytes to generate.
2388
2389 @retval TRUE Hkdf generated successfully.
2390 @retval FALSE Hkdf generation failed.
2391
2392 **/
2393 BOOLEAN
2394 EFIAPI
2395 HkdfSha256ExtractAndExpand (
2396 IN CONST UINT8 *Key,
2397 IN UINTN KeySize,
2398 IN CONST UINT8 *Salt,
2399 IN UINTN SaltSize,
2400 IN CONST UINT8 *Info,
2401 IN UINTN InfoSize,
2402 OUT UINT8 *Out,
2403 IN UINTN OutSize
2404 );
2405
2406 #endif // __BASE_CRYPT_LIB_H__