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