]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg: Add BigNum support
[mirror_edk2.git] / CryptoPkg / Include / Library / BaseCryptLib.h
1 /** @file
2 Defines base cryptographic library APIs.
3 The Base Cryptographic Library provides implementations of basic cryptography
4 primitives (Hash Serials, HMAC, RSA, Diffie-Hellman, etc) for UEFI security
5 functionality enabling.
6
7 Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #ifndef __BASE_CRYPT_LIB_H__
13 #define __BASE_CRYPT_LIB_H__
14
15 #include <Uefi/UefiBaseType.h>
16
17 ///
18 /// MD5 digest size in bytes
19 ///
20 #define MD5_DIGEST_SIZE 16
21
22 ///
23 /// SHA-1 digest size in bytes.
24 ///
25 #define SHA1_DIGEST_SIZE 20
26
27 ///
28 /// SHA-256 digest size in bytes
29 ///
30 #define SHA256_DIGEST_SIZE 32
31
32 ///
33 /// SHA-384 digest size in bytes
34 ///
35 #define SHA384_DIGEST_SIZE 48
36
37 ///
38 /// SHA-512 digest size in bytes
39 ///
40 #define SHA512_DIGEST_SIZE 64
41
42 ///
43 /// SM3 digest size in bytes
44 ///
45 #define SM3_256_DIGEST_SIZE 32
46
47 ///
48 /// TDES block size in bytes
49 ///
50 #define TDES_BLOCK_SIZE 8
51
52 ///
53 /// AES block size in bytes
54 ///
55 #define AES_BLOCK_SIZE 16
56
57 ///
58 /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
59 ///
60 typedef enum {
61 RsaKeyN, ///< RSA public Modulus (N)
62 RsaKeyE, ///< RSA Public exponent (e)
63 RsaKeyD, ///< RSA Private exponent (d)
64 RsaKeyP, ///< RSA secret prime factor of Modulus (p)
65 RsaKeyQ, ///< RSA secret prime factor of Modules (q)
66 RsaKeyDp, ///< p's CRT exponent (== d mod (p - 1))
67 RsaKeyDq, ///< q's CRT exponent (== d mod (q - 1))
68 RsaKeyQInv ///< The CRT coefficient (== 1/q mod p)
69 } RSA_KEY_TAG;
70
71 // =====================================================================================
72 // One-Way Cryptographic Hash Primitives
73 // =====================================================================================
74
75 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES
76
77 /**
78 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
79
80 If this interface is not supported, then return zero.
81
82 @return The size, in bytes, of the context buffer required for MD5 hash operations.
83 @retval 0 This interface is not supported.
84
85 **/
86 UINTN
87 EFIAPI
88 Md5GetContextSize (
89 VOID
90 );
91
92 /**
93 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
94 subsequent use.
95
96 If Md5Context is NULL, then return FALSE.
97 If this interface is not supported, then return FALSE.
98
99 @param[out] Md5Context Pointer to MD5 context being initialized.
100
101 @retval TRUE MD5 context initialization succeeded.
102 @retval FALSE MD5 context initialization failed.
103 @retval FALSE This interface is not supported.
104
105 **/
106 BOOLEAN
107 EFIAPI
108 Md5Init (
109 OUT VOID *Md5Context
110 );
111
112 /**
113 Makes a copy of an existing MD5 context.
114
115 If Md5Context is NULL, then return FALSE.
116 If NewMd5Context is NULL, then return FALSE.
117 If this interface is not supported, then return FALSE.
118
119 @param[in] Md5Context Pointer to MD5 context being copied.
120 @param[out] NewMd5Context Pointer to new MD5 context.
121
122 @retval TRUE MD5 context copy succeeded.
123 @retval FALSE MD5 context copy failed.
124 @retval FALSE This interface is not supported.
125
126 **/
127 BOOLEAN
128 EFIAPI
129 Md5Duplicate (
130 IN CONST VOID *Md5Context,
131 OUT VOID *NewMd5Context
132 );
133
134 /**
135 Digests the input data and updates MD5 context.
136
137 This function performs MD5 digest on a data buffer of the specified size.
138 It can be called multiple times to compute the digest of long or discontinuous data streams.
139 MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
140 by Md5Final(). Behavior with invalid context is undefined.
141
142 If Md5Context is NULL, then return FALSE.
143 If this interface is not supported, then return FALSE.
144
145 @param[in, out] Md5Context Pointer to the MD5 context.
146 @param[in] Data Pointer to the buffer containing the data to be hashed.
147 @param[in] DataSize Size of Data buffer in bytes.
148
149 @retval TRUE MD5 data digest succeeded.
150 @retval FALSE MD5 data digest failed.
151 @retval FALSE This interface is not supported.
152
153 **/
154 BOOLEAN
155 EFIAPI
156 Md5Update (
157 IN OUT VOID *Md5Context,
158 IN CONST VOID *Data,
159 IN UINTN DataSize
160 );
161
162 /**
163 Completes computation of the MD5 digest value.
164
165 This function completes MD5 hash computation and retrieves the digest value into
166 the specified memory. After this function has been called, the MD5 context cannot
167 be used again.
168 MD5 context should be already correctly initialized by Md5Init(), and should not be
169 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
170
171 If Md5Context is NULL, then return FALSE.
172 If HashValue is NULL, then return FALSE.
173 If this interface is not supported, then return FALSE.
174
175 @param[in, out] Md5Context Pointer to the MD5 context.
176 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
177 value (16 bytes).
178
179 @retval TRUE MD5 digest computation succeeded.
180 @retval FALSE MD5 digest computation failed.
181 @retval FALSE This interface is not supported.
182
183 **/
184 BOOLEAN
185 EFIAPI
186 Md5Final (
187 IN OUT VOID *Md5Context,
188 OUT UINT8 *HashValue
189 );
190
191 /**
192 Computes the MD5 message digest of a input data buffer.
193
194 This function performs the MD5 message digest of a given data buffer, and places
195 the digest value into the specified memory.
196
197 If this interface is not supported, then return FALSE.
198
199 @param[in] Data Pointer to the buffer containing the data to be hashed.
200 @param[in] DataSize Size of Data buffer in bytes.
201 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
202 value (16 bytes).
203
204 @retval TRUE MD5 digest computation succeeded.
205 @retval FALSE MD5 digest computation failed.
206 @retval FALSE This interface is not supported.
207
208 **/
209 BOOLEAN
210 EFIAPI
211 Md5HashAll (
212 IN CONST VOID *Data,
213 IN UINTN DataSize,
214 OUT UINT8 *HashValue
215 );
216
217 #endif
218
219 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
220
221 /**
222 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
223
224 If this interface is not supported, then return zero.
225
226 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
227 @retval 0 This interface is not supported.
228
229 **/
230 UINTN
231 EFIAPI
232 Sha1GetContextSize (
233 VOID
234 );
235
236 /**
237 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
238 subsequent use.
239
240 If Sha1Context is NULL, then return FALSE.
241 If this interface is not supported, then return FALSE.
242
243 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
244
245 @retval TRUE SHA-1 context initialization succeeded.
246 @retval FALSE SHA-1 context initialization failed.
247 @retval FALSE This interface is not supported.
248
249 **/
250 BOOLEAN
251 EFIAPI
252 Sha1Init (
253 OUT VOID *Sha1Context
254 );
255
256 /**
257 Makes a copy of an existing SHA-1 context.
258
259 If Sha1Context is NULL, then return FALSE.
260 If NewSha1Context is NULL, then return FALSE.
261 If this interface is not supported, then return FALSE.
262
263 @param[in] Sha1Context Pointer to SHA-1 context being copied.
264 @param[out] NewSha1Context Pointer to new SHA-1 context.
265
266 @retval TRUE SHA-1 context copy succeeded.
267 @retval FALSE SHA-1 context copy failed.
268 @retval FALSE This interface is not supported.
269
270 **/
271 BOOLEAN
272 EFIAPI
273 Sha1Duplicate (
274 IN CONST VOID *Sha1Context,
275 OUT VOID *NewSha1Context
276 );
277
278 /**
279 Digests the input data and updates SHA-1 context.
280
281 This function performs SHA-1 digest on a data buffer of the specified size.
282 It can be called multiple times to compute the digest of long or discontinuous data streams.
283 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
284 by Sha1Final(). Behavior with invalid context is undefined.
285
286 If Sha1Context is NULL, then return FALSE.
287 If this interface is not supported, then return FALSE.
288
289 @param[in, out] Sha1Context Pointer to the SHA-1 context.
290 @param[in] Data Pointer to the buffer containing the data to be hashed.
291 @param[in] DataSize Size of Data buffer in bytes.
292
293 @retval TRUE SHA-1 data digest succeeded.
294 @retval FALSE SHA-1 data digest failed.
295 @retval FALSE This interface is not supported.
296
297 **/
298 BOOLEAN
299 EFIAPI
300 Sha1Update (
301 IN OUT VOID *Sha1Context,
302 IN CONST VOID *Data,
303 IN UINTN DataSize
304 );
305
306 /**
307 Completes computation of the SHA-1 digest value.
308
309 This function completes SHA-1 hash computation and retrieves the digest value into
310 the specified memory. After this function has been called, the SHA-1 context cannot
311 be used again.
312 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
313 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
314
315 If Sha1Context is NULL, then return FALSE.
316 If HashValue is NULL, then return FALSE.
317 If this interface is not supported, then return FALSE.
318
319 @param[in, out] Sha1Context Pointer to the SHA-1 context.
320 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
321 value (20 bytes).
322
323 @retval TRUE SHA-1 digest computation succeeded.
324 @retval FALSE SHA-1 digest computation failed.
325 @retval FALSE This interface is not supported.
326
327 **/
328 BOOLEAN
329 EFIAPI
330 Sha1Final (
331 IN OUT VOID *Sha1Context,
332 OUT UINT8 *HashValue
333 );
334
335 /**
336 Computes the SHA-1 message digest of a input data buffer.
337
338 This function performs the SHA-1 message digest of a given data buffer, and places
339 the digest value into the specified memory.
340
341 If this interface is not supported, then return FALSE.
342
343 @param[in] Data Pointer to the buffer containing the data to be hashed.
344 @param[in] DataSize Size of Data buffer in bytes.
345 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
346 value (20 bytes).
347
348 @retval TRUE SHA-1 digest computation succeeded.
349 @retval FALSE SHA-1 digest computation failed.
350 @retval FALSE This interface is not supported.
351
352 **/
353 BOOLEAN
354 EFIAPI
355 Sha1HashAll (
356 IN CONST VOID *Data,
357 IN UINTN DataSize,
358 OUT UINT8 *HashValue
359 );
360
361 #endif
362
363 /**
364 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
365
366 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
367
368 **/
369 UINTN
370 EFIAPI
371 Sha256GetContextSize (
372 VOID
373 );
374
375 /**
376 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
377 subsequent use.
378
379 If Sha256Context is NULL, then return FALSE.
380
381 @param[out] Sha256Context Pointer to SHA-256 context being initialized.
382
383 @retval TRUE SHA-256 context initialization succeeded.
384 @retval FALSE SHA-256 context initialization failed.
385
386 **/
387 BOOLEAN
388 EFIAPI
389 Sha256Init (
390 OUT VOID *Sha256Context
391 );
392
393 /**
394 Makes a copy of an existing SHA-256 context.
395
396 If Sha256Context is NULL, then return FALSE.
397 If NewSha256Context is NULL, then return FALSE.
398 If this interface is not supported, then return FALSE.
399
400 @param[in] Sha256Context Pointer to SHA-256 context being copied.
401 @param[out] NewSha256Context Pointer to new SHA-256 context.
402
403 @retval TRUE SHA-256 context copy succeeded.
404 @retval FALSE SHA-256 context copy failed.
405 @retval FALSE This interface is not supported.
406
407 **/
408 BOOLEAN
409 EFIAPI
410 Sha256Duplicate (
411 IN CONST VOID *Sha256Context,
412 OUT VOID *NewSha256Context
413 );
414
415 /**
416 Digests the input data and updates SHA-256 context.
417
418 This function performs SHA-256 digest on a data buffer of the specified size.
419 It can be called multiple times to compute the digest of long or discontinuous data streams.
420 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
421 by Sha256Final(). Behavior with invalid context is undefined.
422
423 If Sha256Context is NULL, then return FALSE.
424
425 @param[in, out] Sha256Context Pointer to the SHA-256 context.
426 @param[in] Data Pointer to the buffer containing the data to be hashed.
427 @param[in] DataSize Size of Data buffer in bytes.
428
429 @retval TRUE SHA-256 data digest succeeded.
430 @retval FALSE SHA-256 data digest failed.
431
432 **/
433 BOOLEAN
434 EFIAPI
435 Sha256Update (
436 IN OUT VOID *Sha256Context,
437 IN CONST VOID *Data,
438 IN UINTN DataSize
439 );
440
441 /**
442 Completes computation of the SHA-256 digest value.
443
444 This function completes SHA-256 hash computation and retrieves the digest value into
445 the specified memory. After this function has been called, the SHA-256 context cannot
446 be used again.
447 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
448 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
449
450 If Sha256Context is NULL, then return FALSE.
451 If HashValue is NULL, then return FALSE.
452
453 @param[in, out] Sha256Context Pointer to the SHA-256 context.
454 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
455 value (32 bytes).
456
457 @retval TRUE SHA-256 digest computation succeeded.
458 @retval FALSE SHA-256 digest computation failed.
459
460 **/
461 BOOLEAN
462 EFIAPI
463 Sha256Final (
464 IN OUT VOID *Sha256Context,
465 OUT UINT8 *HashValue
466 );
467
468 /**
469 Computes the SHA-256 message digest of a input data buffer.
470
471 This function performs the SHA-256 message digest of a given data buffer, and places
472 the digest value into the specified memory.
473
474 If this interface is not supported, then return FALSE.
475
476 @param[in] Data Pointer to the buffer containing the data to be hashed.
477 @param[in] DataSize Size of Data buffer in bytes.
478 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
479 value (32 bytes).
480
481 @retval TRUE SHA-256 digest computation succeeded.
482 @retval FALSE SHA-256 digest computation failed.
483 @retval FALSE This interface is not supported.
484
485 **/
486 BOOLEAN
487 EFIAPI
488 Sha256HashAll (
489 IN CONST VOID *Data,
490 IN UINTN DataSize,
491 OUT UINT8 *HashValue
492 );
493
494 /**
495 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
496
497 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.
498
499 **/
500 UINTN
501 EFIAPI
502 Sha384GetContextSize (
503 VOID
504 );
505
506 /**
507 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
508 subsequent use.
509
510 If Sha384Context is NULL, then return FALSE.
511
512 @param[out] Sha384Context Pointer to SHA-384 context being initialized.
513
514 @retval TRUE SHA-384 context initialization succeeded.
515 @retval FALSE SHA-384 context initialization failed.
516
517 **/
518 BOOLEAN
519 EFIAPI
520 Sha384Init (
521 OUT VOID *Sha384Context
522 );
523
524 /**
525 Makes a copy of an existing SHA-384 context.
526
527 If Sha384Context is NULL, then return FALSE.
528 If NewSha384Context is NULL, then return FALSE.
529 If this interface is not supported, then return FALSE.
530
531 @param[in] Sha384Context Pointer to SHA-384 context being copied.
532 @param[out] NewSha384Context Pointer to new SHA-384 context.
533
534 @retval TRUE SHA-384 context copy succeeded.
535 @retval FALSE SHA-384 context copy failed.
536 @retval FALSE This interface is not supported.
537
538 **/
539 BOOLEAN
540 EFIAPI
541 Sha384Duplicate (
542 IN CONST VOID *Sha384Context,
543 OUT VOID *NewSha384Context
544 );
545
546 /**
547 Digests the input data and updates SHA-384 context.
548
549 This function performs SHA-384 digest on a data buffer of the specified size.
550 It can be called multiple times to compute the digest of long or discontinuous data streams.
551 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized
552 by Sha384Final(). Behavior with invalid context is undefined.
553
554 If Sha384Context is NULL, then return FALSE.
555
556 @param[in, out] Sha384Context Pointer to the SHA-384 context.
557 @param[in] Data Pointer to the buffer containing the data to be hashed.
558 @param[in] DataSize Size of Data buffer in bytes.
559
560 @retval TRUE SHA-384 data digest succeeded.
561 @retval FALSE SHA-384 data digest failed.
562
563 **/
564 BOOLEAN
565 EFIAPI
566 Sha384Update (
567 IN OUT VOID *Sha384Context,
568 IN CONST VOID *Data,
569 IN UINTN DataSize
570 );
571
572 /**
573 Completes computation of the SHA-384 digest value.
574
575 This function completes SHA-384 hash computation and retrieves the digest value into
576 the specified memory. After this function has been called, the SHA-384 context cannot
577 be used again.
578 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be
579 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
580
581 If Sha384Context is NULL, then return FALSE.
582 If HashValue is NULL, then return FALSE.
583
584 @param[in, out] Sha384Context Pointer to the SHA-384 context.
585 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
586 value (48 bytes).
587
588 @retval TRUE SHA-384 digest computation succeeded.
589 @retval FALSE SHA-384 digest computation failed.
590
591 **/
592 BOOLEAN
593 EFIAPI
594 Sha384Final (
595 IN OUT VOID *Sha384Context,
596 OUT UINT8 *HashValue
597 );
598
599 /**
600 Computes the SHA-384 message digest of a input data buffer.
601
602 This function performs the SHA-384 message digest of a given data buffer, and places
603 the digest value into the specified memory.
604
605 If this interface is not supported, then return FALSE.
606
607 @param[in] Data Pointer to the buffer containing the data to be hashed.
608 @param[in] DataSize Size of Data buffer in bytes.
609 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
610 value (48 bytes).
611
612 @retval TRUE SHA-384 digest computation succeeded.
613 @retval FALSE SHA-384 digest computation failed.
614 @retval FALSE This interface is not supported.
615
616 **/
617 BOOLEAN
618 EFIAPI
619 Sha384HashAll (
620 IN CONST VOID *Data,
621 IN UINTN DataSize,
622 OUT UINT8 *HashValue
623 );
624
625 /**
626 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
627
628 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.
629
630 **/
631 UINTN
632 EFIAPI
633 Sha512GetContextSize (
634 VOID
635 );
636
637 /**
638 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
639 subsequent use.
640
641 If Sha512Context is NULL, then return FALSE.
642
643 @param[out] Sha512Context Pointer to SHA-512 context being initialized.
644
645 @retval TRUE SHA-512 context initialization succeeded.
646 @retval FALSE SHA-512 context initialization failed.
647
648 **/
649 BOOLEAN
650 EFIAPI
651 Sha512Init (
652 OUT VOID *Sha512Context
653 );
654
655 /**
656 Makes a copy of an existing SHA-512 context.
657
658 If Sha512Context is NULL, then return FALSE.
659 If NewSha512Context is NULL, then return FALSE.
660 If this interface is not supported, then return FALSE.
661
662 @param[in] Sha512Context Pointer to SHA-512 context being copied.
663 @param[out] NewSha512Context Pointer to new SHA-512 context.
664
665 @retval TRUE SHA-512 context copy succeeded.
666 @retval FALSE SHA-512 context copy failed.
667 @retval FALSE This interface is not supported.
668
669 **/
670 BOOLEAN
671 EFIAPI
672 Sha512Duplicate (
673 IN CONST VOID *Sha512Context,
674 OUT VOID *NewSha512Context
675 );
676
677 /**
678 Digests the input data and updates SHA-512 context.
679
680 This function performs SHA-512 digest on a data buffer of the specified size.
681 It can be called multiple times to compute the digest of long or discontinuous data streams.
682 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized
683 by Sha512Final(). Behavior with invalid context is undefined.
684
685 If Sha512Context is NULL, then return FALSE.
686
687 @param[in, out] Sha512Context Pointer to the SHA-512 context.
688 @param[in] Data Pointer to the buffer containing the data to be hashed.
689 @param[in] DataSize Size of Data buffer in bytes.
690
691 @retval TRUE SHA-512 data digest succeeded.
692 @retval FALSE SHA-512 data digest failed.
693
694 **/
695 BOOLEAN
696 EFIAPI
697 Sha512Update (
698 IN OUT VOID *Sha512Context,
699 IN CONST VOID *Data,
700 IN UINTN DataSize
701 );
702
703 /**
704 Completes computation of the SHA-512 digest value.
705
706 This function completes SHA-512 hash computation and retrieves the digest value into
707 the specified memory. After this function has been called, the SHA-512 context cannot
708 be used again.
709 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be
710 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
711
712 If Sha512Context is NULL, then return FALSE.
713 If HashValue is NULL, then return FALSE.
714
715 @param[in, out] Sha512Context Pointer to the SHA-512 context.
716 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
717 value (64 bytes).
718
719 @retval TRUE SHA-512 digest computation succeeded.
720 @retval FALSE SHA-512 digest computation failed.
721
722 **/
723 BOOLEAN
724 EFIAPI
725 Sha512Final (
726 IN OUT VOID *Sha512Context,
727 OUT UINT8 *HashValue
728 );
729
730 /**
731 Computes the SHA-512 message digest of a input data buffer.
732
733 This function performs the SHA-512 message digest of a given data buffer, and places
734 the digest value into the specified memory.
735
736 If this interface is not supported, then return FALSE.
737
738 @param[in] Data Pointer to the buffer containing the data to be hashed.
739 @param[in] DataSize Size of Data buffer in bytes.
740 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
741 value (64 bytes).
742
743 @retval TRUE SHA-512 digest computation succeeded.
744 @retval FALSE SHA-512 digest computation failed.
745 @retval FALSE This interface is not supported.
746
747 **/
748 BOOLEAN
749 EFIAPI
750 Sha512HashAll (
751 IN CONST VOID *Data,
752 IN UINTN DataSize,
753 OUT UINT8 *HashValue
754 );
755
756 /**
757 Parallel hash function ParallelHash256, as defined in NIST's Special Publication 800-185,
758 published December 2016.
759
760 @param[in] Input Pointer to the input message (X).
761 @param[in] InputByteLen The number(>0) of input bytes provided for the input data.
762 @param[in] BlockSize The size of each block (B).
763 @param[out] Output Pointer to the output buffer.
764 @param[in] OutputByteLen The desired number of output bytes (L).
765 @param[in] Customization Pointer to the customization string (S).
766 @param[in] CustomByteLen The length of the customization string in bytes.
767
768 @retval TRUE ParallelHash256 digest computation succeeded.
769 @retval FALSE ParallelHash256 digest computation failed.
770 @retval FALSE This interface is not supported.
771
772 **/
773 BOOLEAN
774 EFIAPI
775 ParallelHash256HashAll (
776 IN CONST VOID *Input,
777 IN UINTN InputByteLen,
778 IN UINTN BlockSize,
779 OUT VOID *Output,
780 IN UINTN OutputByteLen,
781 IN CONST VOID *Customization,
782 IN UINTN CustomByteLen
783 );
784
785 /**
786 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
787
788 @return The size, in bytes, of the context buffer required for SM3 hash operations.
789
790 **/
791 UINTN
792 EFIAPI
793 Sm3GetContextSize (
794 VOID
795 );
796
797 /**
798 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
799 subsequent use.
800
801 If Sm3Context is NULL, then return FALSE.
802
803 @param[out] Sm3Context Pointer to SM3 context being initialized.
804
805 @retval TRUE SM3 context initialization succeeded.
806 @retval FALSE SM3 context initialization failed.
807
808 **/
809 BOOLEAN
810 EFIAPI
811 Sm3Init (
812 OUT VOID *Sm3Context
813 );
814
815 /**
816 Makes a copy of an existing SM3 context.
817
818 If Sm3Context is NULL, then return FALSE.
819 If NewSm3Context is NULL, then return FALSE.
820 If this interface is not supported, then return FALSE.
821
822 @param[in] Sm3Context Pointer to SM3 context being copied.
823 @param[out] NewSm3Context Pointer to new SM3 context.
824
825 @retval TRUE SM3 context copy succeeded.
826 @retval FALSE SM3 context copy failed.
827 @retval FALSE This interface is not supported.
828
829 **/
830 BOOLEAN
831 EFIAPI
832 Sm3Duplicate (
833 IN CONST VOID *Sm3Context,
834 OUT VOID *NewSm3Context
835 );
836
837 /**
838 Digests the input data and updates SM3 context.
839
840 This function performs SM3 digest on a data buffer of the specified size.
841 It can be called multiple times to compute the digest of long or discontinuous data streams.
842 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
843 by Sm3Final(). Behavior with invalid context is undefined.
844
845 If Sm3Context is NULL, then return FALSE.
846
847 @param[in, out] Sm3Context Pointer to the SM3 context.
848 @param[in] Data Pointer to the buffer containing the data to be hashed.
849 @param[in] DataSize Size of Data buffer in bytes.
850
851 @retval TRUE SM3 data digest succeeded.
852 @retval FALSE SM3 data digest failed.
853
854 **/
855 BOOLEAN
856 EFIAPI
857 Sm3Update (
858 IN OUT VOID *Sm3Context,
859 IN CONST VOID *Data,
860 IN UINTN DataSize
861 );
862
863 /**
864 Completes computation of the SM3 digest value.
865
866 This function completes SM3 hash computation and retrieves the digest value into
867 the specified memory. After this function has been called, the SM3 context cannot
868 be used again.
869 SM3 context should be already correctly initialized by Sm3Init(), and should not be
870 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
871
872 If Sm3Context is NULL, then return FALSE.
873 If HashValue is NULL, then return FALSE.
874
875 @param[in, out] Sm3Context Pointer to the SM3 context.
876 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
877 value (32 bytes).
878
879 @retval TRUE SM3 digest computation succeeded.
880 @retval FALSE SM3 digest computation failed.
881
882 **/
883 BOOLEAN
884 EFIAPI
885 Sm3Final (
886 IN OUT VOID *Sm3Context,
887 OUT UINT8 *HashValue
888 );
889
890 /**
891 Computes the SM3 message digest of a input data buffer.
892
893 This function performs the SM3 message digest of a given data buffer, and places
894 the digest value into the specified memory.
895
896 If this interface is not supported, then return FALSE.
897
898 @param[in] Data Pointer to the buffer containing the data to be hashed.
899 @param[in] DataSize Size of Data buffer in bytes.
900 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
901 value (32 bytes).
902
903 @retval TRUE SM3 digest computation succeeded.
904 @retval FALSE SM3 digest computation failed.
905 @retval FALSE This interface is not supported.
906
907 **/
908 BOOLEAN
909 EFIAPI
910 Sm3HashAll (
911 IN CONST VOID *Data,
912 IN UINTN DataSize,
913 OUT UINT8 *HashValue
914 );
915
916 // =====================================================================================
917 // MAC (Message Authentication Code) Primitive
918 // =====================================================================================
919
920 /**
921 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
922
923 @return Pointer to the HMAC_CTX context that has been initialized.
924 If the allocations fails, HmacSha256New() returns NULL.
925
926 **/
927 VOID *
928 EFIAPI
929 HmacSha256New (
930 VOID
931 );
932
933 /**
934 Release the specified HMAC_CTX context.
935
936 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
937
938 **/
939 VOID
940 EFIAPI
941 HmacSha256Free (
942 IN VOID *HmacSha256Ctx
943 );
944
945 /**
946 Set user-supplied key for subsequent use. It must be done before any
947 calling to HmacSha256Update().
948
949 If HmacSha256Context is NULL, then return FALSE.
950 If this interface is not supported, then return FALSE.
951
952 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
953 @param[in] Key Pointer to the user-supplied key.
954 @param[in] KeySize Key size in bytes.
955
956 @retval TRUE The Key is set successfully.
957 @retval FALSE The Key is set unsuccessfully.
958 @retval FALSE This interface is not supported.
959
960 **/
961 BOOLEAN
962 EFIAPI
963 HmacSha256SetKey (
964 OUT VOID *HmacSha256Context,
965 IN CONST UINT8 *Key,
966 IN UINTN KeySize
967 );
968
969 /**
970 Makes a copy of an existing HMAC-SHA256 context.
971
972 If HmacSha256Context is NULL, then return FALSE.
973 If NewHmacSha256Context is NULL, then return FALSE.
974 If this interface is not supported, then return FALSE.
975
976 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
977 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
978
979 @retval TRUE HMAC-SHA256 context copy succeeded.
980 @retval FALSE HMAC-SHA256 context copy failed.
981 @retval FALSE This interface is not supported.
982
983 **/
984 BOOLEAN
985 EFIAPI
986 HmacSha256Duplicate (
987 IN CONST VOID *HmacSha256Context,
988 OUT VOID *NewHmacSha256Context
989 );
990
991 /**
992 Digests the input data and updates HMAC-SHA256 context.
993
994 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
995 It can be called multiple times to compute the digest of long or discontinuous data streams.
996 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
997 by HmacSha256Final(). Behavior with invalid context is undefined.
998
999 If HmacSha256Context is NULL, then return FALSE.
1000 If this interface is not supported, then return FALSE.
1001
1002 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1003 @param[in] Data Pointer to the buffer containing the data to be digested.
1004 @param[in] DataSize Size of Data buffer in bytes.
1005
1006 @retval TRUE HMAC-SHA256 data digest succeeded.
1007 @retval FALSE HMAC-SHA256 data digest failed.
1008 @retval FALSE This interface is not supported.
1009
1010 **/
1011 BOOLEAN
1012 EFIAPI
1013 HmacSha256Update (
1014 IN OUT VOID *HmacSha256Context,
1015 IN CONST VOID *Data,
1016 IN UINTN DataSize
1017 );
1018
1019 /**
1020 Completes computation of the HMAC-SHA256 digest value.
1021
1022 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
1023 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
1024 be used again.
1025 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
1026 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
1027
1028 If HmacSha256Context is NULL, then return FALSE.
1029 If HmacValue is NULL, then return FALSE.
1030 If this interface is not supported, then return FALSE.
1031
1032 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1033 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
1034 value (32 bytes).
1035
1036 @retval TRUE HMAC-SHA256 digest computation succeeded.
1037 @retval FALSE HMAC-SHA256 digest computation failed.
1038 @retval FALSE This interface is not supported.
1039
1040 **/
1041 BOOLEAN
1042 EFIAPI
1043 HmacSha256Final (
1044 IN OUT VOID *HmacSha256Context,
1045 OUT UINT8 *HmacValue
1046 );
1047
1048 /**
1049 Computes the HMAC-SHA256 digest of a input data buffer.
1050
1051 This function performs the HMAC-SHA256 digest of a given data buffer, and places
1052 the digest value into the specified memory.
1053
1054 If this interface is not supported, then return FALSE.
1055
1056 @param[in] Data Pointer to the buffer containing the data to be digested.
1057 @param[in] DataSize Size of Data buffer in bytes.
1058 @param[in] Key Pointer to the user-supplied key.
1059 @param[in] KeySize Key size in bytes.
1060 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
1061 value (32 bytes).
1062
1063 @retval TRUE HMAC-SHA256 digest computation succeeded.
1064 @retval FALSE HMAC-SHA256 digest computation failed.
1065 @retval FALSE This interface is not supported.
1066
1067 **/
1068 BOOLEAN
1069 EFIAPI
1070 HmacSha256All (
1071 IN CONST VOID *Data,
1072 IN UINTN DataSize,
1073 IN CONST UINT8 *Key,
1074 IN UINTN KeySize,
1075 OUT UINT8 *HmacValue
1076 );
1077
1078 /**
1079 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA384 use.
1080
1081 @return Pointer to the HMAC_CTX context that has been initialized.
1082 If the allocations fails, HmacSha384New() returns NULL.
1083
1084 **/
1085 VOID *
1086 EFIAPI
1087 HmacSha384New (
1088 VOID
1089 );
1090
1091 /**
1092 Release the specified HMAC_CTX context.
1093
1094 @param[in] HmacSha384Ctx Pointer to the HMAC_CTX context to be released.
1095
1096 **/
1097 VOID
1098 EFIAPI
1099 HmacSha384Free (
1100 IN VOID *HmacSha384Ctx
1101 );
1102
1103 /**
1104 Set user-supplied key for subsequent use. It must be done before any
1105 calling to HmacSha384Update().
1106
1107 If HmacSha384Context is NULL, then return FALSE.
1108 If this interface is not supported, then return FALSE.
1109
1110 @param[out] HmacSha384Context Pointer to HMAC-SHA384 context.
1111 @param[in] Key Pointer to the user-supplied key.
1112 @param[in] KeySize Key size in bytes.
1113
1114 @retval TRUE The Key is set successfully.
1115 @retval FALSE The Key is set unsuccessfully.
1116 @retval FALSE This interface is not supported.
1117
1118 **/
1119 BOOLEAN
1120 EFIAPI
1121 HmacSha384SetKey (
1122 OUT VOID *HmacSha384Context,
1123 IN CONST UINT8 *Key,
1124 IN UINTN KeySize
1125 );
1126
1127 /**
1128 Makes a copy of an existing HMAC-SHA384 context.
1129
1130 If HmacSha384Context is NULL, then return FALSE.
1131 If NewHmacSha384Context is NULL, then return FALSE.
1132 If this interface is not supported, then return FALSE.
1133
1134 @param[in] HmacSha384Context Pointer to HMAC-SHA384 context being copied.
1135 @param[out] NewHmacSha384Context Pointer to new HMAC-SHA384 context.
1136
1137 @retval TRUE HMAC-SHA384 context copy succeeded.
1138 @retval FALSE HMAC-SHA384 context copy failed.
1139 @retval FALSE This interface is not supported.
1140
1141 **/
1142 BOOLEAN
1143 EFIAPI
1144 HmacSha384Duplicate (
1145 IN CONST VOID *HmacSha384Context,
1146 OUT VOID *NewHmacSha384Context
1147 );
1148
1149 /**
1150 Digests the input data and updates HMAC-SHA384 context.
1151
1152 This function performs HMAC-SHA384 digest on a data buffer of the specified size.
1153 It can be called multiple times to compute the digest of long or discontinuous data streams.
1154 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1155 by HmacSha384Final(). Behavior with invalid context is undefined.
1156
1157 If HmacSha384Context is NULL, then return FALSE.
1158 If this interface is not supported, then return FALSE.
1159
1160 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1161 @param[in] Data Pointer to the buffer containing the data to be digested.
1162 @param[in] DataSize Size of Data buffer in bytes.
1163
1164 @retval TRUE HMAC-SHA384 data digest succeeded.
1165 @retval FALSE HMAC-SHA384 data digest failed.
1166 @retval FALSE This interface is not supported.
1167
1168 **/
1169 BOOLEAN
1170 EFIAPI
1171 HmacSha384Update (
1172 IN OUT VOID *HmacSha384Context,
1173 IN CONST VOID *Data,
1174 IN UINTN DataSize
1175 );
1176
1177 /**
1178 Completes computation of the HMAC-SHA384 digest value.
1179
1180 This function completes HMAC-SHA384 hash computation and retrieves the digest value into
1181 the specified memory. After this function has been called, the HMAC-SHA384 context cannot
1182 be used again.
1183 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1184 by HmacSha384Final(). Behavior with invalid HMAC-SHA384 context is undefined.
1185
1186 If HmacSha384Context is NULL, then return FALSE.
1187 If HmacValue is NULL, then return FALSE.
1188 If this interface is not supported, then return FALSE.
1189
1190 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1191 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
1192 value (48 bytes).
1193
1194 @retval TRUE HMAC-SHA384 digest computation succeeded.
1195 @retval FALSE HMAC-SHA384 digest computation failed.
1196 @retval FALSE This interface is not supported.
1197
1198 **/
1199 BOOLEAN
1200 EFIAPI
1201 HmacSha384Final (
1202 IN OUT VOID *HmacSha384Context,
1203 OUT UINT8 *HmacValue
1204 );
1205
1206 /**
1207 Computes the HMAC-SHA384 digest of a input data buffer.
1208
1209 This function performs the HMAC-SHA384 digest of a given data buffer, and places
1210 the digest value into the specified memory.
1211
1212 If this interface is not supported, then return FALSE.
1213
1214 @param[in] Data Pointer to the buffer containing the data to be digested.
1215 @param[in] DataSize Size of Data buffer in bytes.
1216 @param[in] Key Pointer to the user-supplied key.
1217 @param[in] KeySize Key size in bytes.
1218 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA384 digest
1219 value (48 bytes).
1220
1221 @retval TRUE HMAC-SHA384 digest computation succeeded.
1222 @retval FALSE HMAC-SHA384 digest computation failed.
1223 @retval FALSE This interface is not supported.
1224
1225 **/
1226 BOOLEAN
1227 EFIAPI
1228 HmacSha384All (
1229 IN CONST VOID *Data,
1230 IN UINTN DataSize,
1231 IN CONST UINT8 *Key,
1232 IN UINTN KeySize,
1233 OUT UINT8 *HmacValue
1234 );
1235
1236 // =====================================================================================
1237 // Symmetric Cryptography Primitive
1238 // =====================================================================================
1239
1240 /**
1241 Retrieves the size, in bytes, of the context buffer required for AES operations.
1242
1243 If this interface is not supported, then return zero.
1244
1245 @return The size, in bytes, of the context buffer required for AES operations.
1246 @retval 0 This interface is not supported.
1247
1248 **/
1249 UINTN
1250 EFIAPI
1251 AesGetContextSize (
1252 VOID
1253 );
1254
1255 /**
1256 Initializes user-supplied memory as AES context for subsequent use.
1257
1258 This function initializes user-supplied memory pointed by AesContext as AES context.
1259 In addition, it sets up all AES key materials for subsequent encryption and decryption
1260 operations.
1261 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1262
1263 If AesContext is NULL, then return FALSE.
1264 If Key is NULL, then return FALSE.
1265 If KeyLength is not valid, then return FALSE.
1266 If this interface is not supported, then return FALSE.
1267
1268 @param[out] AesContext Pointer to AES context being initialized.
1269 @param[in] Key Pointer to the user-supplied AES key.
1270 @param[in] KeyLength Length of AES key in bits.
1271
1272 @retval TRUE AES context initialization succeeded.
1273 @retval FALSE AES context initialization failed.
1274 @retval FALSE This interface is not supported.
1275
1276 **/
1277 BOOLEAN
1278 EFIAPI
1279 AesInit (
1280 OUT VOID *AesContext,
1281 IN CONST UINT8 *Key,
1282 IN UINTN KeyLength
1283 );
1284
1285 /**
1286 Performs AES encryption on a data buffer of the specified size in CBC mode.
1287
1288 This function performs AES encryption on data buffer pointed by Input, of specified
1289 size of InputSize, in CBC mode.
1290 InputSize must be multiple of block size (16 bytes). This function does not perform
1291 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1292 Initialization vector should be one block size (16 bytes).
1293 AesContext should be already correctly initialized by AesInit(). Behavior with
1294 invalid AES context is undefined.
1295
1296 If AesContext is NULL, then return FALSE.
1297 If Input is NULL, then return FALSE.
1298 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1299 If Ivec is NULL, then return FALSE.
1300 If Output is NULL, then return FALSE.
1301 If this interface is not supported, then return FALSE.
1302
1303 @param[in] AesContext Pointer to the AES context.
1304 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1305 @param[in] InputSize Size of the Input buffer in bytes.
1306 @param[in] Ivec Pointer to initialization vector.
1307 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1308
1309 @retval TRUE AES encryption succeeded.
1310 @retval FALSE AES encryption failed.
1311 @retval FALSE This interface is not supported.
1312
1313 **/
1314 BOOLEAN
1315 EFIAPI
1316 AesCbcEncrypt (
1317 IN VOID *AesContext,
1318 IN CONST UINT8 *Input,
1319 IN UINTN InputSize,
1320 IN CONST UINT8 *Ivec,
1321 OUT UINT8 *Output
1322 );
1323
1324 /**
1325 Performs AES decryption on a data buffer of the specified size in CBC mode.
1326
1327 This function performs AES decryption on data buffer pointed by Input, of specified
1328 size of InputSize, in CBC mode.
1329 InputSize must be multiple of block size (16 bytes). This function does not perform
1330 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1331 Initialization vector should be one block size (16 bytes).
1332 AesContext should be already correctly initialized by AesInit(). Behavior with
1333 invalid AES context is undefined.
1334
1335 If AesContext is NULL, then return FALSE.
1336 If Input is NULL, then return FALSE.
1337 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1338 If Ivec is NULL, then return FALSE.
1339 If Output is NULL, then return FALSE.
1340 If this interface is not supported, then return FALSE.
1341
1342 @param[in] AesContext Pointer to the AES context.
1343 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1344 @param[in] InputSize Size of the Input buffer in bytes.
1345 @param[in] Ivec Pointer to initialization vector.
1346 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1347
1348 @retval TRUE AES decryption succeeded.
1349 @retval FALSE AES decryption failed.
1350 @retval FALSE This interface is not supported.
1351
1352 **/
1353 BOOLEAN
1354 EFIAPI
1355 AesCbcDecrypt (
1356 IN VOID *AesContext,
1357 IN CONST UINT8 *Input,
1358 IN UINTN InputSize,
1359 IN CONST UINT8 *Ivec,
1360 OUT UINT8 *Output
1361 );
1362
1363 // =====================================================================================
1364 // Authenticated Encryption with Associated Data (AEAD) Cryptography Primitive
1365 // =====================================================================================
1366
1367 /**
1368 Performs AEAD AES-GCM authenticated encryption on a data buffer and additional authenticated data (AAD).
1369
1370 IvSize must be 12, otherwise FALSE is returned.
1371 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1372 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1373
1374 @param[in] Key Pointer to the encryption key.
1375 @param[in] KeySize Size of the encryption key in bytes.
1376 @param[in] Iv Pointer to the IV value.
1377 @param[in] IvSize Size of the IV value in bytes.
1378 @param[in] AData Pointer to the additional authenticated data (AAD).
1379 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1380 @param[in] DataIn Pointer to the input data buffer to be encrypted.
1381 @param[in] DataInSize Size of the input data buffer in bytes.
1382 @param[out] TagOut Pointer to a buffer that receives the authentication tag output.
1383 @param[in] TagSize Size of the authentication tag in bytes.
1384 @param[out] DataOut Pointer to a buffer that receives the encryption output.
1385 @param[out] DataOutSize Size of the output data buffer in bytes.
1386
1387 @retval TRUE AEAD AES-GCM authenticated encryption succeeded.
1388 @retval FALSE AEAD AES-GCM authenticated encryption failed.
1389
1390 **/
1391 BOOLEAN
1392 EFIAPI
1393 AeadAesGcmEncrypt (
1394 IN CONST UINT8 *Key,
1395 IN UINTN KeySize,
1396 IN CONST UINT8 *Iv,
1397 IN UINTN IvSize,
1398 IN CONST UINT8 *AData,
1399 IN UINTN ADataSize,
1400 IN CONST UINT8 *DataIn,
1401 IN UINTN DataInSize,
1402 OUT UINT8 *TagOut,
1403 IN UINTN TagSize,
1404 OUT UINT8 *DataOut,
1405 OUT UINTN *DataOutSize
1406 );
1407
1408 /**
1409 Performs AEAD AES-GCM authenticated decryption on a data buffer and additional authenticated data (AAD).
1410
1411 IvSize must be 12, otherwise FALSE is returned.
1412 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1413 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1414 If additional authenticated data verification fails, FALSE is returned.
1415
1416 @param[in] Key Pointer to the encryption key.
1417 @param[in] KeySize Size of the encryption key in bytes.
1418 @param[in] Iv Pointer to the IV value.
1419 @param[in] IvSize Size of the IV value in bytes.
1420 @param[in] AData Pointer to the additional authenticated data (AAD).
1421 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1422 @param[in] DataIn Pointer to the input data buffer to be decrypted.
1423 @param[in] DataInSize Size of the input data buffer in bytes.
1424 @param[in] Tag Pointer to a buffer that contains the authentication tag.
1425 @param[in] TagSize Size of the authentication tag in bytes.
1426 @param[out] DataOut Pointer to a buffer that receives the decryption output.
1427 @param[out] DataOutSize Size of the output data buffer in bytes.
1428
1429 @retval TRUE AEAD AES-GCM authenticated decryption succeeded.
1430 @retval FALSE AEAD AES-GCM authenticated decryption failed.
1431
1432 **/
1433 BOOLEAN
1434 EFIAPI
1435 AeadAesGcmDecrypt (
1436 IN CONST UINT8 *Key,
1437 IN UINTN KeySize,
1438 IN CONST UINT8 *Iv,
1439 IN UINTN IvSize,
1440 IN CONST UINT8 *AData,
1441 IN UINTN ADataSize,
1442 IN CONST UINT8 *DataIn,
1443 IN UINTN DataInSize,
1444 IN CONST UINT8 *Tag,
1445 IN UINTN TagSize,
1446 OUT UINT8 *DataOut,
1447 OUT UINTN *DataOutSize
1448 );
1449
1450 // =====================================================================================
1451 // Asymmetric Cryptography Primitive
1452 // =====================================================================================
1453
1454 /**
1455 Allocates and initializes one RSA context for subsequent use.
1456
1457 @return Pointer to the RSA context that has been initialized.
1458 If the allocations fails, RsaNew() returns NULL.
1459
1460 **/
1461 VOID *
1462 EFIAPI
1463 RsaNew (
1464 VOID
1465 );
1466
1467 /**
1468 Release the specified RSA context.
1469
1470 If RsaContext is NULL, then return FALSE.
1471
1472 @param[in] RsaContext Pointer to the RSA context to be released.
1473
1474 **/
1475 VOID
1476 EFIAPI
1477 RsaFree (
1478 IN VOID *RsaContext
1479 );
1480
1481 /**
1482 Sets the tag-designated key component into the established RSA context.
1483
1484 This function sets the tag-designated RSA key component into the established
1485 RSA context from the user-specified non-negative integer (octet string format
1486 represented in RSA PKCS#1).
1487 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1488
1489 If RsaContext is NULL, then return FALSE.
1490
1491 @param[in, out] RsaContext Pointer to RSA context being set.
1492 @param[in] KeyTag Tag of RSA key component being set.
1493 @param[in] BigNumber Pointer to octet integer buffer.
1494 If NULL, then the specified key component in RSA
1495 context is cleared.
1496 @param[in] BnSize Size of big number buffer in bytes.
1497 If BigNumber is NULL, then it is ignored.
1498
1499 @retval TRUE RSA key component was set successfully.
1500 @retval FALSE Invalid RSA key component tag.
1501
1502 **/
1503 BOOLEAN
1504 EFIAPI
1505 RsaSetKey (
1506 IN OUT VOID *RsaContext,
1507 IN RSA_KEY_TAG KeyTag,
1508 IN CONST UINT8 *BigNumber,
1509 IN UINTN BnSize
1510 );
1511
1512 /**
1513 Gets the tag-designated RSA key component from the established RSA context.
1514
1515 This function retrieves the tag-designated RSA key component from the
1516 established RSA context as a non-negative integer (octet string format
1517 represented in RSA PKCS#1).
1518 If specified key component has not been set or has been cleared, then returned
1519 BnSize is set to 0.
1520 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1521 is returned and BnSize is set to the required buffer size to obtain the key.
1522
1523 If RsaContext is NULL, then return FALSE.
1524 If BnSize is NULL, then return FALSE.
1525 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1526 If this interface is not supported, then return FALSE.
1527
1528 @param[in, out] RsaContext Pointer to RSA context being set.
1529 @param[in] KeyTag Tag of RSA key component being set.
1530 @param[out] BigNumber Pointer to octet integer buffer.
1531 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1532 On output, the size of data returned in big number buffer in bytes.
1533
1534 @retval TRUE RSA key component was retrieved successfully.
1535 @retval FALSE Invalid RSA key component tag.
1536 @retval FALSE BnSize is too small.
1537 @retval FALSE This interface is not supported.
1538
1539 **/
1540 BOOLEAN
1541 EFIAPI
1542 RsaGetKey (
1543 IN OUT VOID *RsaContext,
1544 IN RSA_KEY_TAG KeyTag,
1545 OUT UINT8 *BigNumber,
1546 IN OUT UINTN *BnSize
1547 );
1548
1549 /**
1550 Generates RSA key components.
1551
1552 This function generates RSA key components. It takes RSA public exponent E and
1553 length in bits of RSA modulus N as input, and generates all key components.
1554 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1555
1556 Before this function can be invoked, pseudorandom number generator must be correctly
1557 initialized by RandomSeed().
1558
1559 If RsaContext is NULL, then return FALSE.
1560 If this interface is not supported, then return FALSE.
1561
1562 @param[in, out] RsaContext Pointer to RSA context being set.
1563 @param[in] ModulusLength Length of RSA modulus N in bits.
1564 @param[in] PublicExponent Pointer to RSA public exponent.
1565 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1566
1567 @retval TRUE RSA key component was generated successfully.
1568 @retval FALSE Invalid RSA key component tag.
1569 @retval FALSE This interface is not supported.
1570
1571 **/
1572 BOOLEAN
1573 EFIAPI
1574 RsaGenerateKey (
1575 IN OUT VOID *RsaContext,
1576 IN UINTN ModulusLength,
1577 IN CONST UINT8 *PublicExponent,
1578 IN UINTN PublicExponentSize
1579 );
1580
1581 /**
1582 Validates key components of RSA context.
1583 NOTE: This function performs integrity checks on all the RSA key material, so
1584 the RSA key structure must contain all the private key data.
1585
1586 This function validates key components of RSA context in following aspects:
1587 - Whether p is a prime
1588 - Whether q is a prime
1589 - Whether n = p * q
1590 - Whether d*e = 1 mod lcm(p-1,q-1)
1591
1592 If RsaContext is NULL, then return FALSE.
1593 If this interface is not supported, then return FALSE.
1594
1595 @param[in] RsaContext Pointer to RSA context to check.
1596
1597 @retval TRUE RSA key components are valid.
1598 @retval FALSE RSA key components are not valid.
1599 @retval FALSE This interface is not supported.
1600
1601 **/
1602 BOOLEAN
1603 EFIAPI
1604 RsaCheckKey (
1605 IN VOID *RsaContext
1606 );
1607
1608 /**
1609 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1610
1611 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1612 RSA PKCS#1.
1613 If the Signature buffer is too small to hold the contents of signature, FALSE
1614 is returned and SigSize is set to the required buffer size to obtain the signature.
1615
1616 If RsaContext is NULL, then return FALSE.
1617 If MessageHash is NULL, then return FALSE.
1618 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1619 If SigSize is large enough but Signature is NULL, then return FALSE.
1620 If this interface is not supported, then return FALSE.
1621
1622 @param[in] RsaContext Pointer to RSA context for signature generation.
1623 @param[in] MessageHash Pointer to octet message hash to be signed.
1624 @param[in] HashSize Size of the message hash in bytes.
1625 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1626 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1627 On output, the size of data returned in Signature buffer in bytes.
1628
1629 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1630 @retval FALSE Signature generation failed.
1631 @retval FALSE SigSize is too small.
1632 @retval FALSE This interface is not supported.
1633
1634 **/
1635 BOOLEAN
1636 EFIAPI
1637 RsaPkcs1Sign (
1638 IN VOID *RsaContext,
1639 IN CONST UINT8 *MessageHash,
1640 IN UINTN HashSize,
1641 OUT UINT8 *Signature,
1642 IN OUT UINTN *SigSize
1643 );
1644
1645 /**
1646 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1647 RSA PKCS#1.
1648
1649 If RsaContext is NULL, then return FALSE.
1650 If MessageHash is NULL, then return FALSE.
1651 If Signature is NULL, then return FALSE.
1652 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1653
1654 @param[in] RsaContext Pointer to RSA context for signature verification.
1655 @param[in] MessageHash Pointer to octet message hash to be checked.
1656 @param[in] HashSize Size of the message hash in bytes.
1657 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1658 @param[in] SigSize Size of signature in bytes.
1659
1660 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1661 @retval FALSE Invalid signature or invalid RSA context.
1662
1663 **/
1664 BOOLEAN
1665 EFIAPI
1666 RsaPkcs1Verify (
1667 IN VOID *RsaContext,
1668 IN CONST UINT8 *MessageHash,
1669 IN UINTN HashSize,
1670 IN CONST UINT8 *Signature,
1671 IN UINTN SigSize
1672 );
1673
1674 /**
1675 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1676
1677 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1678 RFC 8017.
1679 Mask generation function is the same as the message digest algorithm.
1680 If the Signature buffer is too small to hold the contents of signature, FALSE
1681 is returned and SigSize is set to the required buffer size to obtain the signature.
1682
1683 If RsaContext is NULL, then return FALSE.
1684 If Message is NULL, then return FALSE.
1685 If MsgSize is zero or > INT_MAX, then return FALSE.
1686 If DigestLen is NOT 32, 48 or 64, return FALSE.
1687 If SaltLen is not equal to DigestLen, then return FALSE.
1688 If SigSize is large enough but Signature is NULL, then return FALSE.
1689 If this interface is not supported, then return FALSE.
1690
1691 @param[in] RsaContext Pointer to RSA context for signature generation.
1692 @param[in] Message Pointer to octet message to be signed.
1693 @param[in] MsgSize Size of the message in bytes.
1694 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1695 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1696 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1697 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1698 On output, the size of data returned in Signature buffer in bytes.
1699
1700 @retval TRUE Signature successfully generated in RSASSA-PSS.
1701 @retval FALSE Signature generation failed.
1702 @retval FALSE SigSize is too small.
1703 @retval FALSE This interface is not supported.
1704
1705 **/
1706 BOOLEAN
1707 EFIAPI
1708 RsaPssSign (
1709 IN VOID *RsaContext,
1710 IN CONST UINT8 *Message,
1711 IN UINTN MsgSize,
1712 IN UINT16 DigestLen,
1713 IN UINT16 SaltLen,
1714 OUT UINT8 *Signature,
1715 IN OUT UINTN *SigSize
1716 );
1717
1718 /**
1719 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1720 Implementation determines salt length automatically from the signature encoding.
1721 Mask generation function is the same as the message digest algorithm.
1722 Salt length should be equal to digest length.
1723
1724 @param[in] RsaContext Pointer to RSA context for signature verification.
1725 @param[in] Message Pointer to octet message to be verified.
1726 @param[in] MsgSize Size of the message in bytes.
1727 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1728 @param[in] SigSize Size of signature in bytes.
1729 @param[in] DigestLen Length of digest for RSA operation.
1730 @param[in] SaltLen Salt length for PSS encoding.
1731
1732 @retval TRUE Valid signature encoded in RSASSA-PSS.
1733 @retval FALSE Invalid signature or invalid RSA context.
1734
1735 **/
1736 BOOLEAN
1737 EFIAPI
1738 RsaPssVerify (
1739 IN VOID *RsaContext,
1740 IN CONST UINT8 *Message,
1741 IN UINTN MsgSize,
1742 IN CONST UINT8 *Signature,
1743 IN UINTN SigSize,
1744 IN UINT16 DigestLen,
1745 IN UINT16 SaltLen
1746 );
1747
1748 /**
1749 Retrieve the RSA Private Key from the password-protected PEM key data.
1750
1751 If PemData is NULL, then return FALSE.
1752 If RsaContext is NULL, then return FALSE.
1753 If this interface is not supported, then return FALSE.
1754
1755 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1756 @param[in] PemSize Size of the PEM key data in bytes.
1757 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1758 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1759 RSA private key component. Use RsaFree() function to free the
1760 resource.
1761
1762 @retval TRUE RSA Private Key was retrieved successfully.
1763 @retval FALSE Invalid PEM key data or incorrect password.
1764 @retval FALSE This interface is not supported.
1765
1766 **/
1767 BOOLEAN
1768 EFIAPI
1769 RsaGetPrivateKeyFromPem (
1770 IN CONST UINT8 *PemData,
1771 IN UINTN PemSize,
1772 IN CONST CHAR8 *Password,
1773 OUT VOID **RsaContext
1774 );
1775
1776 /**
1777 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1778
1779 If Cert is NULL, then return FALSE.
1780 If RsaContext is NULL, then return FALSE.
1781 If this interface is not supported, then return FALSE.
1782
1783 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1784 @param[in] CertSize Size of the X509 certificate in bytes.
1785 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1786 RSA public key component. Use RsaFree() function to free the
1787 resource.
1788
1789 @retval TRUE RSA Public Key was retrieved successfully.
1790 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1791 @retval FALSE This interface is not supported.
1792
1793 **/
1794 BOOLEAN
1795 EFIAPI
1796 RsaGetPublicKeyFromX509 (
1797 IN CONST UINT8 *Cert,
1798 IN UINTN CertSize,
1799 OUT VOID **RsaContext
1800 );
1801
1802 /**
1803 Retrieve the subject bytes from one X.509 certificate.
1804
1805 If Cert is NULL, then return FALSE.
1806 If SubjectSize is NULL, then return FALSE.
1807 If this interface is not supported, then return FALSE.
1808
1809 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1810 @param[in] CertSize Size of the X509 certificate in bytes.
1811 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1812 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1813 and the size of buffer returned CertSubject on output.
1814
1815 @retval TRUE The certificate subject retrieved successfully.
1816 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1817 The SubjectSize will be updated with the required size.
1818 @retval FALSE This interface is not supported.
1819
1820 **/
1821 BOOLEAN
1822 EFIAPI
1823 X509GetSubjectName (
1824 IN CONST UINT8 *Cert,
1825 IN UINTN CertSize,
1826 OUT UINT8 *CertSubject,
1827 IN OUT UINTN *SubjectSize
1828 );
1829
1830 /**
1831 Retrieve the common name (CN) string from one X.509 certificate.
1832
1833 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1834 @param[in] CertSize Size of the X509 certificate in bytes.
1835 @param[out] CommonName Buffer to contain the retrieved certificate common
1836 name string (UTF8). At most CommonNameSize bytes will be
1837 written and the string will be null terminated. May be
1838 NULL in order to determine the size buffer needed.
1839 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1840 and the size of buffer returned CommonName on output.
1841 If CommonName is NULL then the amount of space needed
1842 in buffer (including the final null) is returned.
1843
1844 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1845 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1846 If CommonNameSize is NULL.
1847 If CommonName is not NULL and *CommonNameSize is 0.
1848 If Certificate is invalid.
1849 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1850 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1851 (including the final null) is returned in the
1852 CommonNameSize parameter.
1853 @retval RETURN_UNSUPPORTED The operation is not supported.
1854
1855 **/
1856 RETURN_STATUS
1857 EFIAPI
1858 X509GetCommonName (
1859 IN CONST UINT8 *Cert,
1860 IN UINTN CertSize,
1861 OUT CHAR8 *CommonName OPTIONAL,
1862 IN OUT UINTN *CommonNameSize
1863 );
1864
1865 /**
1866 Retrieve the organization name (O) string from one X.509 certificate.
1867
1868 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1869 @param[in] CertSize Size of the X509 certificate in bytes.
1870 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1871 name string. At most NameBufferSize bytes will be
1872 written and the string will be null terminated. May be
1873 NULL in order to determine the size buffer needed.
1874 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1875 and the size of buffer returned Name on output.
1876 If NameBuffer is NULL then the amount of space needed
1877 in buffer (including the final null) is returned.
1878
1879 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1880 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1881 If NameBufferSize is NULL.
1882 If NameBuffer is not NULL and *CommonNameSize is 0.
1883 If Certificate is invalid.
1884 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1885 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1886 (including the final null) is returned in the
1887 CommonNameSize parameter.
1888 @retval RETURN_UNSUPPORTED The operation is not supported.
1889
1890 **/
1891 RETURN_STATUS
1892 EFIAPI
1893 X509GetOrganizationName (
1894 IN CONST UINT8 *Cert,
1895 IN UINTN CertSize,
1896 OUT CHAR8 *NameBuffer OPTIONAL,
1897 IN OUT UINTN *NameBufferSize
1898 );
1899
1900 /**
1901 Verify one X509 certificate was issued by the trusted CA.
1902
1903 If Cert is NULL, then return FALSE.
1904 If CACert is NULL, then return FALSE.
1905 If this interface is not supported, then return FALSE.
1906
1907 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1908 @param[in] CertSize Size of the X509 certificate in bytes.
1909 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1910 @param[in] CACertSize Size of the CA Certificate in bytes.
1911
1912 @retval TRUE The certificate was issued by the trusted CA.
1913 @retval FALSE Invalid certificate or the certificate was not issued by the given
1914 trusted CA.
1915 @retval FALSE This interface is not supported.
1916
1917 **/
1918 BOOLEAN
1919 EFIAPI
1920 X509VerifyCert (
1921 IN CONST UINT8 *Cert,
1922 IN UINTN CertSize,
1923 IN CONST UINT8 *CACert,
1924 IN UINTN CACertSize
1925 );
1926
1927 /**
1928 Construct a X509 object from DER-encoded certificate data.
1929
1930 If Cert is NULL, then return FALSE.
1931 If SingleX509Cert is NULL, then return FALSE.
1932 If this interface is not supported, then return FALSE.
1933
1934 @param[in] Cert Pointer to the DER-encoded certificate data.
1935 @param[in] CertSize The size of certificate data in bytes.
1936 @param[out] SingleX509Cert The generated X509 object.
1937
1938 @retval TRUE The X509 object generation succeeded.
1939 @retval FALSE The operation failed.
1940 @retval FALSE This interface is not supported.
1941
1942 **/
1943 BOOLEAN
1944 EFIAPI
1945 X509ConstructCertificate (
1946 IN CONST UINT8 *Cert,
1947 IN UINTN CertSize,
1948 OUT UINT8 **SingleX509Cert
1949 );
1950
1951 /**
1952 Construct a X509 stack object from a list of DER-encoded certificate data.
1953
1954 If X509Stack is NULL, then return FALSE.
1955 If this interface is not supported, then return FALSE.
1956
1957 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1958 On output, pointer to the X509 stack object with new
1959 inserted X509 certificate.
1960 @param[in] Args VA_LIST marker for the variable argument list.
1961 A list of DER-encoded single certificate data followed
1962 by certificate size. A NULL terminates the list. The
1963 pairs are the arguments to X509ConstructCertificate().
1964
1965 @retval TRUE The X509 stack construction succeeded.
1966 @retval FALSE The construction operation failed.
1967 @retval FALSE This interface is not supported.
1968
1969 **/
1970 BOOLEAN
1971 EFIAPI
1972 X509ConstructCertificateStackV (
1973 IN OUT UINT8 **X509Stack,
1974 IN VA_LIST Args
1975 );
1976
1977 /**
1978 Construct a X509 stack object from a list of DER-encoded certificate data.
1979
1980 If X509Stack is NULL, then return FALSE.
1981 If this interface is not supported, then return FALSE.
1982
1983 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1984 On output, pointer to the X509 stack object with new
1985 inserted X509 certificate.
1986 @param ... A list of DER-encoded single certificate data followed
1987 by certificate size. A NULL terminates the list. The
1988 pairs are the arguments to X509ConstructCertificate().
1989
1990 @retval TRUE The X509 stack construction succeeded.
1991 @retval FALSE The construction operation failed.
1992 @retval FALSE This interface is not supported.
1993
1994 **/
1995 BOOLEAN
1996 EFIAPI
1997 X509ConstructCertificateStack (
1998 IN OUT UINT8 **X509Stack,
1999 ...
2000 );
2001
2002 /**
2003 Release the specified X509 object.
2004
2005 If the interface is not supported, then ASSERT().
2006
2007 @param[in] X509Cert Pointer to the X509 object to be released.
2008
2009 **/
2010 VOID
2011 EFIAPI
2012 X509Free (
2013 IN VOID *X509Cert
2014 );
2015
2016 /**
2017 Release the specified X509 stack object.
2018
2019 If the interface is not supported, then ASSERT().
2020
2021 @param[in] X509Stack Pointer to the X509 stack object to be released.
2022
2023 **/
2024 VOID
2025 EFIAPI
2026 X509StackFree (
2027 IN VOID *X509Stack
2028 );
2029
2030 /**
2031 Retrieve the TBSCertificate from one given X.509 certificate.
2032
2033 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
2034 @param[in] CertSize Size of the X509 certificate in bytes.
2035 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
2036 @param[out] TBSCertSize Size of the TBS certificate in bytes.
2037
2038 If Cert is NULL, then return FALSE.
2039 If TBSCert is NULL, then return FALSE.
2040 If TBSCertSize is NULL, then return FALSE.
2041 If this interface is not supported, then return FALSE.
2042
2043 @retval TRUE The TBSCertificate was retrieved successfully.
2044 @retval FALSE Invalid X.509 certificate.
2045
2046 **/
2047 BOOLEAN
2048 EFIAPI
2049 X509GetTBSCert (
2050 IN CONST UINT8 *Cert,
2051 IN UINTN CertSize,
2052 OUT UINT8 **TBSCert,
2053 OUT UINTN *TBSCertSize
2054 );
2055
2056 /**
2057 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
2058 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
2059
2060 If Password or Salt or OutKey is NULL, then return FALSE.
2061 If the hash algorithm could not be determined, then return FALSE.
2062 If this interface is not supported, then return FALSE.
2063
2064 @param[in] PasswordLength Length of input password in bytes.
2065 @param[in] Password Pointer to the array for the password.
2066 @param[in] SaltLength Size of the Salt in bytes.
2067 @param[in] Salt Pointer to the Salt.
2068 @param[in] IterationCount Number of iterations to perform. Its value should be
2069 greater than or equal to 1.
2070 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
2071 NOTE: DigestSize will be used to determine the hash algorithm.
2072 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
2073 @param[in] KeyLength Size of the derived key buffer in bytes.
2074 @param[out] OutKey Pointer to the output derived key buffer.
2075
2076 @retval TRUE A key was derived successfully.
2077 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
2078 @retval FALSE The hash algorithm could not be determined from the digest size.
2079 @retval FALSE The key derivation operation failed.
2080 @retval FALSE This interface is not supported.
2081
2082 **/
2083 BOOLEAN
2084 EFIAPI
2085 Pkcs5HashPassword (
2086 IN UINTN PasswordLength,
2087 IN CONST CHAR8 *Password,
2088 IN UINTN SaltLength,
2089 IN CONST UINT8 *Salt,
2090 IN UINTN IterationCount,
2091 IN UINTN DigestSize,
2092 IN UINTN KeyLength,
2093 OUT UINT8 *OutKey
2094 );
2095
2096 /**
2097 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
2098 encrypted message in a newly allocated buffer.
2099
2100 Things that can cause a failure include:
2101 - X509 key size does not match any known key size.
2102 - Fail to parse X509 certificate.
2103 - Fail to allocate an intermediate buffer.
2104 - Null pointer provided for a non-optional parameter.
2105 - Data size is too large for the provided key size (max size is a function of key size
2106 and hash digest size).
2107
2108 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
2109 will be used to encrypt the data.
2110 @param[in] PublicKeySize Size of the X509 cert buffer.
2111 @param[in] InData Data to be encrypted.
2112 @param[in] InDataSize Size of the data buffer.
2113 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
2114 to be used when initializing the PRNG. NULL otherwise.
2115 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
2116 0 otherwise.
2117 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
2118 message.
2119 @param[out] EncryptedDataSize Size of the encrypted message buffer.
2120
2121 @retval TRUE Encryption was successful.
2122 @retval FALSE Encryption failed.
2123
2124 **/
2125 BOOLEAN
2126 EFIAPI
2127 Pkcs1v2Encrypt (
2128 IN CONST UINT8 *PublicKey,
2129 IN UINTN PublicKeySize,
2130 IN UINT8 *InData,
2131 IN UINTN InDataSize,
2132 IN CONST UINT8 *PrngSeed OPTIONAL,
2133 IN UINTN PrngSeedSize OPTIONAL,
2134 OUT UINT8 **EncryptedData,
2135 OUT UINTN *EncryptedDataSize
2136 );
2137
2138 /**
2139 The 3rd parameter of Pkcs7GetSigners will return all embedded
2140 X.509 certificate in one given PKCS7 signature. The format is:
2141 //
2142 // UINT8 CertNumber;
2143 // UINT32 Cert1Length;
2144 // UINT8 Cert1[];
2145 // UINT32 Cert2Length;
2146 // UINT8 Cert2[];
2147 // ...
2148 // UINT32 CertnLength;
2149 // UINT8 Certn[];
2150 //
2151
2152 The two following C-structure are used for parsing CertStack more clearly.
2153 **/
2154 #pragma pack(1)
2155
2156 typedef struct {
2157 UINT32 CertDataLength; // The length in bytes of X.509 certificate.
2158 UINT8 CertDataBuffer[0]; // The X.509 certificate content (DER).
2159 } EFI_CERT_DATA;
2160
2161 typedef struct {
2162 UINT8 CertNumber; // Number of X.509 certificate.
2163 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
2164 } EFI_CERT_STACK;
2165
2166 #pragma pack()
2167
2168 /**
2169 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
2170 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2171 in a ContentInfo structure.
2172
2173 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
2174 return FALSE. If P7Length overflow, then return FALSE.
2175 If this interface is not supported, then return FALSE.
2176
2177 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2178 @param[in] P7Length Length of the PKCS#7 message in bytes.
2179 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
2180 It's caller's responsibility to free the buffer with
2181 Pkcs7FreeSigners().
2182 This data structure is EFI_CERT_STACK type.
2183 @param[out] StackLength Length of signer's certificates in bytes.
2184 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
2185 It's caller's responsibility to free the buffer with
2186 Pkcs7FreeSigners().
2187 @param[out] CertLength Length of the trusted certificate in bytes.
2188
2189 @retval TRUE The operation is finished successfully.
2190 @retval FALSE Error occurs during the operation.
2191 @retval FALSE This interface is not supported.
2192
2193 **/
2194 BOOLEAN
2195 EFIAPI
2196 Pkcs7GetSigners (
2197 IN CONST UINT8 *P7Data,
2198 IN UINTN P7Length,
2199 OUT UINT8 **CertStack,
2200 OUT UINTN *StackLength,
2201 OUT UINT8 **TrustedCert,
2202 OUT UINTN *CertLength
2203 );
2204
2205 /**
2206 Wrap function to use free() to free allocated memory for certificates.
2207
2208 If this interface is not supported, then ASSERT().
2209
2210 @param[in] Certs Pointer to the certificates to be freed.
2211
2212 **/
2213 VOID
2214 EFIAPI
2215 Pkcs7FreeSigners (
2216 IN UINT8 *Certs
2217 );
2218
2219 /**
2220 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
2221 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
2222 unchained to the signer's certificates.
2223 The input signed data could be wrapped in a ContentInfo structure.
2224
2225 @param[in] P7Data Pointer to the PKCS#7 message.
2226 @param[in] P7Length Length of the PKCS#7 message in bytes.
2227 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
2228 certificate. It's caller's responsibility to free the buffer
2229 with Pkcs7FreeSigners().
2230 This data structure is EFI_CERT_STACK type.
2231 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
2232 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
2233 responsibility to free the buffer with Pkcs7FreeSigners().
2234 This data structure is EFI_CERT_STACK type.
2235 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
2236
2237 @retval TRUE The operation is finished successfully.
2238 @retval FALSE Error occurs during the operation.
2239
2240 **/
2241 BOOLEAN
2242 EFIAPI
2243 Pkcs7GetCertificatesList (
2244 IN CONST UINT8 *P7Data,
2245 IN UINTN P7Length,
2246 OUT UINT8 **SignerChainCerts,
2247 OUT UINTN *ChainLength,
2248 OUT UINT8 **UnchainCerts,
2249 OUT UINTN *UnchainLength
2250 );
2251
2252 /**
2253 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
2254 Syntax Standard, version 1.5". This interface is only intended to be used for
2255 application to perform PKCS#7 functionality validation.
2256
2257 If this interface is not supported, then return FALSE.
2258
2259 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
2260 data signing.
2261 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
2262 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
2263 key data.
2264 @param[in] InData Pointer to the content to be signed.
2265 @param[in] InDataSize Size of InData in bytes.
2266 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
2267 @param[in] OtherCerts Pointer to an optional additional set of certificates to
2268 include in the PKCS#7 signedData (e.g. any intermediate
2269 CAs in the chain).
2270 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
2271 responsibility to free the buffer with FreePool().
2272 @param[out] SignedDataSize Size of SignedData in bytes.
2273
2274 @retval TRUE PKCS#7 data signing succeeded.
2275 @retval FALSE PKCS#7 data signing failed.
2276 @retval FALSE This interface is not supported.
2277
2278 **/
2279 BOOLEAN
2280 EFIAPI
2281 Pkcs7Sign (
2282 IN CONST UINT8 *PrivateKey,
2283 IN UINTN PrivateKeySize,
2284 IN CONST UINT8 *KeyPassword,
2285 IN UINT8 *InData,
2286 IN UINTN InDataSize,
2287 IN UINT8 *SignCert,
2288 IN UINT8 *OtherCerts OPTIONAL,
2289 OUT UINT8 **SignedData,
2290 OUT UINTN *SignedDataSize
2291 );
2292
2293 /**
2294 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
2295 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2296 in a ContentInfo structure.
2297
2298 If P7Data, TrustedCert or InData is NULL, then return FALSE.
2299 If P7Length, CertLength or DataLength overflow, then return FALSE.
2300 If this interface is not supported, then return FALSE.
2301
2302 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2303 @param[in] P7Length Length of the PKCS#7 message in bytes.
2304 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2305 is used for certificate chain verification.
2306 @param[in] CertLength Length of the trusted certificate in bytes.
2307 @param[in] InData Pointer to the content to be verified.
2308 @param[in] DataLength Length of InData in bytes.
2309
2310 @retval TRUE The specified PKCS#7 signed data is valid.
2311 @retval FALSE Invalid PKCS#7 signed data.
2312 @retval FALSE This interface is not supported.
2313
2314 **/
2315 BOOLEAN
2316 EFIAPI
2317 Pkcs7Verify (
2318 IN CONST UINT8 *P7Data,
2319 IN UINTN P7Length,
2320 IN CONST UINT8 *TrustedCert,
2321 IN UINTN CertLength,
2322 IN CONST UINT8 *InData,
2323 IN UINTN DataLength
2324 );
2325
2326 /**
2327 This function receives a PKCS7 formatted signature, and then verifies that
2328 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2329 leaf signing certificate.
2330 Note that this function does not validate the certificate chain.
2331
2332 Applications for custom EKU's are quite flexible. For example, a policy EKU
2333 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2334 certificate issued might also contain this EKU, thus constraining the
2335 sub-ordinate certificate. Other applications might allow a certificate
2336 embedded in a device to specify that other Object Identifiers (OIDs) are
2337 present which contains binary data specifying custom capabilities that
2338 the device is able to do.
2339
2340 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2341 containing the content block with both the signature,
2342 the signer's certificate, and any necessary intermediate
2343 certificates.
2344 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2345 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2346 required EKUs that must be present in the signature.
2347 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2348 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2349 must be present in the leaf signer. If it is
2350 FALSE, then we will succeed if we find any
2351 of the specified EKU's.
2352
2353 @retval EFI_SUCCESS The required EKUs were found in the signature.
2354 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2355 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2356
2357 **/
2358 RETURN_STATUS
2359 EFIAPI
2360 VerifyEKUsInPkcs7Signature (
2361 IN CONST UINT8 *Pkcs7Signature,
2362 IN CONST UINT32 SignatureSize,
2363 IN CONST CHAR8 *RequiredEKUs[],
2364 IN CONST UINT32 RequiredEKUsSize,
2365 IN BOOLEAN RequireAllPresent
2366 );
2367
2368 /**
2369 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2370 data could be wrapped in a ContentInfo structure.
2371
2372 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2373 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2374
2375 Caution: This function may receive untrusted input. So this function will do
2376 basic check for PKCS#7 data structure.
2377
2378 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2379 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2380 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2381 It's caller's responsibility to free the buffer with FreePool().
2382 @param[out] ContentSize The size of the extracted content in bytes.
2383
2384 @retval TRUE The P7Data was correctly formatted for processing.
2385 @retval FALSE The P7Data was not correctly formatted for processing.
2386
2387 **/
2388 BOOLEAN
2389 EFIAPI
2390 Pkcs7GetAttachedContent (
2391 IN CONST UINT8 *P7Data,
2392 IN UINTN P7Length,
2393 OUT VOID **Content,
2394 OUT UINTN *ContentSize
2395 );
2396
2397 /**
2398 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2399 Authenticode Portable Executable Signature Format".
2400
2401 If AuthData is NULL, then return FALSE.
2402 If ImageHash is NULL, then return FALSE.
2403 If this interface is not supported, then return FALSE.
2404
2405 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2406 PE/COFF image to be verified.
2407 @param[in] DataSize Size of the Authenticode Signature in bytes.
2408 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2409 is used for certificate chain verification.
2410 @param[in] CertSize Size of the trusted certificate in bytes.
2411 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2412 for calculating the image hash value is described in Authenticode
2413 specification.
2414 @param[in] HashSize Size of Image hash value in bytes.
2415
2416 @retval TRUE The specified Authenticode Signature is valid.
2417 @retval FALSE Invalid Authenticode Signature.
2418 @retval FALSE This interface is not supported.
2419
2420 **/
2421 BOOLEAN
2422 EFIAPI
2423 AuthenticodeVerify (
2424 IN CONST UINT8 *AuthData,
2425 IN UINTN DataSize,
2426 IN CONST UINT8 *TrustedCert,
2427 IN UINTN CertSize,
2428 IN CONST UINT8 *ImageHash,
2429 IN UINTN HashSize
2430 );
2431
2432 /**
2433 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2434 signature.
2435
2436 If AuthData is NULL, then return FALSE.
2437 If this interface is not supported, then return FALSE.
2438
2439 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2440 PE/COFF image to be verified.
2441 @param[in] DataSize Size of the Authenticode Signature in bytes.
2442 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2443 is used for TSA certificate chain verification.
2444 @param[in] CertSize Size of the trusted certificate in bytes.
2445 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2446 signature is valid.
2447
2448 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2449 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2450
2451 **/
2452 BOOLEAN
2453 EFIAPI
2454 ImageTimestampVerify (
2455 IN CONST UINT8 *AuthData,
2456 IN UINTN DataSize,
2457 IN CONST UINT8 *TsaCert,
2458 IN UINTN CertSize,
2459 OUT EFI_TIME *SigningTime
2460 );
2461
2462 // =====================================================================================
2463 // DH Key Exchange Primitive
2464 // =====================================================================================
2465
2466 /**
2467 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2468
2469 @return Pointer to the Diffie-Hellman Context that has been initialized.
2470 If the allocations fails, DhNew() returns NULL.
2471 If the interface is not supported, DhNew() returns NULL.
2472
2473 **/
2474 VOID *
2475 EFIAPI
2476 DhNew (
2477 VOID
2478 );
2479
2480 /**
2481 Release the specified DH context.
2482
2483 If the interface is not supported, then ASSERT().
2484
2485 @param[in] DhContext Pointer to the DH context to be released.
2486
2487 **/
2488 VOID
2489 EFIAPI
2490 DhFree (
2491 IN VOID *DhContext
2492 );
2493
2494 /**
2495 Generates DH parameter.
2496
2497 Given generator g, and length of prime number p in bits, this function generates p,
2498 and sets DH context according to value of g and p.
2499
2500 Before this function can be invoked, pseudorandom number generator must be correctly
2501 initialized by RandomSeed().
2502
2503 If DhContext is NULL, then return FALSE.
2504 If Prime is NULL, then return FALSE.
2505 If this interface is not supported, then return FALSE.
2506
2507 @param[in, out] DhContext Pointer to the DH context.
2508 @param[in] Generator Value of generator.
2509 @param[in] PrimeLength Length in bits of prime to be generated.
2510 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2511
2512 @retval TRUE DH parameter generation succeeded.
2513 @retval FALSE Value of Generator is not supported.
2514 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2515 @retval FALSE This interface is not supported.
2516
2517 **/
2518 BOOLEAN
2519 EFIAPI
2520 DhGenerateParameter (
2521 IN OUT VOID *DhContext,
2522 IN UINTN Generator,
2523 IN UINTN PrimeLength,
2524 OUT UINT8 *Prime
2525 );
2526
2527 /**
2528 Sets generator and prime parameters for DH.
2529
2530 Given generator g, and prime number p, this function and sets DH
2531 context accordingly.
2532
2533 If DhContext is NULL, then return FALSE.
2534 If Prime is NULL, then return FALSE.
2535 If this interface is not supported, then return FALSE.
2536
2537 @param[in, out] DhContext Pointer to the DH context.
2538 @param[in] Generator Value of generator.
2539 @param[in] PrimeLength Length in bits of prime to be generated.
2540 @param[in] Prime Pointer to the prime number.
2541
2542 @retval TRUE DH parameter setting succeeded.
2543 @retval FALSE Value of Generator is not supported.
2544 @retval FALSE Value of Generator is not suitable for the Prime.
2545 @retval FALSE Value of Prime is not a prime number.
2546 @retval FALSE Value of Prime is not a safe prime number.
2547 @retval FALSE This interface is not supported.
2548
2549 **/
2550 BOOLEAN
2551 EFIAPI
2552 DhSetParameter (
2553 IN OUT VOID *DhContext,
2554 IN UINTN Generator,
2555 IN UINTN PrimeLength,
2556 IN CONST UINT8 *Prime
2557 );
2558
2559 /**
2560 Generates DH public key.
2561
2562 This function generates random secret exponent, and computes the public key, which is
2563 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2564 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2565 PublicKeySize is set to the required buffer size to obtain the public key.
2566
2567 If DhContext is NULL, then return FALSE.
2568 If PublicKeySize is NULL, then return FALSE.
2569 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2570 If this interface is not supported, then return FALSE.
2571
2572 @param[in, out] DhContext Pointer to the DH context.
2573 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2574 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2575 On output, the size of data returned in PublicKey buffer in bytes.
2576
2577 @retval TRUE DH public key generation succeeded.
2578 @retval FALSE DH public key generation failed.
2579 @retval FALSE PublicKeySize is not large enough.
2580 @retval FALSE This interface is not supported.
2581
2582 **/
2583 BOOLEAN
2584 EFIAPI
2585 DhGenerateKey (
2586 IN OUT VOID *DhContext,
2587 OUT UINT8 *PublicKey,
2588 IN OUT UINTN *PublicKeySize
2589 );
2590
2591 /**
2592 Computes exchanged common key.
2593
2594 Given peer's public key, this function computes the exchanged common key, based on its own
2595 context including value of prime modulus and random secret exponent.
2596
2597 If DhContext is NULL, then return FALSE.
2598 If PeerPublicKey is NULL, then return FALSE.
2599 If KeySize is NULL, then return FALSE.
2600 If Key is NULL, then return FALSE.
2601 If KeySize is not large enough, then return FALSE.
2602 If this interface is not supported, then return FALSE.
2603
2604 @param[in, out] DhContext Pointer to the DH context.
2605 @param[in] PeerPublicKey Pointer to the peer's public key.
2606 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2607 @param[out] Key Pointer to the buffer to receive generated key.
2608 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2609 On output, the size of data returned in Key buffer in bytes.
2610
2611 @retval TRUE DH exchanged key generation succeeded.
2612 @retval FALSE DH exchanged key generation failed.
2613 @retval FALSE KeySize is not large enough.
2614 @retval FALSE This interface is not supported.
2615
2616 **/
2617 BOOLEAN
2618 EFIAPI
2619 DhComputeKey (
2620 IN OUT VOID *DhContext,
2621 IN CONST UINT8 *PeerPublicKey,
2622 IN UINTN PeerPublicKeySize,
2623 OUT UINT8 *Key,
2624 IN OUT UINTN *KeySize
2625 );
2626
2627 // =====================================================================================
2628 // Pseudo-Random Generation Primitive
2629 // =====================================================================================
2630
2631 /**
2632 Sets up the seed value for the pseudorandom number generator.
2633
2634 This function sets up the seed value for the pseudorandom number generator.
2635 If Seed is not NULL, then the seed passed in is used.
2636 If Seed is NULL, then default seed is used.
2637 If this interface is not supported, then return FALSE.
2638
2639 @param[in] Seed Pointer to seed value.
2640 If NULL, default seed is used.
2641 @param[in] SeedSize Size of seed value.
2642 If Seed is NULL, this parameter is ignored.
2643
2644 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2645 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2646 @retval FALSE This interface is not supported.
2647
2648 **/
2649 BOOLEAN
2650 EFIAPI
2651 RandomSeed (
2652 IN CONST UINT8 *Seed OPTIONAL,
2653 IN UINTN SeedSize
2654 );
2655
2656 /**
2657 Generates a pseudorandom byte stream of the specified size.
2658
2659 If Output is NULL, then return FALSE.
2660 If this interface is not supported, then return FALSE.
2661
2662 @param[out] Output Pointer to buffer to receive random value.
2663 @param[in] Size Size of random bytes to generate.
2664
2665 @retval TRUE Pseudorandom byte stream generated successfully.
2666 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2667 @retval FALSE This interface is not supported.
2668
2669 **/
2670 BOOLEAN
2671 EFIAPI
2672 RandomBytes (
2673 OUT UINT8 *Output,
2674 IN UINTN Size
2675 );
2676
2677 // =====================================================================================
2678 // Key Derivation Function Primitive
2679 // =====================================================================================
2680
2681 /**
2682 Derive key data using HMAC-SHA256 based KDF.
2683
2684 @param[in] Key Pointer to the user-supplied key.
2685 @param[in] KeySize Key size in bytes.
2686 @param[in] Salt Pointer to the salt(non-secret) value.
2687 @param[in] SaltSize Salt size in bytes.
2688 @param[in] Info Pointer to the application specific info.
2689 @param[in] InfoSize Info size in bytes.
2690 @param[out] Out Pointer to buffer to receive hkdf value.
2691 @param[in] OutSize Size of hkdf bytes to generate.
2692
2693 @retval TRUE Hkdf generated successfully.
2694 @retval FALSE Hkdf generation failed.
2695
2696 **/
2697 BOOLEAN
2698 EFIAPI
2699 HkdfSha256ExtractAndExpand (
2700 IN CONST UINT8 *Key,
2701 IN UINTN KeySize,
2702 IN CONST UINT8 *Salt,
2703 IN UINTN SaltSize,
2704 IN CONST UINT8 *Info,
2705 IN UINTN InfoSize,
2706 OUT UINT8 *Out,
2707 IN UINTN OutSize
2708 );
2709
2710 /**
2711 Derive SHA256 HMAC-based Extract key Derivation Function (HKDF).
2712
2713 @param[in] Key Pointer to the user-supplied key.
2714 @param[in] KeySize key size in bytes.
2715 @param[in] Salt Pointer to the salt(non-secret) value.
2716 @param[in] SaltSize salt size in bytes.
2717 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2718 @param[in] PrkOutSize size of hkdf bytes to generate.
2719
2720 @retval true Hkdf generated successfully.
2721 @retval false Hkdf generation failed.
2722
2723 **/
2724 BOOLEAN
2725 EFIAPI
2726 HkdfSha256Extract (
2727 IN CONST UINT8 *Key,
2728 IN UINTN KeySize,
2729 IN CONST UINT8 *Salt,
2730 IN UINTN SaltSize,
2731 OUT UINT8 *PrkOut,
2732 UINTN PrkOutSize
2733 );
2734
2735 /**
2736 Derive SHA256 HMAC-based Expand Key Derivation Function (HKDF).
2737
2738 @param[in] Prk Pointer to the user-supplied key.
2739 @param[in] PrkSize Key size in bytes.
2740 @param[in] Info Pointer to the application specific info.
2741 @param[in] InfoSize Info size in bytes.
2742 @param[out] Out Pointer to buffer to receive hkdf value.
2743 @param[in] OutSize Size of hkdf bytes to generate.
2744
2745 @retval TRUE Hkdf generated successfully.
2746 @retval FALSE Hkdf generation failed.
2747
2748 **/
2749 BOOLEAN
2750 EFIAPI
2751 HkdfSha256Expand (
2752 IN CONST UINT8 *Prk,
2753 IN UINTN PrkSize,
2754 IN CONST UINT8 *Info,
2755 IN UINTN InfoSize,
2756 OUT UINT8 *Out,
2757 IN UINTN OutSize
2758 );
2759
2760 /**
2761 Derive SHA384 HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
2762
2763 @param[in] Key Pointer to the user-supplied key.
2764 @param[in] KeySize Key size in bytes.
2765 @param[in] Salt Pointer to the salt(non-secret) value.
2766 @param[in] SaltSize Salt size in bytes.
2767 @param[in] Info Pointer to the application specific info.
2768 @param[in] InfoSize Info size in bytes.
2769 @param[out] Out Pointer to buffer to receive hkdf value.
2770 @param[in] OutSize Size of hkdf bytes to generate.
2771
2772 @retval TRUE Hkdf generated successfully.
2773 @retval FALSE Hkdf generation failed.
2774
2775 **/
2776 BOOLEAN
2777 EFIAPI
2778 HkdfSha384ExtractAndExpand (
2779 IN CONST UINT8 *Key,
2780 IN UINTN KeySize,
2781 IN CONST UINT8 *Salt,
2782 IN UINTN SaltSize,
2783 IN CONST UINT8 *Info,
2784 IN UINTN InfoSize,
2785 OUT UINT8 *Out,
2786 IN UINTN OutSize
2787 );
2788
2789 /**
2790 Derive SHA384 HMAC-based Extract key Derivation Function (HKDF).
2791
2792 @param[in] Key Pointer to the user-supplied key.
2793 @param[in] KeySize key size in bytes.
2794 @param[in] Salt Pointer to the salt(non-secret) value.
2795 @param[in] SaltSize salt size in bytes.
2796 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2797 @param[in] PrkOutSize size of hkdf bytes to generate.
2798
2799 @retval true Hkdf generated successfully.
2800 @retval false Hkdf generation failed.
2801
2802 **/
2803 BOOLEAN
2804 EFIAPI
2805 HkdfSha384Extract (
2806 IN CONST UINT8 *Key,
2807 IN UINTN KeySize,
2808 IN CONST UINT8 *Salt,
2809 IN UINTN SaltSize,
2810 OUT UINT8 *PrkOut,
2811 UINTN PrkOutSize
2812 );
2813
2814 /**
2815 Derive SHA384 HMAC-based Expand Key Derivation Function (HKDF).
2816
2817 @param[in] Prk Pointer to the user-supplied key.
2818 @param[in] PrkSize Key size in bytes.
2819 @param[in] Info Pointer to the application specific info.
2820 @param[in] InfoSize Info size in bytes.
2821 @param[out] Out Pointer to buffer to receive hkdf value.
2822 @param[in] OutSize Size of hkdf bytes to generate.
2823
2824 @retval TRUE Hkdf generated successfully.
2825 @retval FALSE Hkdf generation failed.
2826
2827 **/
2828 BOOLEAN
2829 EFIAPI
2830 HkdfSha384Expand (
2831 IN CONST UINT8 *Prk,
2832 IN UINTN PrkSize,
2833 IN CONST UINT8 *Info,
2834 IN UINTN InfoSize,
2835 OUT UINT8 *Out,
2836 IN UINTN OutSize
2837 );
2838
2839 // =====================================================================================
2840 // Big number primitives
2841 // =====================================================================================
2842
2843 /**
2844 Allocate new Big Number.
2845
2846 @retval New BigNum opaque structure or NULL on failure.
2847 **/
2848 VOID *
2849 EFIAPI
2850 BigNumInit (
2851 VOID
2852 );
2853
2854 /**
2855 Allocate new Big Number and assign the provided value to it.
2856
2857 @param[in] Buf Big endian encoded buffer.
2858 @param[in] Len Buffer length.
2859
2860 @retval New BigNum opaque structure or NULL on failure.
2861 **/
2862 VOID *
2863 EFIAPI
2864 BigNumFromBin (
2865 IN CONST UINT8 *Buf,
2866 IN UINTN Len
2867 );
2868
2869 /**
2870 Convert the absolute value of Bn into big-endian form and store it at Buf.
2871 The Buf array should have at least BigNumBytes() in it.
2872
2873 @param[in] Bn Big number to convert.
2874 @param[out] Buf Output buffer.
2875
2876 @retval The length of the big-endian number placed at Buf or -1 on error.
2877 **/
2878 INTN
2879 EFIAPI
2880 BigNumToBin (
2881 IN CONST VOID *Bn,
2882 OUT UINT8 *Buf
2883 );
2884
2885 /**
2886 Free the Big Number.
2887
2888 @param[in] Bn Big number to free.
2889 @param[in] Clear TRUE if the buffer should be cleared.
2890 **/
2891 VOID
2892 EFIAPI
2893 BigNumFree (
2894 IN VOID *Bn,
2895 IN BOOLEAN Clear
2896 );
2897
2898 /**
2899 Calculate the sum of two Big Numbers.
2900 Please note, all "out" Big number arguments should be properly initialized
2901 by calling to BigNumInit() or BigNumFromBin() functions.
2902
2903 @param[in] BnA Big number.
2904 @param[in] BnB Big number.
2905 @param[out] BnRes The result of BnA + BnB.
2906
2907 @retval TRUE On success.
2908 @retval FALSE Otherwise.
2909 **/
2910 BOOLEAN
2911 EFIAPI
2912 BigNumAdd (
2913 IN CONST VOID *BnA,
2914 IN CONST VOID *BnB,
2915 OUT VOID *BnRes
2916 );
2917
2918 /**
2919 Subtract two Big Numbers.
2920 Please note, all "out" Big number arguments should be properly initialized
2921 by calling to BigNumInit() or BigNumFromBin() functions.
2922
2923 @param[in] BnA Big number.
2924 @param[in] BnB Big number.
2925 @param[out] BnRes The result of BnA - BnB.
2926
2927 @retval TRUE On success.
2928 @retval FALSE Otherwise.
2929 **/
2930 BOOLEAN
2931 EFIAPI
2932 BigNumSub (
2933 IN CONST VOID *BnA,
2934 IN CONST VOID *BnB,
2935 OUT VOID *BnRes
2936 );
2937
2938 /**
2939 Calculate remainder: BnRes = BnA % BnB.
2940 Please note, all "out" Big number arguments should be properly initialized
2941 by calling to BigNumInit() or BigNumFromBin() functions.
2942
2943 @param[in] BnA Big number.
2944 @param[in] BnB Big number.
2945 @param[out] BnRes The result of BnA % BnB.
2946
2947 @retval TRUE On success.
2948 @retval FALSE Otherwise.
2949 **/
2950 BOOLEAN
2951 EFIAPI
2952 BigNumMod (
2953 IN CONST VOID *BnA,
2954 IN CONST VOID *BnB,
2955 OUT VOID *BnRes
2956 );
2957
2958 /**
2959 Compute BnA to the BnP-th power modulo BnM.
2960 Please note, all "out" Big number arguments should be properly initialized
2961 by calling to BigNumInit() or BigNumFromBin() functions.
2962
2963 @param[in] BnA Big number.
2964 @param[in] BnP Big number (power).
2965 @param[in] BnM Big number (modulo).
2966 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
2967
2968 @retval TRUE On success.
2969 @retval FALSE Otherwise.
2970 **/
2971 BOOLEAN
2972 EFIAPI
2973 BigNumExpMod (
2974 IN CONST VOID *BnA,
2975 IN CONST VOID *BnP,
2976 IN CONST VOID *BnM,
2977 OUT VOID *BnRes
2978 );
2979
2980 /**
2981 Compute BnA inverse modulo BnM.
2982 Please note, all "out" Big number arguments should be properly initialized
2983 by calling to BigNumInit() or BigNumFromBin() functions.
2984
2985 @param[in] BnA Big number.
2986 @param[in] BnM Big number (modulo).
2987 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
2988
2989 @retval TRUE On success.
2990 @retval FALSE Otherwise.
2991 **/
2992 BOOLEAN
2993 EFIAPI
2994 BigNumInverseMod (
2995 IN CONST VOID *BnA,
2996 IN CONST VOID *BnM,
2997 OUT VOID *BnRes
2998 );
2999
3000 /**
3001 Divide two Big Numbers.
3002 Please note, all "out" Big number arguments should be properly initialized
3003 by calling to BigNumInit() or BigNumFromBin() functions.
3004
3005 @param[in] BnA Big number.
3006 @param[in] BnB Big number.
3007 @param[out] BnRes The result, such that BnA / BnB.
3008
3009 @retval TRUE On success.
3010 @retval FALSE Otherwise.
3011 **/
3012 BOOLEAN
3013 EFIAPI
3014 BigNumDiv (
3015 IN CONST VOID *BnA,
3016 IN CONST VOID *BnB,
3017 OUT VOID *BnRes
3018 );
3019
3020 /**
3021 Multiply two Big Numbers modulo BnM.
3022 Please note, all "out" Big number arguments should be properly initialized
3023 by calling to BigNumInit() or BigNumFromBin() functions.
3024
3025 @param[in] BnA Big number.
3026 @param[in] BnB Big number.
3027 @param[in] BnM Big number (modulo).
3028 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
3029
3030 @retval TRUE On success.
3031 @retval FALSE Otherwise.
3032 **/
3033 BOOLEAN
3034 EFIAPI
3035 BigNumMulMod (
3036 IN CONST VOID *BnA,
3037 IN CONST VOID *BnB,
3038 IN CONST VOID *BnM,
3039 OUT VOID *BnRes
3040 );
3041
3042 /**
3043 Compare two Big Numbers.
3044
3045 @param[in] BnA Big number.
3046 @param[in] BnB Big number.
3047
3048 @retval 0 BnA == BnB.
3049 @retval 1 BnA > BnB.
3050 @retval -1 BnA < BnB.
3051 **/
3052 INTN
3053 EFIAPI
3054 BigNumCmp (
3055 IN CONST VOID *BnA,
3056 IN CONST VOID *BnB
3057 );
3058
3059 /**
3060 Get number of bits in Bn.
3061
3062 @param[in] Bn Big number.
3063
3064 @retval Number of bits.
3065 **/
3066
3067 UINTN
3068 EFIAPI
3069 BigNumBits (
3070 IN CONST VOID *Bn
3071 );
3072
3073 /**
3074 Get number of bytes in Bn.
3075
3076 @param[in] Bn Big number.
3077
3078 @retval Number of bytes.
3079 **/
3080 UINTN
3081 EFIAPI
3082 BigNumBytes (
3083 IN CONST VOID *Bn
3084 );
3085
3086 /**
3087 Checks if Big Number equals to the given Num.
3088
3089 @param[in] Bn Big number.
3090 @param[in] Num Number.
3091
3092 @retval TRUE iff Bn == Num.
3093 @retval FALSE otherwise.
3094 **/
3095 BOOLEAN
3096 EFIAPI
3097 BigNumIsWord (
3098 IN CONST VOID *Bn,
3099 IN UINTN Num
3100 );
3101
3102 /**
3103 Checks if Big Number is odd.
3104
3105 @param[in] Bn Big number.
3106
3107 @retval TRUE Bn is odd (Bn % 2 == 1).
3108 @retval FALSE otherwise.
3109 **/
3110 BOOLEAN
3111 EFIAPI
3112 BigNumIsOdd (
3113 IN CONST VOID *Bn
3114 );
3115
3116 /**
3117 Copy Big number.
3118
3119 @param[out] BnDst Destination.
3120 @param[in] BnSrc Source.
3121
3122 @retval BnDst on success.
3123 @retval NULL otherwise.
3124 **/
3125 VOID *
3126 EFIAPI
3127 BigNumCopy (
3128 OUT VOID *BnDst,
3129 IN CONST VOID *BnSrc
3130 );
3131
3132 /**
3133 Get constant Big number with value of "1".
3134 This may be used to save expensive allocations.
3135
3136 @retval Big Number with value of 1.
3137 **/
3138 CONST VOID *
3139 EFIAPI
3140 BigNumValueOne (
3141 VOID
3142 );
3143
3144 /**
3145 Shift right Big Number.
3146 Please note, all "out" Big number arguments should be properly initialized
3147 by calling to BigNumInit() or BigNumFromBin() functions.
3148
3149 @param[in] Bn Big number.
3150 @param[in] N Number of bits to shift.
3151 @param[out] BnRes The result.
3152
3153 @retval TRUE On success.
3154 @retval FALSE Otherwise.
3155 **/
3156 BOOLEAN
3157 EFIAPI
3158 BigNumRShift (
3159 IN CONST VOID *Bn,
3160 IN UINTN N,
3161 OUT VOID *BnRes
3162 );
3163
3164 /**
3165 Mark Big Number for constant time computations.
3166 This function should be called before any constant time computations are
3167 performed on the given Big number.
3168
3169 @param[in] Bn Big number.
3170 **/
3171 VOID
3172 EFIAPI
3173 BigNumConstTime (
3174 IN VOID *Bn
3175 );
3176
3177 /**
3178 Calculate square modulo.
3179 Please note, all "out" Big number arguments should be properly initialized
3180 by calling to BigNumInit() or BigNumFromBin() functions.
3181
3182 @param[in] BnA Big number.
3183 @param[in] BnM Big number (modulo).
3184 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
3185
3186 @retval TRUE On success.
3187 @retval FALSE Otherwise.
3188 **/
3189 BOOLEAN
3190 EFIAPI
3191 BigNumSqrMod (
3192 IN CONST VOID *BnA,
3193 IN CONST VOID *BnM,
3194 OUT VOID *BnRes
3195 );
3196
3197 /**
3198 Create new Big Number computation context. This is an opaque structure
3199 which should be passed to any function that requires it. The BN context is
3200 needed to optimize calculations and expensive allocations.
3201
3202 @retval Big Number context struct or NULL on failure.
3203 **/
3204 VOID *
3205 EFIAPI
3206 BigNumNewContext (
3207 VOID
3208 );
3209
3210 /**
3211 Free Big Number context that was allocated with BigNumNewContext().
3212
3213 @param[in] BnCtx Big number context to free.
3214 **/
3215 VOID
3216 EFIAPI
3217 BigNumContextFree (
3218 IN VOID *BnCtx
3219 );
3220
3221 /**
3222 Set Big Number to a given value.
3223
3224 @param[in] Bn Big number to set.
3225 @param[in] Val Value to set.
3226
3227 @retval TRUE On success.
3228 @retval FALSE Otherwise.
3229 **/
3230 BOOLEAN
3231 EFIAPI
3232 BigNumSetUint (
3233 IN VOID *Bn,
3234 IN UINTN Val
3235 );
3236
3237 /**
3238 Add two Big Numbers modulo BnM.
3239
3240 @param[in] BnA Big number.
3241 @param[in] BnB Big number.
3242 @param[in] BnM Big number (modulo).
3243 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
3244
3245 @retval TRUE On success.
3246 @retval FALSE Otherwise.
3247 **/
3248 BOOLEAN
3249 EFIAPI
3250 BigNumAddMod (
3251 IN CONST VOID *BnA,
3252 IN CONST VOID *BnB,
3253 IN CONST VOID *BnM,
3254 OUT VOID *BnRes
3255 );
3256
3257 #endif // __BASE_CRYPT_LIB_H__