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