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