]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Include/Library/BaseCryptLib.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 #define CRYPTO_NID_NULL 0x0000
18
19 // Hash
20 #define CRYPTO_NID_SHA256 0x0001
21 #define CRYPTO_NID_SHA384 0x0002
22 #define CRYPTO_NID_SHA512 0x0003
23
24 // Key Exchange
25 #define CRYPTO_NID_SECP256R1 0x0204
26 #define CRYPTO_NID_SECP384R1 0x0205
27 #define CRYPTO_NID_SECP521R1 0x0206
28
29 ///
30 /// MD5 digest size in bytes
31 ///
32 #define MD5_DIGEST_SIZE 16
33
34 ///
35 /// SHA-1 digest size in bytes.
36 ///
37 #define SHA1_DIGEST_SIZE 20
38
39 ///
40 /// SHA-256 digest size in bytes
41 ///
42 #define SHA256_DIGEST_SIZE 32
43
44 ///
45 /// SHA-384 digest size in bytes
46 ///
47 #define SHA384_DIGEST_SIZE 48
48
49 ///
50 /// SHA-512 digest size in bytes
51 ///
52 #define SHA512_DIGEST_SIZE 64
53
54 ///
55 /// SM3 digest size in bytes
56 ///
57 #define SM3_256_DIGEST_SIZE 32
58
59 ///
60 /// TDES block size in bytes
61 ///
62 #define TDES_BLOCK_SIZE 8
63
64 ///
65 /// AES block size in bytes
66 ///
67 #define AES_BLOCK_SIZE 16
68
69 ///
70 /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
71 ///
72 typedef enum {
73 RsaKeyN, ///< RSA public Modulus (N)
74 RsaKeyE, ///< RSA Public exponent (e)
75 RsaKeyD, ///< RSA Private exponent (d)
76 RsaKeyP, ///< RSA secret prime factor of Modulus (p)
77 RsaKeyQ, ///< RSA secret prime factor of Modules (q)
78 RsaKeyDp, ///< p's CRT exponent (== d mod (p - 1))
79 RsaKeyDq, ///< q's CRT exponent (== d mod (q - 1))
80 RsaKeyQInv ///< The CRT coefficient (== 1/q mod p)
81 } RSA_KEY_TAG;
82
83 // =====================================================================================
84 // One-Way Cryptographic Hash Primitives
85 // =====================================================================================
86
87 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES
88
89 /**
90 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
91
92 If this interface is not supported, then return zero.
93
94 @return The size, in bytes, of the context buffer required for MD5 hash operations.
95 @retval 0 This interface is not supported.
96
97 **/
98 UINTN
99 EFIAPI
100 Md5GetContextSize (
101 VOID
102 );
103
104 /**
105 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
106 subsequent use.
107
108 If Md5Context is NULL, then return FALSE.
109 If this interface is not supported, then return FALSE.
110
111 @param[out] Md5Context Pointer to MD5 context being initialized.
112
113 @retval TRUE MD5 context initialization succeeded.
114 @retval FALSE MD5 context initialization failed.
115 @retval FALSE This interface is not supported.
116
117 **/
118 BOOLEAN
119 EFIAPI
120 Md5Init (
121 OUT VOID *Md5Context
122 );
123
124 /**
125 Makes a copy of an existing MD5 context.
126
127 If Md5Context is NULL, then return FALSE.
128 If NewMd5Context is NULL, then return FALSE.
129 If this interface is not supported, then return FALSE.
130
131 @param[in] Md5Context Pointer to MD5 context being copied.
132 @param[out] NewMd5Context Pointer to new MD5 context.
133
134 @retval TRUE MD5 context copy succeeded.
135 @retval FALSE MD5 context copy failed.
136 @retval FALSE This interface is not supported.
137
138 **/
139 BOOLEAN
140 EFIAPI
141 Md5Duplicate (
142 IN CONST VOID *Md5Context,
143 OUT VOID *NewMd5Context
144 );
145
146 /**
147 Digests the input data and updates MD5 context.
148
149 This function performs MD5 digest on a data buffer of the specified size.
150 It can be called multiple times to compute the digest of long or discontinuous data streams.
151 MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
152 by Md5Final(). Behavior with invalid context is undefined.
153
154 If Md5Context is NULL, then return FALSE.
155 If this interface is not supported, then return FALSE.
156
157 @param[in, out] Md5Context Pointer to the MD5 context.
158 @param[in] Data Pointer to the buffer containing the data to be hashed.
159 @param[in] DataSize Size of Data buffer in bytes.
160
161 @retval TRUE MD5 data digest succeeded.
162 @retval FALSE MD5 data digest failed.
163 @retval FALSE This interface is not supported.
164
165 **/
166 BOOLEAN
167 EFIAPI
168 Md5Update (
169 IN OUT VOID *Md5Context,
170 IN CONST VOID *Data,
171 IN UINTN DataSize
172 );
173
174 /**
175 Completes computation of the MD5 digest value.
176
177 This function completes MD5 hash computation and retrieves the digest value into
178 the specified memory. After this function has been called, the MD5 context cannot
179 be used again.
180 MD5 context should be already correctly initialized by Md5Init(), and should not be
181 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
182
183 If Md5Context is NULL, then return FALSE.
184 If HashValue is NULL, then return FALSE.
185 If this interface is not supported, then return FALSE.
186
187 @param[in, out] Md5Context Pointer to the MD5 context.
188 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
189 value (16 bytes).
190
191 @retval TRUE MD5 digest computation succeeded.
192 @retval FALSE MD5 digest computation failed.
193 @retval FALSE This interface is not supported.
194
195 **/
196 BOOLEAN
197 EFIAPI
198 Md5Final (
199 IN OUT VOID *Md5Context,
200 OUT UINT8 *HashValue
201 );
202
203 /**
204 Computes the MD5 message digest of a input data buffer.
205
206 This function performs the MD5 message digest of a given data buffer, and places
207 the digest value into the specified memory.
208
209 If this interface is not supported, then return FALSE.
210
211 @param[in] Data Pointer to the buffer containing the data to be hashed.
212 @param[in] DataSize Size of Data buffer in bytes.
213 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
214 value (16 bytes).
215
216 @retval TRUE MD5 digest computation succeeded.
217 @retval FALSE MD5 digest computation failed.
218 @retval FALSE This interface is not supported.
219
220 **/
221 BOOLEAN
222 EFIAPI
223 Md5HashAll (
224 IN CONST VOID *Data,
225 IN UINTN DataSize,
226 OUT UINT8 *HashValue
227 );
228
229 #endif
230
231 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
232
233 /**
234 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
235
236 If this interface is not supported, then return zero.
237
238 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
239 @retval 0 This interface is not supported.
240
241 **/
242 UINTN
243 EFIAPI
244 Sha1GetContextSize (
245 VOID
246 );
247
248 /**
249 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
250 subsequent use.
251
252 If Sha1Context is NULL, then return FALSE.
253 If this interface is not supported, then return FALSE.
254
255 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
256
257 @retval TRUE SHA-1 context initialization succeeded.
258 @retval FALSE SHA-1 context initialization failed.
259 @retval FALSE This interface is not supported.
260
261 **/
262 BOOLEAN
263 EFIAPI
264 Sha1Init (
265 OUT VOID *Sha1Context
266 );
267
268 /**
269 Makes a copy of an existing SHA-1 context.
270
271 If Sha1Context is NULL, then return FALSE.
272 If NewSha1Context is NULL, then return FALSE.
273 If this interface is not supported, then return FALSE.
274
275 @param[in] Sha1Context Pointer to SHA-1 context being copied.
276 @param[out] NewSha1Context Pointer to new SHA-1 context.
277
278 @retval TRUE SHA-1 context copy succeeded.
279 @retval FALSE SHA-1 context copy failed.
280 @retval FALSE This interface is not supported.
281
282 **/
283 BOOLEAN
284 EFIAPI
285 Sha1Duplicate (
286 IN CONST VOID *Sha1Context,
287 OUT VOID *NewSha1Context
288 );
289
290 /**
291 Digests the input data and updates SHA-1 context.
292
293 This function performs SHA-1 digest on a data buffer of the specified size.
294 It can be called multiple times to compute the digest of long or discontinuous data streams.
295 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
296 by Sha1Final(). Behavior with invalid context is undefined.
297
298 If Sha1Context is NULL, then return FALSE.
299 If this interface is not supported, then return FALSE.
300
301 @param[in, out] Sha1Context Pointer to the SHA-1 context.
302 @param[in] Data Pointer to the buffer containing the data to be hashed.
303 @param[in] DataSize Size of Data buffer in bytes.
304
305 @retval TRUE SHA-1 data digest succeeded.
306 @retval FALSE SHA-1 data digest failed.
307 @retval FALSE This interface is not supported.
308
309 **/
310 BOOLEAN
311 EFIAPI
312 Sha1Update (
313 IN OUT VOID *Sha1Context,
314 IN CONST VOID *Data,
315 IN UINTN DataSize
316 );
317
318 /**
319 Completes computation of the SHA-1 digest value.
320
321 This function completes SHA-1 hash computation and retrieves the digest value into
322 the specified memory. After this function has been called, the SHA-1 context cannot
323 be used again.
324 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
325 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
326
327 If Sha1Context is NULL, then return FALSE.
328 If HashValue is NULL, then return FALSE.
329 If this interface is not supported, then return FALSE.
330
331 @param[in, out] Sha1Context Pointer to the SHA-1 context.
332 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
333 value (20 bytes).
334
335 @retval TRUE SHA-1 digest computation succeeded.
336 @retval FALSE SHA-1 digest computation failed.
337 @retval FALSE This interface is not supported.
338
339 **/
340 BOOLEAN
341 EFIAPI
342 Sha1Final (
343 IN OUT VOID *Sha1Context,
344 OUT UINT8 *HashValue
345 );
346
347 /**
348 Computes the SHA-1 message digest of a input data buffer.
349
350 This function performs the SHA-1 message digest of a given data buffer, and places
351 the digest value into the specified memory.
352
353 If this interface is not supported, then return FALSE.
354
355 @param[in] Data Pointer to the buffer containing the data to be hashed.
356 @param[in] DataSize Size of Data buffer in bytes.
357 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
358 value (20 bytes).
359
360 @retval TRUE SHA-1 digest computation succeeded.
361 @retval FALSE SHA-1 digest computation failed.
362 @retval FALSE This interface is not supported.
363
364 **/
365 BOOLEAN
366 EFIAPI
367 Sha1HashAll (
368 IN CONST VOID *Data,
369 IN UINTN DataSize,
370 OUT UINT8 *HashValue
371 );
372
373 #endif
374
375 /**
376 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
377
378 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
379
380 **/
381 UINTN
382 EFIAPI
383 Sha256GetContextSize (
384 VOID
385 );
386
387 /**
388 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
389 subsequent use.
390
391 If Sha256Context is NULL, then return FALSE.
392
393 @param[out] Sha256Context Pointer to SHA-256 context being initialized.
394
395 @retval TRUE SHA-256 context initialization succeeded.
396 @retval FALSE SHA-256 context initialization failed.
397
398 **/
399 BOOLEAN
400 EFIAPI
401 Sha256Init (
402 OUT VOID *Sha256Context
403 );
404
405 /**
406 Makes a copy of an existing SHA-256 context.
407
408 If Sha256Context is NULL, then return FALSE.
409 If NewSha256Context is NULL, then return FALSE.
410 If this interface is not supported, then return FALSE.
411
412 @param[in] Sha256Context Pointer to SHA-256 context being copied.
413 @param[out] NewSha256Context Pointer to new SHA-256 context.
414
415 @retval TRUE SHA-256 context copy succeeded.
416 @retval FALSE SHA-256 context copy failed.
417 @retval FALSE This interface is not supported.
418
419 **/
420 BOOLEAN
421 EFIAPI
422 Sha256Duplicate (
423 IN CONST VOID *Sha256Context,
424 OUT VOID *NewSha256Context
425 );
426
427 /**
428 Digests the input data and updates SHA-256 context.
429
430 This function performs SHA-256 digest on a data buffer of the specified size.
431 It can be called multiple times to compute the digest of long or discontinuous data streams.
432 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
433 by Sha256Final(). Behavior with invalid context is undefined.
434
435 If Sha256Context is NULL, then return FALSE.
436
437 @param[in, out] Sha256Context Pointer to the SHA-256 context.
438 @param[in] Data Pointer to the buffer containing the data to be hashed.
439 @param[in] DataSize Size of Data buffer in bytes.
440
441 @retval TRUE SHA-256 data digest succeeded.
442 @retval FALSE SHA-256 data digest failed.
443
444 **/
445 BOOLEAN
446 EFIAPI
447 Sha256Update (
448 IN OUT VOID *Sha256Context,
449 IN CONST VOID *Data,
450 IN UINTN DataSize
451 );
452
453 /**
454 Completes computation of the SHA-256 digest value.
455
456 This function completes SHA-256 hash computation and retrieves the digest value into
457 the specified memory. After this function has been called, the SHA-256 context cannot
458 be used again.
459 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
460 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
461
462 If Sha256Context is NULL, then return FALSE.
463 If HashValue is NULL, then return FALSE.
464
465 @param[in, out] Sha256Context Pointer to the SHA-256 context.
466 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
467 value (32 bytes).
468
469 @retval TRUE SHA-256 digest computation succeeded.
470 @retval FALSE SHA-256 digest computation failed.
471
472 **/
473 BOOLEAN
474 EFIAPI
475 Sha256Final (
476 IN OUT VOID *Sha256Context,
477 OUT UINT8 *HashValue
478 );
479
480 /**
481 Computes the SHA-256 message digest of a input data buffer.
482
483 This function performs the SHA-256 message digest of a given data buffer, and places
484 the digest value into the specified memory.
485
486 If this interface is not supported, then return FALSE.
487
488 @param[in] Data Pointer to the buffer containing the data to be hashed.
489 @param[in] DataSize Size of Data buffer in bytes.
490 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
491 value (32 bytes).
492
493 @retval TRUE SHA-256 digest computation succeeded.
494 @retval FALSE SHA-256 digest computation failed.
495 @retval FALSE This interface is not supported.
496
497 **/
498 BOOLEAN
499 EFIAPI
500 Sha256HashAll (
501 IN CONST VOID *Data,
502 IN UINTN DataSize,
503 OUT UINT8 *HashValue
504 );
505
506 /**
507 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
508
509 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.
510
511 **/
512 UINTN
513 EFIAPI
514 Sha384GetContextSize (
515 VOID
516 );
517
518 /**
519 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
520 subsequent use.
521
522 If Sha384Context is NULL, then return FALSE.
523
524 @param[out] Sha384Context Pointer to SHA-384 context being initialized.
525
526 @retval TRUE SHA-384 context initialization succeeded.
527 @retval FALSE SHA-384 context initialization failed.
528
529 **/
530 BOOLEAN
531 EFIAPI
532 Sha384Init (
533 OUT VOID *Sha384Context
534 );
535
536 /**
537 Makes a copy of an existing SHA-384 context.
538
539 If Sha384Context is NULL, then return FALSE.
540 If NewSha384Context is NULL, then return FALSE.
541 If this interface is not supported, then return FALSE.
542
543 @param[in] Sha384Context Pointer to SHA-384 context being copied.
544 @param[out] NewSha384Context Pointer to new SHA-384 context.
545
546 @retval TRUE SHA-384 context copy succeeded.
547 @retval FALSE SHA-384 context copy failed.
548 @retval FALSE This interface is not supported.
549
550 **/
551 BOOLEAN
552 EFIAPI
553 Sha384Duplicate (
554 IN CONST VOID *Sha384Context,
555 OUT VOID *NewSha384Context
556 );
557
558 /**
559 Digests the input data and updates SHA-384 context.
560
561 This function performs SHA-384 digest on a data buffer of the specified size.
562 It can be called multiple times to compute the digest of long or discontinuous data streams.
563 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized
564 by Sha384Final(). Behavior with invalid context is undefined.
565
566 If Sha384Context is NULL, then return FALSE.
567
568 @param[in, out] Sha384Context Pointer to the SHA-384 context.
569 @param[in] Data Pointer to the buffer containing the data to be hashed.
570 @param[in] DataSize Size of Data buffer in bytes.
571
572 @retval TRUE SHA-384 data digest succeeded.
573 @retval FALSE SHA-384 data digest failed.
574
575 **/
576 BOOLEAN
577 EFIAPI
578 Sha384Update (
579 IN OUT VOID *Sha384Context,
580 IN CONST VOID *Data,
581 IN UINTN DataSize
582 );
583
584 /**
585 Completes computation of the SHA-384 digest value.
586
587 This function completes SHA-384 hash computation and retrieves the digest value into
588 the specified memory. After this function has been called, the SHA-384 context cannot
589 be used again.
590 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be
591 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
592
593 If Sha384Context is NULL, then return FALSE.
594 If HashValue is NULL, then return FALSE.
595
596 @param[in, out] Sha384Context Pointer to the SHA-384 context.
597 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
598 value (48 bytes).
599
600 @retval TRUE SHA-384 digest computation succeeded.
601 @retval FALSE SHA-384 digest computation failed.
602
603 **/
604 BOOLEAN
605 EFIAPI
606 Sha384Final (
607 IN OUT VOID *Sha384Context,
608 OUT UINT8 *HashValue
609 );
610
611 /**
612 Computes the SHA-384 message digest of a input data buffer.
613
614 This function performs the SHA-384 message digest of a given data buffer, and places
615 the digest value into the specified memory.
616
617 If this interface is not supported, then return FALSE.
618
619 @param[in] Data Pointer to the buffer containing the data to be hashed.
620 @param[in] DataSize Size of Data buffer in bytes.
621 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
622 value (48 bytes).
623
624 @retval TRUE SHA-384 digest computation succeeded.
625 @retval FALSE SHA-384 digest computation failed.
626 @retval FALSE This interface is not supported.
627
628 **/
629 BOOLEAN
630 EFIAPI
631 Sha384HashAll (
632 IN CONST VOID *Data,
633 IN UINTN DataSize,
634 OUT UINT8 *HashValue
635 );
636
637 /**
638 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
639
640 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.
641
642 **/
643 UINTN
644 EFIAPI
645 Sha512GetContextSize (
646 VOID
647 );
648
649 /**
650 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
651 subsequent use.
652
653 If Sha512Context is NULL, then return FALSE.
654
655 @param[out] Sha512Context Pointer to SHA-512 context being initialized.
656
657 @retval TRUE SHA-512 context initialization succeeded.
658 @retval FALSE SHA-512 context initialization failed.
659
660 **/
661 BOOLEAN
662 EFIAPI
663 Sha512Init (
664 OUT VOID *Sha512Context
665 );
666
667 /**
668 Makes a copy of an existing SHA-512 context.
669
670 If Sha512Context is NULL, then return FALSE.
671 If NewSha512Context is NULL, then return FALSE.
672 If this interface is not supported, then return FALSE.
673
674 @param[in] Sha512Context Pointer to SHA-512 context being copied.
675 @param[out] NewSha512Context Pointer to new SHA-512 context.
676
677 @retval TRUE SHA-512 context copy succeeded.
678 @retval FALSE SHA-512 context copy failed.
679 @retval FALSE This interface is not supported.
680
681 **/
682 BOOLEAN
683 EFIAPI
684 Sha512Duplicate (
685 IN CONST VOID *Sha512Context,
686 OUT VOID *NewSha512Context
687 );
688
689 /**
690 Digests the input data and updates SHA-512 context.
691
692 This function performs SHA-512 digest on a data buffer of the specified size.
693 It can be called multiple times to compute the digest of long or discontinuous data streams.
694 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized
695 by Sha512Final(). Behavior with invalid context is undefined.
696
697 If Sha512Context is NULL, then return FALSE.
698
699 @param[in, out] Sha512Context Pointer to the SHA-512 context.
700 @param[in] Data Pointer to the buffer containing the data to be hashed.
701 @param[in] DataSize Size of Data buffer in bytes.
702
703 @retval TRUE SHA-512 data digest succeeded.
704 @retval FALSE SHA-512 data digest failed.
705
706 **/
707 BOOLEAN
708 EFIAPI
709 Sha512Update (
710 IN OUT VOID *Sha512Context,
711 IN CONST VOID *Data,
712 IN UINTN DataSize
713 );
714
715 /**
716 Completes computation of the SHA-512 digest value.
717
718 This function completes SHA-512 hash computation and retrieves the digest value into
719 the specified memory. After this function has been called, the SHA-512 context cannot
720 be used again.
721 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be
722 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
723
724 If Sha512Context is NULL, then return FALSE.
725 If HashValue is NULL, then return FALSE.
726
727 @param[in, out] Sha512Context Pointer to the SHA-512 context.
728 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
729 value (64 bytes).
730
731 @retval TRUE SHA-512 digest computation succeeded.
732 @retval FALSE SHA-512 digest computation failed.
733
734 **/
735 BOOLEAN
736 EFIAPI
737 Sha512Final (
738 IN OUT VOID *Sha512Context,
739 OUT UINT8 *HashValue
740 );
741
742 /**
743 Computes the SHA-512 message digest of a input data buffer.
744
745 This function performs the SHA-512 message digest of a given data buffer, and places
746 the digest value into the specified memory.
747
748 If this interface is not supported, then return FALSE.
749
750 @param[in] Data Pointer to the buffer containing the data to be hashed.
751 @param[in] DataSize Size of Data buffer in bytes.
752 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
753 value (64 bytes).
754
755 @retval TRUE SHA-512 digest computation succeeded.
756 @retval FALSE SHA-512 digest computation failed.
757 @retval FALSE This interface is not supported.
758
759 **/
760 BOOLEAN
761 EFIAPI
762 Sha512HashAll (
763 IN CONST VOID *Data,
764 IN UINTN DataSize,
765 OUT UINT8 *HashValue
766 );
767
768 /**
769 Parallel hash function ParallelHash256, as defined in NIST's Special Publication 800-185,
770 published December 2016.
771
772 @param[in] Input Pointer to the input message (X).
773 @param[in] InputByteLen The number(>0) of input bytes provided for the input data.
774 @param[in] BlockSize The size of each block (B).
775 @param[out] Output Pointer to the output buffer.
776 @param[in] OutputByteLen The desired number of output bytes (L).
777 @param[in] Customization Pointer to the customization string (S).
778 @param[in] CustomByteLen The length of the customization string in bytes.
779
780 @retval TRUE ParallelHash256 digest computation succeeded.
781 @retval FALSE ParallelHash256 digest computation failed.
782 @retval FALSE This interface is not supported.
783
784 **/
785 BOOLEAN
786 EFIAPI
787 ParallelHash256HashAll (
788 IN CONST VOID *Input,
789 IN UINTN InputByteLen,
790 IN UINTN BlockSize,
791 OUT VOID *Output,
792 IN UINTN OutputByteLen,
793 IN CONST VOID *Customization,
794 IN UINTN CustomByteLen
795 );
796
797 /**
798 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
799
800 @return The size, in bytes, of the context buffer required for SM3 hash operations.
801
802 **/
803 UINTN
804 EFIAPI
805 Sm3GetContextSize (
806 VOID
807 );
808
809 /**
810 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
811 subsequent use.
812
813 If Sm3Context is NULL, then return FALSE.
814
815 @param[out] Sm3Context Pointer to SM3 context being initialized.
816
817 @retval TRUE SM3 context initialization succeeded.
818 @retval FALSE SM3 context initialization failed.
819
820 **/
821 BOOLEAN
822 EFIAPI
823 Sm3Init (
824 OUT VOID *Sm3Context
825 );
826
827 /**
828 Makes a copy of an existing SM3 context.
829
830 If Sm3Context is NULL, then return FALSE.
831 If NewSm3Context is NULL, then return FALSE.
832 If this interface is not supported, then return FALSE.
833
834 @param[in] Sm3Context Pointer to SM3 context being copied.
835 @param[out] NewSm3Context Pointer to new SM3 context.
836
837 @retval TRUE SM3 context copy succeeded.
838 @retval FALSE SM3 context copy failed.
839 @retval FALSE This interface is not supported.
840
841 **/
842 BOOLEAN
843 EFIAPI
844 Sm3Duplicate (
845 IN CONST VOID *Sm3Context,
846 OUT VOID *NewSm3Context
847 );
848
849 /**
850 Digests the input data and updates SM3 context.
851
852 This function performs SM3 digest on a data buffer of the specified size.
853 It can be called multiple times to compute the digest of long or discontinuous data streams.
854 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
855 by Sm3Final(). Behavior with invalid context is undefined.
856
857 If Sm3Context is NULL, then return FALSE.
858
859 @param[in, out] Sm3Context Pointer to the SM3 context.
860 @param[in] Data Pointer to the buffer containing the data to be hashed.
861 @param[in] DataSize Size of Data buffer in bytes.
862
863 @retval TRUE SM3 data digest succeeded.
864 @retval FALSE SM3 data digest failed.
865
866 **/
867 BOOLEAN
868 EFIAPI
869 Sm3Update (
870 IN OUT VOID *Sm3Context,
871 IN CONST VOID *Data,
872 IN UINTN DataSize
873 );
874
875 /**
876 Completes computation of the SM3 digest value.
877
878 This function completes SM3 hash computation and retrieves the digest value into
879 the specified memory. After this function has been called, the SM3 context cannot
880 be used again.
881 SM3 context should be already correctly initialized by Sm3Init(), and should not be
882 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
883
884 If Sm3Context is NULL, then return FALSE.
885 If HashValue is NULL, then return FALSE.
886
887 @param[in, out] Sm3Context Pointer to the SM3 context.
888 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
889 value (32 bytes).
890
891 @retval TRUE SM3 digest computation succeeded.
892 @retval FALSE SM3 digest computation failed.
893
894 **/
895 BOOLEAN
896 EFIAPI
897 Sm3Final (
898 IN OUT VOID *Sm3Context,
899 OUT UINT8 *HashValue
900 );
901
902 /**
903 Computes the SM3 message digest of a input data buffer.
904
905 This function performs the SM3 message digest of a given data buffer, and places
906 the digest value into the specified memory.
907
908 If this interface is not supported, then return FALSE.
909
910 @param[in] Data Pointer to the buffer containing the data to be hashed.
911 @param[in] DataSize Size of Data buffer in bytes.
912 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
913 value (32 bytes).
914
915 @retval TRUE SM3 digest computation succeeded.
916 @retval FALSE SM3 digest computation failed.
917 @retval FALSE This interface is not supported.
918
919 **/
920 BOOLEAN
921 EFIAPI
922 Sm3HashAll (
923 IN CONST VOID *Data,
924 IN UINTN DataSize,
925 OUT UINT8 *HashValue
926 );
927
928 // =====================================================================================
929 // MAC (Message Authentication Code) Primitive
930 // =====================================================================================
931
932 /**
933 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
934
935 @return Pointer to the HMAC_CTX context that has been initialized.
936 If the allocations fails, HmacSha256New() returns NULL.
937
938 **/
939 VOID *
940 EFIAPI
941 HmacSha256New (
942 VOID
943 );
944
945 /**
946 Release the specified HMAC_CTX context.
947
948 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
949
950 **/
951 VOID
952 EFIAPI
953 HmacSha256Free (
954 IN VOID *HmacSha256Ctx
955 );
956
957 /**
958 Set user-supplied key for subsequent use. It must be done before any
959 calling to HmacSha256Update().
960
961 If HmacSha256Context is NULL, then return FALSE.
962 If this interface is not supported, then return FALSE.
963
964 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
965 @param[in] Key Pointer to the user-supplied key.
966 @param[in] KeySize Key size in bytes.
967
968 @retval TRUE The Key is set successfully.
969 @retval FALSE The Key is set unsuccessfully.
970 @retval FALSE This interface is not supported.
971
972 **/
973 BOOLEAN
974 EFIAPI
975 HmacSha256SetKey (
976 OUT VOID *HmacSha256Context,
977 IN CONST UINT8 *Key,
978 IN UINTN KeySize
979 );
980
981 /**
982 Makes a copy of an existing HMAC-SHA256 context.
983
984 If HmacSha256Context is NULL, then return FALSE.
985 If NewHmacSha256Context is NULL, then return FALSE.
986 If this interface is not supported, then return FALSE.
987
988 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
989 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
990
991 @retval TRUE HMAC-SHA256 context copy succeeded.
992 @retval FALSE HMAC-SHA256 context copy failed.
993 @retval FALSE This interface is not supported.
994
995 **/
996 BOOLEAN
997 EFIAPI
998 HmacSha256Duplicate (
999 IN CONST VOID *HmacSha256Context,
1000 OUT VOID *NewHmacSha256Context
1001 );
1002
1003 /**
1004 Digests the input data and updates HMAC-SHA256 context.
1005
1006 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
1007 It can be called multiple times to compute the digest of long or discontinuous data streams.
1008 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
1009 by HmacSha256Final(). Behavior with invalid context is undefined.
1010
1011 If HmacSha256Context is NULL, then return FALSE.
1012 If this interface is not supported, then return FALSE.
1013
1014 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1015 @param[in] Data Pointer to the buffer containing the data to be digested.
1016 @param[in] DataSize Size of Data buffer in bytes.
1017
1018 @retval TRUE HMAC-SHA256 data digest succeeded.
1019 @retval FALSE HMAC-SHA256 data digest failed.
1020 @retval FALSE This interface is not supported.
1021
1022 **/
1023 BOOLEAN
1024 EFIAPI
1025 HmacSha256Update (
1026 IN OUT VOID *HmacSha256Context,
1027 IN CONST VOID *Data,
1028 IN UINTN DataSize
1029 );
1030
1031 /**
1032 Completes computation of the HMAC-SHA256 digest value.
1033
1034 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
1035 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
1036 be used again.
1037 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
1038 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
1039
1040 If HmacSha256Context is NULL, then return FALSE.
1041 If HmacValue is NULL, then return FALSE.
1042 If this interface is not supported, then return FALSE.
1043
1044 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1045 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
1046 value (32 bytes).
1047
1048 @retval TRUE HMAC-SHA256 digest computation succeeded.
1049 @retval FALSE HMAC-SHA256 digest computation failed.
1050 @retval FALSE This interface is not supported.
1051
1052 **/
1053 BOOLEAN
1054 EFIAPI
1055 HmacSha256Final (
1056 IN OUT VOID *HmacSha256Context,
1057 OUT UINT8 *HmacValue
1058 );
1059
1060 /**
1061 Computes the HMAC-SHA256 digest of a input data buffer.
1062
1063 This function performs the HMAC-SHA256 digest of a given data buffer, and places
1064 the digest value into the specified memory.
1065
1066 If this interface is not supported, then return FALSE.
1067
1068 @param[in] Data Pointer to the buffer containing the data to be digested.
1069 @param[in] DataSize Size of Data buffer in bytes.
1070 @param[in] Key Pointer to the user-supplied key.
1071 @param[in] KeySize Key size in bytes.
1072 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
1073 value (32 bytes).
1074
1075 @retval TRUE HMAC-SHA256 digest computation succeeded.
1076 @retval FALSE HMAC-SHA256 digest computation failed.
1077 @retval FALSE This interface is not supported.
1078
1079 **/
1080 BOOLEAN
1081 EFIAPI
1082 HmacSha256All (
1083 IN CONST VOID *Data,
1084 IN UINTN DataSize,
1085 IN CONST UINT8 *Key,
1086 IN UINTN KeySize,
1087 OUT UINT8 *HmacValue
1088 );
1089
1090 /**
1091 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA384 use.
1092
1093 @return Pointer to the HMAC_CTX context that has been initialized.
1094 If the allocations fails, HmacSha384New() returns NULL.
1095
1096 **/
1097 VOID *
1098 EFIAPI
1099 HmacSha384New (
1100 VOID
1101 );
1102
1103 /**
1104 Release the specified HMAC_CTX context.
1105
1106 @param[in] HmacSha384Ctx Pointer to the HMAC_CTX context to be released.
1107
1108 **/
1109 VOID
1110 EFIAPI
1111 HmacSha384Free (
1112 IN VOID *HmacSha384Ctx
1113 );
1114
1115 /**
1116 Set user-supplied key for subsequent use. It must be done before any
1117 calling to HmacSha384Update().
1118
1119 If HmacSha384Context is NULL, then return FALSE.
1120 If this interface is not supported, then return FALSE.
1121
1122 @param[out] HmacSha384Context Pointer to HMAC-SHA384 context.
1123 @param[in] Key Pointer to the user-supplied key.
1124 @param[in] KeySize Key size in bytes.
1125
1126 @retval TRUE The Key is set successfully.
1127 @retval FALSE The Key is set unsuccessfully.
1128 @retval FALSE This interface is not supported.
1129
1130 **/
1131 BOOLEAN
1132 EFIAPI
1133 HmacSha384SetKey (
1134 OUT VOID *HmacSha384Context,
1135 IN CONST UINT8 *Key,
1136 IN UINTN KeySize
1137 );
1138
1139 /**
1140 Makes a copy of an existing HMAC-SHA384 context.
1141
1142 If HmacSha384Context is NULL, then return FALSE.
1143 If NewHmacSha384Context is NULL, then return FALSE.
1144 If this interface is not supported, then return FALSE.
1145
1146 @param[in] HmacSha384Context Pointer to HMAC-SHA384 context being copied.
1147 @param[out] NewHmacSha384Context Pointer to new HMAC-SHA384 context.
1148
1149 @retval TRUE HMAC-SHA384 context copy succeeded.
1150 @retval FALSE HMAC-SHA384 context copy failed.
1151 @retval FALSE This interface is not supported.
1152
1153 **/
1154 BOOLEAN
1155 EFIAPI
1156 HmacSha384Duplicate (
1157 IN CONST VOID *HmacSha384Context,
1158 OUT VOID *NewHmacSha384Context
1159 );
1160
1161 /**
1162 Digests the input data and updates HMAC-SHA384 context.
1163
1164 This function performs HMAC-SHA384 digest on a data buffer of the specified size.
1165 It can be called multiple times to compute the digest of long or discontinuous data streams.
1166 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1167 by HmacSha384Final(). Behavior with invalid context is undefined.
1168
1169 If HmacSha384Context is NULL, then return FALSE.
1170 If this interface is not supported, then return FALSE.
1171
1172 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1173 @param[in] Data Pointer to the buffer containing the data to be digested.
1174 @param[in] DataSize Size of Data buffer in bytes.
1175
1176 @retval TRUE HMAC-SHA384 data digest succeeded.
1177 @retval FALSE HMAC-SHA384 data digest failed.
1178 @retval FALSE This interface is not supported.
1179
1180 **/
1181 BOOLEAN
1182 EFIAPI
1183 HmacSha384Update (
1184 IN OUT VOID *HmacSha384Context,
1185 IN CONST VOID *Data,
1186 IN UINTN DataSize
1187 );
1188
1189 /**
1190 Completes computation of the HMAC-SHA384 digest value.
1191
1192 This function completes HMAC-SHA384 hash computation and retrieves the digest value into
1193 the specified memory. After this function has been called, the HMAC-SHA384 context cannot
1194 be used again.
1195 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1196 by HmacSha384Final(). Behavior with invalid HMAC-SHA384 context is undefined.
1197
1198 If HmacSha384Context is NULL, then return FALSE.
1199 If HmacValue is NULL, then return FALSE.
1200 If this interface is not supported, then return FALSE.
1201
1202 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1203 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
1204 value (48 bytes).
1205
1206 @retval TRUE HMAC-SHA384 digest computation succeeded.
1207 @retval FALSE HMAC-SHA384 digest computation failed.
1208 @retval FALSE This interface is not supported.
1209
1210 **/
1211 BOOLEAN
1212 EFIAPI
1213 HmacSha384Final (
1214 IN OUT VOID *HmacSha384Context,
1215 OUT UINT8 *HmacValue
1216 );
1217
1218 /**
1219 Computes the HMAC-SHA384 digest of a input data buffer.
1220
1221 This function performs the HMAC-SHA384 digest of a given data buffer, and places
1222 the digest value into the specified memory.
1223
1224 If this interface is not supported, then return FALSE.
1225
1226 @param[in] Data Pointer to the buffer containing the data to be digested.
1227 @param[in] DataSize Size of Data buffer in bytes.
1228 @param[in] Key Pointer to the user-supplied key.
1229 @param[in] KeySize Key size in bytes.
1230 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA384 digest
1231 value (48 bytes).
1232
1233 @retval TRUE HMAC-SHA384 digest computation succeeded.
1234 @retval FALSE HMAC-SHA384 digest computation failed.
1235 @retval FALSE This interface is not supported.
1236
1237 **/
1238 BOOLEAN
1239 EFIAPI
1240 HmacSha384All (
1241 IN CONST VOID *Data,
1242 IN UINTN DataSize,
1243 IN CONST UINT8 *Key,
1244 IN UINTN KeySize,
1245 OUT UINT8 *HmacValue
1246 );
1247
1248 // =====================================================================================
1249 // Symmetric Cryptography Primitive
1250 // =====================================================================================
1251
1252 /**
1253 Retrieves the size, in bytes, of the context buffer required for AES operations.
1254
1255 If this interface is not supported, then return zero.
1256
1257 @return The size, in bytes, of the context buffer required for AES operations.
1258 @retval 0 This interface is not supported.
1259
1260 **/
1261 UINTN
1262 EFIAPI
1263 AesGetContextSize (
1264 VOID
1265 );
1266
1267 /**
1268 Initializes user-supplied memory as AES context for subsequent use.
1269
1270 This function initializes user-supplied memory pointed by AesContext as AES context.
1271 In addition, it sets up all AES key materials for subsequent encryption and decryption
1272 operations.
1273 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1274
1275 If AesContext is NULL, then return FALSE.
1276 If Key is NULL, then return FALSE.
1277 If KeyLength is not valid, then return FALSE.
1278 If this interface is not supported, then return FALSE.
1279
1280 @param[out] AesContext Pointer to AES context being initialized.
1281 @param[in] Key Pointer to the user-supplied AES key.
1282 @param[in] KeyLength Length of AES key in bits.
1283
1284 @retval TRUE AES context initialization succeeded.
1285 @retval FALSE AES context initialization failed.
1286 @retval FALSE This interface is not supported.
1287
1288 **/
1289 BOOLEAN
1290 EFIAPI
1291 AesInit (
1292 OUT VOID *AesContext,
1293 IN CONST UINT8 *Key,
1294 IN UINTN KeyLength
1295 );
1296
1297 /**
1298 Performs AES encryption on a data buffer of the specified size in CBC mode.
1299
1300 This function performs AES encryption on data buffer pointed by Input, of specified
1301 size of InputSize, in CBC mode.
1302 InputSize must be multiple of block size (16 bytes). This function does not perform
1303 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1304 Initialization vector should be one block size (16 bytes).
1305 AesContext should be already correctly initialized by AesInit(). Behavior with
1306 invalid AES context is undefined.
1307
1308 If AesContext is NULL, then return FALSE.
1309 If Input is NULL, then return FALSE.
1310 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1311 If Ivec is NULL, then return FALSE.
1312 If Output is NULL, then return FALSE.
1313 If this interface is not supported, then return FALSE.
1314
1315 @param[in] AesContext Pointer to the AES context.
1316 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1317 @param[in] InputSize Size of the Input buffer in bytes.
1318 @param[in] Ivec Pointer to initialization vector.
1319 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1320
1321 @retval TRUE AES encryption succeeded.
1322 @retval FALSE AES encryption failed.
1323 @retval FALSE This interface is not supported.
1324
1325 **/
1326 BOOLEAN
1327 EFIAPI
1328 AesCbcEncrypt (
1329 IN VOID *AesContext,
1330 IN CONST UINT8 *Input,
1331 IN UINTN InputSize,
1332 IN CONST UINT8 *Ivec,
1333 OUT UINT8 *Output
1334 );
1335
1336 /**
1337 Performs AES decryption on a data buffer of the specified size in CBC mode.
1338
1339 This function performs AES decryption on data buffer pointed by Input, of specified
1340 size of InputSize, in CBC mode.
1341 InputSize must be multiple of block size (16 bytes). This function does not perform
1342 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1343 Initialization vector should be one block size (16 bytes).
1344 AesContext should be already correctly initialized by AesInit(). Behavior with
1345 invalid AES context is undefined.
1346
1347 If AesContext is NULL, then return FALSE.
1348 If Input is NULL, then return FALSE.
1349 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1350 If Ivec is NULL, then return FALSE.
1351 If Output is NULL, then return FALSE.
1352 If this interface is not supported, then return FALSE.
1353
1354 @param[in] AesContext Pointer to the AES context.
1355 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1356 @param[in] InputSize Size of the Input buffer in bytes.
1357 @param[in] Ivec Pointer to initialization vector.
1358 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1359
1360 @retval TRUE AES decryption succeeded.
1361 @retval FALSE AES decryption failed.
1362 @retval FALSE This interface is not supported.
1363
1364 **/
1365 BOOLEAN
1366 EFIAPI
1367 AesCbcDecrypt (
1368 IN VOID *AesContext,
1369 IN CONST UINT8 *Input,
1370 IN UINTN InputSize,
1371 IN CONST UINT8 *Ivec,
1372 OUT UINT8 *Output
1373 );
1374
1375 // =====================================================================================
1376 // Authenticated Encryption with Associated Data (AEAD) Cryptography Primitive
1377 // =====================================================================================
1378
1379 /**
1380 Performs AEAD AES-GCM authenticated encryption on a data buffer and additional authenticated data (AAD).
1381
1382 IvSize must be 12, otherwise FALSE is returned.
1383 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1384 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1385
1386 @param[in] Key Pointer to the encryption key.
1387 @param[in] KeySize Size of the encryption key in bytes.
1388 @param[in] Iv Pointer to the IV value.
1389 @param[in] IvSize Size of the IV value in bytes.
1390 @param[in] AData Pointer to the additional authenticated data (AAD).
1391 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1392 @param[in] DataIn Pointer to the input data buffer to be encrypted.
1393 @param[in] DataInSize Size of the input data buffer in bytes.
1394 @param[out] TagOut Pointer to a buffer that receives the authentication tag output.
1395 @param[in] TagSize Size of the authentication tag in bytes.
1396 @param[out] DataOut Pointer to a buffer that receives the encryption output.
1397 @param[out] DataOutSize Size of the output data buffer in bytes.
1398
1399 @retval TRUE AEAD AES-GCM authenticated encryption succeeded.
1400 @retval FALSE AEAD AES-GCM authenticated encryption failed.
1401
1402 **/
1403 BOOLEAN
1404 EFIAPI
1405 AeadAesGcmEncrypt (
1406 IN CONST UINT8 *Key,
1407 IN UINTN KeySize,
1408 IN CONST UINT8 *Iv,
1409 IN UINTN IvSize,
1410 IN CONST UINT8 *AData,
1411 IN UINTN ADataSize,
1412 IN CONST UINT8 *DataIn,
1413 IN UINTN DataInSize,
1414 OUT UINT8 *TagOut,
1415 IN UINTN TagSize,
1416 OUT UINT8 *DataOut,
1417 OUT UINTN *DataOutSize
1418 );
1419
1420 /**
1421 Performs AEAD AES-GCM authenticated decryption on a data buffer and additional authenticated data (AAD).
1422
1423 IvSize must be 12, otherwise FALSE is returned.
1424 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1425 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1426 If additional authenticated data verification fails, FALSE is returned.
1427
1428 @param[in] Key Pointer to the encryption key.
1429 @param[in] KeySize Size of the encryption key in bytes.
1430 @param[in] Iv Pointer to the IV value.
1431 @param[in] IvSize Size of the IV value in bytes.
1432 @param[in] AData Pointer to the additional authenticated data (AAD).
1433 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1434 @param[in] DataIn Pointer to the input data buffer to be decrypted.
1435 @param[in] DataInSize Size of the input data buffer in bytes.
1436 @param[in] Tag Pointer to a buffer that contains the authentication tag.
1437 @param[in] TagSize Size of the authentication tag in bytes.
1438 @param[out] DataOut Pointer to a buffer that receives the decryption output.
1439 @param[out] DataOutSize Size of the output data buffer in bytes.
1440
1441 @retval TRUE AEAD AES-GCM authenticated decryption succeeded.
1442 @retval FALSE AEAD AES-GCM authenticated decryption failed.
1443
1444 **/
1445 BOOLEAN
1446 EFIAPI
1447 AeadAesGcmDecrypt (
1448 IN CONST UINT8 *Key,
1449 IN UINTN KeySize,
1450 IN CONST UINT8 *Iv,
1451 IN UINTN IvSize,
1452 IN CONST UINT8 *AData,
1453 IN UINTN ADataSize,
1454 IN CONST UINT8 *DataIn,
1455 IN UINTN DataInSize,
1456 IN CONST UINT8 *Tag,
1457 IN UINTN TagSize,
1458 OUT UINT8 *DataOut,
1459 OUT UINTN *DataOutSize
1460 );
1461
1462 // =====================================================================================
1463 // Asymmetric Cryptography Primitive
1464 // =====================================================================================
1465
1466 /**
1467 Allocates and initializes one RSA context for subsequent use.
1468
1469 @return Pointer to the RSA context that has been initialized.
1470 If the allocations fails, RsaNew() returns NULL.
1471
1472 **/
1473 VOID *
1474 EFIAPI
1475 RsaNew (
1476 VOID
1477 );
1478
1479 /**
1480 Release the specified RSA context.
1481
1482 If RsaContext is NULL, then return FALSE.
1483
1484 @param[in] RsaContext Pointer to the RSA context to be released.
1485
1486 **/
1487 VOID
1488 EFIAPI
1489 RsaFree (
1490 IN VOID *RsaContext
1491 );
1492
1493 /**
1494 Sets the tag-designated key component into the established RSA context.
1495
1496 This function sets the tag-designated RSA key component into the established
1497 RSA context from the user-specified non-negative integer (octet string format
1498 represented in RSA PKCS#1).
1499 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1500
1501 If RsaContext is NULL, then return FALSE.
1502
1503 @param[in, out] RsaContext Pointer to RSA context being set.
1504 @param[in] KeyTag Tag of RSA key component being set.
1505 @param[in] BigNumber Pointer to octet integer buffer.
1506 If NULL, then the specified key component in RSA
1507 context is cleared.
1508 @param[in] BnSize Size of big number buffer in bytes.
1509 If BigNumber is NULL, then it is ignored.
1510
1511 @retval TRUE RSA key component was set successfully.
1512 @retval FALSE Invalid RSA key component tag.
1513
1514 **/
1515 BOOLEAN
1516 EFIAPI
1517 RsaSetKey (
1518 IN OUT VOID *RsaContext,
1519 IN RSA_KEY_TAG KeyTag,
1520 IN CONST UINT8 *BigNumber,
1521 IN UINTN BnSize
1522 );
1523
1524 /**
1525 Gets the tag-designated RSA key component from the established RSA context.
1526
1527 This function retrieves the tag-designated RSA key component from the
1528 established RSA context as a non-negative integer (octet string format
1529 represented in RSA PKCS#1).
1530 If specified key component has not been set or has been cleared, then returned
1531 BnSize is set to 0.
1532 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1533 is returned and BnSize is set to the required buffer size to obtain the key.
1534
1535 If RsaContext is NULL, then return FALSE.
1536 If BnSize is NULL, then return FALSE.
1537 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1538 If this interface is not supported, then return FALSE.
1539
1540 @param[in, out] RsaContext Pointer to RSA context being set.
1541 @param[in] KeyTag Tag of RSA key component being set.
1542 @param[out] BigNumber Pointer to octet integer buffer.
1543 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1544 On output, the size of data returned in big number buffer in bytes.
1545
1546 @retval TRUE RSA key component was retrieved successfully.
1547 @retval FALSE Invalid RSA key component tag.
1548 @retval FALSE BnSize is too small.
1549 @retval FALSE This interface is not supported.
1550
1551 **/
1552 BOOLEAN
1553 EFIAPI
1554 RsaGetKey (
1555 IN OUT VOID *RsaContext,
1556 IN RSA_KEY_TAG KeyTag,
1557 OUT UINT8 *BigNumber,
1558 IN OUT UINTN *BnSize
1559 );
1560
1561 /**
1562 Generates RSA key components.
1563
1564 This function generates RSA key components. It takes RSA public exponent E and
1565 length in bits of RSA modulus N as input, and generates all key components.
1566 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1567
1568 Before this function can be invoked, pseudorandom number generator must be correctly
1569 initialized by RandomSeed().
1570
1571 If RsaContext is NULL, then return FALSE.
1572 If this interface is not supported, then return FALSE.
1573
1574 @param[in, out] RsaContext Pointer to RSA context being set.
1575 @param[in] ModulusLength Length of RSA modulus N in bits.
1576 @param[in] PublicExponent Pointer to RSA public exponent.
1577 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1578
1579 @retval TRUE RSA key component was generated successfully.
1580 @retval FALSE Invalid RSA key component tag.
1581 @retval FALSE This interface is not supported.
1582
1583 **/
1584 BOOLEAN
1585 EFIAPI
1586 RsaGenerateKey (
1587 IN OUT VOID *RsaContext,
1588 IN UINTN ModulusLength,
1589 IN CONST UINT8 *PublicExponent,
1590 IN UINTN PublicExponentSize
1591 );
1592
1593 /**
1594 Validates key components of RSA context.
1595 NOTE: This function performs integrity checks on all the RSA key material, so
1596 the RSA key structure must contain all the private key data.
1597
1598 This function validates key components of RSA context in following aspects:
1599 - Whether p is a prime
1600 - Whether q is a prime
1601 - Whether n = p * q
1602 - Whether d*e = 1 mod lcm(p-1,q-1)
1603
1604 If RsaContext is NULL, then return FALSE.
1605 If this interface is not supported, then return FALSE.
1606
1607 @param[in] RsaContext Pointer to RSA context to check.
1608
1609 @retval TRUE RSA key components are valid.
1610 @retval FALSE RSA key components are not valid.
1611 @retval FALSE This interface is not supported.
1612
1613 **/
1614 BOOLEAN
1615 EFIAPI
1616 RsaCheckKey (
1617 IN VOID *RsaContext
1618 );
1619
1620 /**
1621 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1622
1623 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1624 RSA PKCS#1.
1625 If the Signature buffer is too small to hold the contents of signature, FALSE
1626 is returned and SigSize is set to the required buffer size to obtain the signature.
1627
1628 If RsaContext is NULL, then return FALSE.
1629 If MessageHash is NULL, then return FALSE.
1630 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1631 If SigSize is large enough but Signature is NULL, then return FALSE.
1632 If this interface is not supported, then return FALSE.
1633
1634 @param[in] RsaContext Pointer to RSA context for signature generation.
1635 @param[in] MessageHash Pointer to octet message hash to be signed.
1636 @param[in] HashSize Size of the message hash in bytes.
1637 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1638 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1639 On output, the size of data returned in Signature buffer in bytes.
1640
1641 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1642 @retval FALSE Signature generation failed.
1643 @retval FALSE SigSize is too small.
1644 @retval FALSE This interface is not supported.
1645
1646 **/
1647 BOOLEAN
1648 EFIAPI
1649 RsaPkcs1Sign (
1650 IN VOID *RsaContext,
1651 IN CONST UINT8 *MessageHash,
1652 IN UINTN HashSize,
1653 OUT UINT8 *Signature,
1654 IN OUT UINTN *SigSize
1655 );
1656
1657 /**
1658 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1659 RSA PKCS#1.
1660
1661 If RsaContext is NULL, then return FALSE.
1662 If MessageHash is NULL, then return FALSE.
1663 If Signature is NULL, then return FALSE.
1664 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1665
1666 @param[in] RsaContext Pointer to RSA context for signature verification.
1667 @param[in] MessageHash Pointer to octet message hash to be checked.
1668 @param[in] HashSize Size of the message hash in bytes.
1669 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1670 @param[in] SigSize Size of signature in bytes.
1671
1672 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1673 @retval FALSE Invalid signature or invalid RSA context.
1674
1675 **/
1676 BOOLEAN
1677 EFIAPI
1678 RsaPkcs1Verify (
1679 IN VOID *RsaContext,
1680 IN CONST UINT8 *MessageHash,
1681 IN UINTN HashSize,
1682 IN CONST UINT8 *Signature,
1683 IN UINTN SigSize
1684 );
1685
1686 /**
1687 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1688
1689 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1690 RFC 8017.
1691 Mask generation function is the same as the message digest algorithm.
1692 If the Signature buffer is too small to hold the contents of signature, FALSE
1693 is returned and SigSize is set to the required buffer size to obtain the signature.
1694
1695 If RsaContext is NULL, then return FALSE.
1696 If Message is NULL, then return FALSE.
1697 If MsgSize is zero or > INT_MAX, then return FALSE.
1698 If DigestLen is NOT 32, 48 or 64, return FALSE.
1699 If SaltLen is not equal to DigestLen, then return FALSE.
1700 If SigSize is large enough but Signature is NULL, then return FALSE.
1701 If this interface is not supported, then return FALSE.
1702
1703 @param[in] RsaContext Pointer to RSA context for signature generation.
1704 @param[in] Message Pointer to octet message to be signed.
1705 @param[in] MsgSize Size of the message in bytes.
1706 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1707 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1708 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1709 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1710 On output, the size of data returned in Signature buffer in bytes.
1711
1712 @retval TRUE Signature successfully generated in RSASSA-PSS.
1713 @retval FALSE Signature generation failed.
1714 @retval FALSE SigSize is too small.
1715 @retval FALSE This interface is not supported.
1716
1717 **/
1718 BOOLEAN
1719 EFIAPI
1720 RsaPssSign (
1721 IN VOID *RsaContext,
1722 IN CONST UINT8 *Message,
1723 IN UINTN MsgSize,
1724 IN UINT16 DigestLen,
1725 IN UINT16 SaltLen,
1726 OUT UINT8 *Signature,
1727 IN OUT UINTN *SigSize
1728 );
1729
1730 /**
1731 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1732 Implementation determines salt length automatically from the signature encoding.
1733 Mask generation function is the same as the message digest algorithm.
1734 Salt length should be equal to digest length.
1735
1736 @param[in] RsaContext Pointer to RSA context for signature verification.
1737 @param[in] Message Pointer to octet message to be verified.
1738 @param[in] MsgSize Size of the message in bytes.
1739 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1740 @param[in] SigSize Size of signature in bytes.
1741 @param[in] DigestLen Length of digest for RSA operation.
1742 @param[in] SaltLen Salt length for PSS encoding.
1743
1744 @retval TRUE Valid signature encoded in RSASSA-PSS.
1745 @retval FALSE Invalid signature or invalid RSA context.
1746
1747 **/
1748 BOOLEAN
1749 EFIAPI
1750 RsaPssVerify (
1751 IN VOID *RsaContext,
1752 IN CONST UINT8 *Message,
1753 IN UINTN MsgSize,
1754 IN CONST UINT8 *Signature,
1755 IN UINTN SigSize,
1756 IN UINT16 DigestLen,
1757 IN UINT16 SaltLen
1758 );
1759
1760 /**
1761 Retrieve the RSA Private Key from the password-protected PEM key data.
1762
1763 If PemData is NULL, then return FALSE.
1764 If RsaContext is NULL, then return FALSE.
1765 If this interface is not supported, then return FALSE.
1766
1767 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1768 @param[in] PemSize Size of the PEM key data in bytes.
1769 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1770 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1771 RSA private key component. Use RsaFree() function to free the
1772 resource.
1773
1774 @retval TRUE RSA Private Key was retrieved successfully.
1775 @retval FALSE Invalid PEM key data or incorrect password.
1776 @retval FALSE This interface is not supported.
1777
1778 **/
1779 BOOLEAN
1780 EFIAPI
1781 RsaGetPrivateKeyFromPem (
1782 IN CONST UINT8 *PemData,
1783 IN UINTN PemSize,
1784 IN CONST CHAR8 *Password,
1785 OUT VOID **RsaContext
1786 );
1787
1788 /**
1789 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1790
1791 If Cert is NULL, then return FALSE.
1792 If RsaContext is NULL, then return FALSE.
1793 If this interface is not supported, then return FALSE.
1794
1795 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1796 @param[in] CertSize Size of the X509 certificate in bytes.
1797 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1798 RSA public key component. Use RsaFree() function to free the
1799 resource.
1800
1801 @retval TRUE RSA Public Key was retrieved successfully.
1802 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1803 @retval FALSE This interface is not supported.
1804
1805 **/
1806 BOOLEAN
1807 EFIAPI
1808 RsaGetPublicKeyFromX509 (
1809 IN CONST UINT8 *Cert,
1810 IN UINTN CertSize,
1811 OUT VOID **RsaContext
1812 );
1813
1814 /**
1815 Retrieve the subject bytes from one X.509 certificate.
1816
1817 If Cert is NULL, then return FALSE.
1818 If SubjectSize is NULL, then return FALSE.
1819 If this interface is not supported, then return FALSE.
1820
1821 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1822 @param[in] CertSize Size of the X509 certificate in bytes.
1823 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1824 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1825 and the size of buffer returned CertSubject on output.
1826
1827 @retval TRUE The certificate subject retrieved successfully.
1828 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1829 The SubjectSize will be updated with the required size.
1830 @retval FALSE This interface is not supported.
1831
1832 **/
1833 BOOLEAN
1834 EFIAPI
1835 X509GetSubjectName (
1836 IN CONST UINT8 *Cert,
1837 IN UINTN CertSize,
1838 OUT UINT8 *CertSubject,
1839 IN OUT UINTN *SubjectSize
1840 );
1841
1842 /**
1843 Retrieve the common name (CN) string from one X.509 certificate.
1844
1845 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1846 @param[in] CertSize Size of the X509 certificate in bytes.
1847 @param[out] CommonName Buffer to contain the retrieved certificate common
1848 name string (UTF8). At most CommonNameSize bytes will be
1849 written and the string will be null terminated. May be
1850 NULL in order to determine the size buffer needed.
1851 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1852 and the size of buffer returned CommonName on output.
1853 If CommonName is NULL then the amount of space needed
1854 in buffer (including the final null) is returned.
1855
1856 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1857 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1858 If CommonNameSize is NULL.
1859 If CommonName is not NULL and *CommonNameSize is 0.
1860 If Certificate is invalid.
1861 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1862 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1863 (including the final null) is returned in the
1864 CommonNameSize parameter.
1865 @retval RETURN_UNSUPPORTED The operation is not supported.
1866
1867 **/
1868 RETURN_STATUS
1869 EFIAPI
1870 X509GetCommonName (
1871 IN CONST UINT8 *Cert,
1872 IN UINTN CertSize,
1873 OUT CHAR8 *CommonName OPTIONAL,
1874 IN OUT UINTN *CommonNameSize
1875 );
1876
1877 /**
1878 Retrieve the organization name (O) string from one X.509 certificate.
1879
1880 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1881 @param[in] CertSize Size of the X509 certificate in bytes.
1882 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1883 name string. At most NameBufferSize bytes will be
1884 written and the string will be null terminated. May be
1885 NULL in order to determine the size buffer needed.
1886 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1887 and the size of buffer returned Name on output.
1888 If NameBuffer is NULL then the amount of space needed
1889 in buffer (including the final null) is returned.
1890
1891 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1892 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1893 If NameBufferSize is NULL.
1894 If NameBuffer is not NULL and *CommonNameSize is 0.
1895 If Certificate is invalid.
1896 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1897 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1898 (including the final null) is returned in the
1899 CommonNameSize parameter.
1900 @retval RETURN_UNSUPPORTED The operation is not supported.
1901
1902 **/
1903 RETURN_STATUS
1904 EFIAPI
1905 X509GetOrganizationName (
1906 IN CONST UINT8 *Cert,
1907 IN UINTN CertSize,
1908 OUT CHAR8 *NameBuffer OPTIONAL,
1909 IN OUT UINTN *NameBufferSize
1910 );
1911
1912 /**
1913 Verify one X509 certificate was issued by the trusted CA.
1914
1915 If Cert is NULL, then return FALSE.
1916 If CACert is NULL, then return FALSE.
1917 If this interface is not supported, then return FALSE.
1918
1919 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1920 @param[in] CertSize Size of the X509 certificate in bytes.
1921 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1922 @param[in] CACertSize Size of the CA Certificate in bytes.
1923
1924 @retval TRUE The certificate was issued by the trusted CA.
1925 @retval FALSE Invalid certificate or the certificate was not issued by the given
1926 trusted CA.
1927 @retval FALSE This interface is not supported.
1928
1929 **/
1930 BOOLEAN
1931 EFIAPI
1932 X509VerifyCert (
1933 IN CONST UINT8 *Cert,
1934 IN UINTN CertSize,
1935 IN CONST UINT8 *CACert,
1936 IN UINTN CACertSize
1937 );
1938
1939 /**
1940 Construct a X509 object from DER-encoded certificate data.
1941
1942 If Cert is NULL, then return FALSE.
1943 If SingleX509Cert is NULL, then return FALSE.
1944 If this interface is not supported, then return FALSE.
1945
1946 @param[in] Cert Pointer to the DER-encoded certificate data.
1947 @param[in] CertSize The size of certificate data in bytes.
1948 @param[out] SingleX509Cert The generated X509 object.
1949
1950 @retval TRUE The X509 object generation succeeded.
1951 @retval FALSE The operation failed.
1952 @retval FALSE This interface is not supported.
1953
1954 **/
1955 BOOLEAN
1956 EFIAPI
1957 X509ConstructCertificate (
1958 IN CONST UINT8 *Cert,
1959 IN UINTN CertSize,
1960 OUT UINT8 **SingleX509Cert
1961 );
1962
1963 /**
1964 Construct a X509 stack object from a list of DER-encoded certificate data.
1965
1966 If X509Stack is NULL, then return FALSE.
1967 If this interface is not supported, then return FALSE.
1968
1969 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1970 On output, pointer to the X509 stack object with new
1971 inserted X509 certificate.
1972 @param[in] Args VA_LIST marker for the variable argument list.
1973 A list of DER-encoded single certificate data followed
1974 by certificate size. A NULL terminates the list. The
1975 pairs are the arguments to X509ConstructCertificate().
1976
1977 @retval TRUE The X509 stack construction succeeded.
1978 @retval FALSE The construction operation failed.
1979 @retval FALSE This interface is not supported.
1980
1981 **/
1982 BOOLEAN
1983 EFIAPI
1984 X509ConstructCertificateStackV (
1985 IN OUT UINT8 **X509Stack,
1986 IN VA_LIST Args
1987 );
1988
1989 /**
1990 Construct a X509 stack object from a list of DER-encoded certificate data.
1991
1992 If X509Stack is NULL, then return FALSE.
1993 If this interface is not supported, then return FALSE.
1994
1995 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1996 On output, pointer to the X509 stack object with new
1997 inserted X509 certificate.
1998 @param ... A list of DER-encoded single certificate data followed
1999 by certificate size. A NULL terminates the list. The
2000 pairs are the arguments to X509ConstructCertificate().
2001
2002 @retval TRUE The X509 stack construction succeeded.
2003 @retval FALSE The construction operation failed.
2004 @retval FALSE This interface is not supported.
2005
2006 **/
2007 BOOLEAN
2008 EFIAPI
2009 X509ConstructCertificateStack (
2010 IN OUT UINT8 **X509Stack,
2011 ...
2012 );
2013
2014 /**
2015 Release the specified X509 object.
2016
2017 If the interface is not supported, then ASSERT().
2018
2019 @param[in] X509Cert Pointer to the X509 object to be released.
2020
2021 **/
2022 VOID
2023 EFIAPI
2024 X509Free (
2025 IN VOID *X509Cert
2026 );
2027
2028 /**
2029 Release the specified X509 stack object.
2030
2031 If the interface is not supported, then ASSERT().
2032
2033 @param[in] X509Stack Pointer to the X509 stack object to be released.
2034
2035 **/
2036 VOID
2037 EFIAPI
2038 X509StackFree (
2039 IN VOID *X509Stack
2040 );
2041
2042 /**
2043 Retrieve the TBSCertificate from one given X.509 certificate.
2044
2045 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
2046 @param[in] CertSize Size of the X509 certificate in bytes.
2047 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
2048 @param[out] TBSCertSize Size of the TBS certificate in bytes.
2049
2050 If Cert is NULL, then return FALSE.
2051 If TBSCert is NULL, then return FALSE.
2052 If TBSCertSize is NULL, then return FALSE.
2053 If this interface is not supported, then return FALSE.
2054
2055 @retval TRUE The TBSCertificate was retrieved successfully.
2056 @retval FALSE Invalid X.509 certificate.
2057
2058 **/
2059 BOOLEAN
2060 EFIAPI
2061 X509GetTBSCert (
2062 IN CONST UINT8 *Cert,
2063 IN UINTN CertSize,
2064 OUT UINT8 **TBSCert,
2065 OUT UINTN *TBSCertSize
2066 );
2067
2068 /**
2069 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
2070 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
2071
2072 If Password or Salt or OutKey is NULL, then return FALSE.
2073 If the hash algorithm could not be determined, then return FALSE.
2074 If this interface is not supported, then return FALSE.
2075
2076 @param[in] PasswordLength Length of input password in bytes.
2077 @param[in] Password Pointer to the array for the password.
2078 @param[in] SaltLength Size of the Salt in bytes.
2079 @param[in] Salt Pointer to the Salt.
2080 @param[in] IterationCount Number of iterations to perform. Its value should be
2081 greater than or equal to 1.
2082 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
2083 NOTE: DigestSize will be used to determine the hash algorithm.
2084 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
2085 @param[in] KeyLength Size of the derived key buffer in bytes.
2086 @param[out] OutKey Pointer to the output derived key buffer.
2087
2088 @retval TRUE A key was derived successfully.
2089 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
2090 @retval FALSE The hash algorithm could not be determined from the digest size.
2091 @retval FALSE The key derivation operation failed.
2092 @retval FALSE This interface is not supported.
2093
2094 **/
2095 BOOLEAN
2096 EFIAPI
2097 Pkcs5HashPassword (
2098 IN UINTN PasswordLength,
2099 IN CONST CHAR8 *Password,
2100 IN UINTN SaltLength,
2101 IN CONST UINT8 *Salt,
2102 IN UINTN IterationCount,
2103 IN UINTN DigestSize,
2104 IN UINTN KeyLength,
2105 OUT UINT8 *OutKey
2106 );
2107
2108 /**
2109 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
2110 encrypted message in a newly allocated buffer.
2111
2112 Things that can cause a failure include:
2113 - X509 key size does not match any known key size.
2114 - Fail to parse X509 certificate.
2115 - Fail to allocate an intermediate buffer.
2116 - Null pointer provided for a non-optional parameter.
2117 - Data size is too large for the provided key size (max size is a function of key size
2118 and hash digest size).
2119
2120 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
2121 will be used to encrypt the data.
2122 @param[in] PublicKeySize Size of the X509 cert buffer.
2123 @param[in] InData Data to be encrypted.
2124 @param[in] InDataSize Size of the data buffer.
2125 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
2126 to be used when initializing the PRNG. NULL otherwise.
2127 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
2128 0 otherwise.
2129 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
2130 message.
2131 @param[out] EncryptedDataSize Size of the encrypted message buffer.
2132
2133 @retval TRUE Encryption was successful.
2134 @retval FALSE Encryption failed.
2135
2136 **/
2137 BOOLEAN
2138 EFIAPI
2139 Pkcs1v2Encrypt (
2140 IN CONST UINT8 *PublicKey,
2141 IN UINTN PublicKeySize,
2142 IN UINT8 *InData,
2143 IN UINTN InDataSize,
2144 IN CONST UINT8 *PrngSeed OPTIONAL,
2145 IN UINTN PrngSeedSize OPTIONAL,
2146 OUT UINT8 **EncryptedData,
2147 OUT UINTN *EncryptedDataSize
2148 );
2149
2150 /**
2151 The 3rd parameter of Pkcs7GetSigners will return all embedded
2152 X.509 certificate in one given PKCS7 signature. The format is:
2153 //
2154 // UINT8 CertNumber;
2155 // UINT32 Cert1Length;
2156 // UINT8 Cert1[];
2157 // UINT32 Cert2Length;
2158 // UINT8 Cert2[];
2159 // ...
2160 // UINT32 CertnLength;
2161 // UINT8 Certn[];
2162 //
2163
2164 The two following C-structure are used for parsing CertStack more clearly.
2165 **/
2166 #pragma pack(1)
2167
2168 typedef struct {
2169 UINT32 CertDataLength; // The length in bytes of X.509 certificate.
2170 UINT8 CertDataBuffer[0]; // The X.509 certificate content (DER).
2171 } EFI_CERT_DATA;
2172
2173 typedef struct {
2174 UINT8 CertNumber; // Number of X.509 certificate.
2175 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
2176 } EFI_CERT_STACK;
2177
2178 #pragma pack()
2179
2180 /**
2181 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
2182 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2183 in a ContentInfo structure.
2184
2185 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
2186 return FALSE. If P7Length overflow, then return FALSE.
2187 If this interface is not supported, then return FALSE.
2188
2189 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2190 @param[in] P7Length Length of the PKCS#7 message in bytes.
2191 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
2192 It's caller's responsibility to free the buffer with
2193 Pkcs7FreeSigners().
2194 This data structure is EFI_CERT_STACK type.
2195 @param[out] StackLength Length of signer's certificates in bytes.
2196 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
2197 It's caller's responsibility to free the buffer with
2198 Pkcs7FreeSigners().
2199 @param[out] CertLength Length of the trusted certificate in bytes.
2200
2201 @retval TRUE The operation is finished successfully.
2202 @retval FALSE Error occurs during the operation.
2203 @retval FALSE This interface is not supported.
2204
2205 **/
2206 BOOLEAN
2207 EFIAPI
2208 Pkcs7GetSigners (
2209 IN CONST UINT8 *P7Data,
2210 IN UINTN P7Length,
2211 OUT UINT8 **CertStack,
2212 OUT UINTN *StackLength,
2213 OUT UINT8 **TrustedCert,
2214 OUT UINTN *CertLength
2215 );
2216
2217 /**
2218 Wrap function to use free() to free allocated memory for certificates.
2219
2220 If this interface is not supported, then ASSERT().
2221
2222 @param[in] Certs Pointer to the certificates to be freed.
2223
2224 **/
2225 VOID
2226 EFIAPI
2227 Pkcs7FreeSigners (
2228 IN UINT8 *Certs
2229 );
2230
2231 /**
2232 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
2233 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
2234 unchained to the signer's certificates.
2235 The input signed data could be wrapped in a ContentInfo structure.
2236
2237 @param[in] P7Data Pointer to the PKCS#7 message.
2238 @param[in] P7Length Length of the PKCS#7 message in bytes.
2239 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
2240 certificate. It's caller's responsibility to free the buffer
2241 with Pkcs7FreeSigners().
2242 This data structure is EFI_CERT_STACK type.
2243 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
2244 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
2245 responsibility to free the buffer with Pkcs7FreeSigners().
2246 This data structure is EFI_CERT_STACK type.
2247 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
2248
2249 @retval TRUE The operation is finished successfully.
2250 @retval FALSE Error occurs during the operation.
2251
2252 **/
2253 BOOLEAN
2254 EFIAPI
2255 Pkcs7GetCertificatesList (
2256 IN CONST UINT8 *P7Data,
2257 IN UINTN P7Length,
2258 OUT UINT8 **SignerChainCerts,
2259 OUT UINTN *ChainLength,
2260 OUT UINT8 **UnchainCerts,
2261 OUT UINTN *UnchainLength
2262 );
2263
2264 /**
2265 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
2266 Syntax Standard, version 1.5". This interface is only intended to be used for
2267 application to perform PKCS#7 functionality validation.
2268
2269 If this interface is not supported, then return FALSE.
2270
2271 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
2272 data signing.
2273 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
2274 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
2275 key data.
2276 @param[in] InData Pointer to the content to be signed.
2277 @param[in] InDataSize Size of InData in bytes.
2278 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
2279 @param[in] OtherCerts Pointer to an optional additional set of certificates to
2280 include in the PKCS#7 signedData (e.g. any intermediate
2281 CAs in the chain).
2282 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
2283 responsibility to free the buffer with FreePool().
2284 @param[out] SignedDataSize Size of SignedData in bytes.
2285
2286 @retval TRUE PKCS#7 data signing succeeded.
2287 @retval FALSE PKCS#7 data signing failed.
2288 @retval FALSE This interface is not supported.
2289
2290 **/
2291 BOOLEAN
2292 EFIAPI
2293 Pkcs7Sign (
2294 IN CONST UINT8 *PrivateKey,
2295 IN UINTN PrivateKeySize,
2296 IN CONST UINT8 *KeyPassword,
2297 IN UINT8 *InData,
2298 IN UINTN InDataSize,
2299 IN UINT8 *SignCert,
2300 IN UINT8 *OtherCerts OPTIONAL,
2301 OUT UINT8 **SignedData,
2302 OUT UINTN *SignedDataSize
2303 );
2304
2305 /**
2306 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
2307 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2308 in a ContentInfo structure.
2309
2310 If P7Data, TrustedCert or InData is NULL, then return FALSE.
2311 If P7Length, CertLength or DataLength overflow, then return FALSE.
2312 If this interface is not supported, then return FALSE.
2313
2314 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2315 @param[in] P7Length Length of the PKCS#7 message in bytes.
2316 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2317 is used for certificate chain verification.
2318 @param[in] CertLength Length of the trusted certificate in bytes.
2319 @param[in] InData Pointer to the content to be verified.
2320 @param[in] DataLength Length of InData in bytes.
2321
2322 @retval TRUE The specified PKCS#7 signed data is valid.
2323 @retval FALSE Invalid PKCS#7 signed data.
2324 @retval FALSE This interface is not supported.
2325
2326 **/
2327 BOOLEAN
2328 EFIAPI
2329 Pkcs7Verify (
2330 IN CONST UINT8 *P7Data,
2331 IN UINTN P7Length,
2332 IN CONST UINT8 *TrustedCert,
2333 IN UINTN CertLength,
2334 IN CONST UINT8 *InData,
2335 IN UINTN DataLength
2336 );
2337
2338 /**
2339 This function receives a PKCS7 formatted signature, and then verifies that
2340 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2341 leaf signing certificate.
2342 Note that this function does not validate the certificate chain.
2343
2344 Applications for custom EKU's are quite flexible. For example, a policy EKU
2345 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2346 certificate issued might also contain this EKU, thus constraining the
2347 sub-ordinate certificate. Other applications might allow a certificate
2348 embedded in a device to specify that other Object Identifiers (OIDs) are
2349 present which contains binary data specifying custom capabilities that
2350 the device is able to do.
2351
2352 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2353 containing the content block with both the signature,
2354 the signer's certificate, and any necessary intermediate
2355 certificates.
2356 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2357 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2358 required EKUs that must be present in the signature.
2359 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2360 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2361 must be present in the leaf signer. If it is
2362 FALSE, then we will succeed if we find any
2363 of the specified EKU's.
2364
2365 @retval EFI_SUCCESS The required EKUs were found in the signature.
2366 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2367 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2368
2369 **/
2370 RETURN_STATUS
2371 EFIAPI
2372 VerifyEKUsInPkcs7Signature (
2373 IN CONST UINT8 *Pkcs7Signature,
2374 IN CONST UINT32 SignatureSize,
2375 IN CONST CHAR8 *RequiredEKUs[],
2376 IN CONST UINT32 RequiredEKUsSize,
2377 IN BOOLEAN RequireAllPresent
2378 );
2379
2380 /**
2381 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2382 data could be wrapped in a ContentInfo structure.
2383
2384 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2385 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2386
2387 Caution: This function may receive untrusted input. So this function will do
2388 basic check for PKCS#7 data structure.
2389
2390 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2391 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2392 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2393 It's caller's responsibility to free the buffer with FreePool().
2394 @param[out] ContentSize The size of the extracted content in bytes.
2395
2396 @retval TRUE The P7Data was correctly formatted for processing.
2397 @retval FALSE The P7Data was not correctly formatted for processing.
2398
2399 **/
2400 BOOLEAN
2401 EFIAPI
2402 Pkcs7GetAttachedContent (
2403 IN CONST UINT8 *P7Data,
2404 IN UINTN P7Length,
2405 OUT VOID **Content,
2406 OUT UINTN *ContentSize
2407 );
2408
2409 /**
2410 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2411 Authenticode Portable Executable Signature Format".
2412
2413 If AuthData is NULL, then return FALSE.
2414 If ImageHash is NULL, then return FALSE.
2415 If this interface is not supported, then return FALSE.
2416
2417 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2418 PE/COFF image to be verified.
2419 @param[in] DataSize Size of the Authenticode Signature in bytes.
2420 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2421 is used for certificate chain verification.
2422 @param[in] CertSize Size of the trusted certificate in bytes.
2423 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2424 for calculating the image hash value is described in Authenticode
2425 specification.
2426 @param[in] HashSize Size of Image hash value in bytes.
2427
2428 @retval TRUE The specified Authenticode Signature is valid.
2429 @retval FALSE Invalid Authenticode Signature.
2430 @retval FALSE This interface is not supported.
2431
2432 **/
2433 BOOLEAN
2434 EFIAPI
2435 AuthenticodeVerify (
2436 IN CONST UINT8 *AuthData,
2437 IN UINTN DataSize,
2438 IN CONST UINT8 *TrustedCert,
2439 IN UINTN CertSize,
2440 IN CONST UINT8 *ImageHash,
2441 IN UINTN HashSize
2442 );
2443
2444 /**
2445 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2446 signature.
2447
2448 If AuthData is NULL, then return FALSE.
2449 If this interface is not supported, then return FALSE.
2450
2451 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2452 PE/COFF image to be verified.
2453 @param[in] DataSize Size of the Authenticode Signature in bytes.
2454 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2455 is used for TSA certificate chain verification.
2456 @param[in] CertSize Size of the trusted certificate in bytes.
2457 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2458 signature is valid.
2459
2460 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2461 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2462
2463 **/
2464 BOOLEAN
2465 EFIAPI
2466 ImageTimestampVerify (
2467 IN CONST UINT8 *AuthData,
2468 IN UINTN DataSize,
2469 IN CONST UINT8 *TsaCert,
2470 IN UINTN CertSize,
2471 OUT EFI_TIME *SigningTime
2472 );
2473
2474 /**
2475 Retrieve the version from one X.509 certificate.
2476
2477 If Cert is NULL, then return FALSE.
2478 If CertSize is 0, then return FALSE.
2479 If this interface is not supported, then return FALSE.
2480
2481 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2482 @param[in] CertSize Size of the X509 certificate in bytes.
2483 @param[out] Version Pointer to the retrieved version integer.
2484
2485 @retval TRUE The certificate version retrieved successfully.
2486 @retval FALSE If Cert is NULL or CertSize is Zero.
2487 @retval FALSE The operation is not supported.
2488
2489 **/
2490 BOOLEAN
2491 EFIAPI
2492 X509GetVersion (
2493 IN CONST UINT8 *Cert,
2494 IN UINTN CertSize,
2495 OUT UINTN *Version
2496 );
2497
2498 /**
2499 Retrieve the serialNumber from one X.509 certificate.
2500
2501 If Cert is NULL, then return FALSE.
2502 If CertSize is 0, then return FALSE.
2503 If this interface is not supported, then return FALSE.
2504
2505 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2506 @param[in] CertSize Size of the X509 certificate in bytes.
2507 @param[out] SerialNumber Pointer to the retrieved certificate SerialNumber bytes.
2508 @param[in, out] SerialNumberSize The size in bytes of the SerialNumber buffer on input,
2509 and the size of buffer returned SerialNumber on output.
2510
2511 @retval TRUE The certificate serialNumber retrieved successfully.
2512 @retval FALSE If Cert is NULL or CertSize is Zero.
2513 If SerialNumberSize is NULL.
2514 If Certificate is invalid.
2515 @retval FALSE If no SerialNumber exists.
2516 @retval FALSE If the SerialNumber is NULL. The required buffer size
2517 (including the final null) is returned in the
2518 SerialNumberSize parameter.
2519 @retval FALSE The operation is not supported.
2520 **/
2521 BOOLEAN
2522 EFIAPI
2523 X509GetSerialNumber (
2524 IN CONST UINT8 *Cert,
2525 IN UINTN CertSize,
2526 OUT UINT8 *SerialNumber, OPTIONAL
2527 IN OUT UINTN *SerialNumberSize
2528 );
2529
2530 /**
2531 Retrieve the issuer bytes from one X.509 certificate.
2532
2533 If Cert is NULL, then return FALSE.
2534 If CertIssuerSize is NULL, then return FALSE.
2535 If this interface is not supported, then return FALSE.
2536
2537 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2538 @param[in] CertSize Size of the X509 certificate in bytes.
2539 @param[out] CertIssuer Pointer to the retrieved certificate subject bytes.
2540 @param[in, out] CertIssuerSize The size in bytes of the CertIssuer buffer on input,
2541 and the size of buffer returned CertSubject on output.
2542
2543 @retval TRUE The certificate issuer retrieved successfully.
2544 @retval FALSE Invalid certificate, or the CertIssuerSize is too small for the result.
2545 The CertIssuerSize will be updated with the required size.
2546 @retval FALSE This interface is not supported.
2547
2548 **/
2549 BOOLEAN
2550 EFIAPI
2551 X509GetIssuerName (
2552 IN CONST UINT8 *Cert,
2553 IN UINTN CertSize,
2554 OUT UINT8 *CertIssuer,
2555 IN OUT UINTN *CertIssuerSize
2556 );
2557
2558 /**
2559 Retrieve the Signature Algorithm from one X.509 certificate.
2560
2561 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2562 @param[in] CertSize Size of the X509 certificate in bytes.
2563 @param[out] Oid Signature Algorithm Object identifier buffer.
2564 @param[in,out] OidSize Signature Algorithm Object identifier buffer size
2565
2566 @retval TRUE The certificate Extension data retrieved successfully.
2567 @retval FALSE If Cert is NULL.
2568 If OidSize is NULL.
2569 If Oid is not NULL and *OidSize is 0.
2570 If Certificate is invalid.
2571 @retval FALSE If no SignatureType.
2572 @retval FALSE If the Oid is NULL. The required buffer size
2573 is returned in the OidSize.
2574 @retval FALSE The operation is not supported.
2575 **/
2576 BOOLEAN
2577 EFIAPI
2578 X509GetSignatureAlgorithm (
2579 IN CONST UINT8 *Cert,
2580 IN UINTN CertSize,
2581 OUT UINT8 *Oid, OPTIONAL
2582 IN OUT UINTN *OidSize
2583 );
2584
2585 /**
2586 Retrieve Extension data from one X.509 certificate.
2587
2588 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2589 @param[in] CertSize Size of the X509 certificate in bytes.
2590 @param[in] Oid Object identifier buffer
2591 @param[in] OidSize Object identifier buffer size
2592 @param[out] ExtensionData Extension bytes.
2593 @param[in, out] ExtensionDataSize Extension bytes size.
2594
2595 @retval TRUE The certificate Extension data retrieved successfully.
2596 @retval FALSE If Cert is NULL.
2597 If ExtensionDataSize is NULL.
2598 If ExtensionData is not NULL and *ExtensionDataSize is 0.
2599 If Certificate is invalid.
2600 @retval FALSE If no Extension entry match Oid.
2601 @retval FALSE If the ExtensionData is NULL. The required buffer size
2602 is returned in the ExtensionDataSize parameter.
2603 @retval FALSE The operation is not supported.
2604 **/
2605 BOOLEAN
2606 EFIAPI
2607 X509GetExtensionData (
2608 IN CONST UINT8 *Cert,
2609 IN UINTN CertSize,
2610 IN CONST UINT8 *Oid,
2611 IN UINTN OidSize,
2612 OUT UINT8 *ExtensionData,
2613 IN OUT UINTN *ExtensionDataSize
2614 );
2615
2616 /**
2617 Retrieve the Validity from one X.509 certificate
2618
2619 If Cert is NULL, then return FALSE.
2620 If CertIssuerSize is NULL, then return FALSE.
2621 If this interface is not supported, then return FALSE.
2622
2623 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2624 @param[in] CertSize Size of the X509 certificate in bytes.
2625 @param[in] From notBefore Pointer to DateTime object.
2626 @param[in,out] FromSize notBefore DateTime object size.
2627 @param[in] To notAfter Pointer to DateTime object.
2628 @param[in,out] ToSize notAfter DateTime object size.
2629
2630 Note: X509CompareDateTime to compare DateTime oject
2631 x509SetDateTime to get a DateTime object from a DateTimeStr
2632
2633 @retval TRUE The certificate Validity retrieved successfully.
2634 @retval FALSE Invalid certificate, or Validity retrieve failed.
2635 @retval FALSE This interface is not supported.
2636 **/
2637 BOOLEAN
2638 EFIAPI
2639 X509GetValidity (
2640 IN CONST UINT8 *Cert,
2641 IN UINTN CertSize,
2642 IN UINT8 *From,
2643 IN OUT UINTN *FromSize,
2644 IN UINT8 *To,
2645 IN OUT UINTN *ToSize
2646 );
2647
2648 /**
2649 Format a DateTimeStr to DataTime object in DataTime Buffer
2650
2651 If DateTimeStr is NULL, then return FALSE.
2652 If DateTimeSize is NULL, then return FALSE.
2653 If this interface is not supported, then return FALSE.
2654
2655 @param[in] DateTimeStr DateTime string like YYYYMMDDhhmmssZ
2656 Ref: https://www.w3.org/TR/NOTE-datetime
2657 Z stand for UTC time
2658 @param[out] DateTime Pointer to a DateTime object.
2659 @param[in,out] DateTimeSize DateTime object buffer size.
2660
2661 @retval TRUE The DateTime object create successfully.
2662 @retval FALSE If DateTimeStr is NULL.
2663 If DateTimeSize is NULL.
2664 If DateTime is not NULL and *DateTimeSize is 0.
2665 If Year Month Day Hour Minute Second combination is invalid datetime.
2666 @retval FALSE If the DateTime is NULL. The required buffer size
2667 (including the final null) is returned in the
2668 DateTimeSize parameter.
2669 @retval FALSE The operation is not supported.
2670 **/
2671 BOOLEAN
2672 EFIAPI
2673 X509FormatDateTime (
2674 IN CONST CHAR8 *DateTimeStr,
2675 OUT VOID *DateTime,
2676 IN OUT UINTN *DateTimeSize
2677 );
2678
2679 /**
2680 Compare DateTime1 object and DateTime2 object.
2681
2682 If DateTime1 is NULL, then return -2.
2683 If DateTime2 is NULL, then return -2.
2684 If DateTime1 == DateTime2, then return 0
2685 If DateTime1 > DateTime2, then return 1
2686 If DateTime1 < DateTime2, then return -1
2687
2688 @param[in] DateTime1 Pointer to a DateTime Ojbect
2689 @param[in] DateTime2 Pointer to a DateTime Object
2690
2691 @retval 0 If DateTime1 == DateTime2
2692 @retval 1 If DateTime1 > DateTime2
2693 @retval -1 If DateTime1 < DateTime2
2694 **/
2695 INT32
2696 EFIAPI
2697 X509CompareDateTime (
2698 IN CONST VOID *DateTime1,
2699 IN CONST VOID *DateTime2
2700 );
2701
2702 /**
2703 Retrieve the Key Usage from one X.509 certificate.
2704
2705 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2706 @param[in] CertSize Size of the X509 certificate in bytes.
2707 @param[out] Usage Key Usage (CRYPTO_X509_KU_*)
2708
2709 @retval TRUE The certificate Key Usage retrieved successfully.
2710 @retval FALSE Invalid certificate, or Usage is NULL
2711 @retval FALSE This interface is not supported.
2712 **/
2713 BOOLEAN
2714 EFIAPI
2715 X509GetKeyUsage (
2716 IN CONST UINT8 *Cert,
2717 IN UINTN CertSize,
2718 OUT UINTN *Usage
2719 );
2720
2721 /**
2722 Retrieve the Extended Key Usage from one X.509 certificate.
2723
2724 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2725 @param[in] CertSize Size of the X509 certificate in bytes.
2726 @param[out] Usage Key Usage bytes.
2727 @param[in, out] UsageSize Key Usage buffer sizs in bytes.
2728
2729 @retval TRUE The Usage bytes retrieve successfully.
2730 @retval FALSE If Cert is NULL.
2731 If CertSize is NULL.
2732 If Usage is not NULL and *UsageSize is 0.
2733 If Cert is invalid.
2734 @retval FALSE If the Usage is NULL. The required buffer size
2735 is returned in the UsageSize parameter.
2736 @retval FALSE The operation is not supported.
2737 **/
2738 BOOLEAN
2739 EFIAPI
2740 X509GetExtendedKeyUsage (
2741 IN CONST UINT8 *Cert,
2742 IN UINTN CertSize,
2743 OUT UINT8 *Usage,
2744 IN OUT UINTN *UsageSize
2745 );
2746
2747 /**
2748 Verify one X509 certificate was issued by the trusted CA.
2749 @param[in] RootCert Trusted Root Certificate buffer
2750
2751 @param[in] RootCertLength Trusted Root Certificate buffer length
2752 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
2753 where the first certificate is signed by the Root
2754 Certificate or is the Root Cerificate itself. and
2755 subsequent cerificate is signed by the preceding
2756 cerificate.
2757 @param[in] CertChainLength Total length of the certificate chain, in bytes.
2758
2759 @retval TRUE All cerificates was issued by the first certificate in X509Certchain.
2760 @retval FALSE Invalid certificate or the certificate was not issued by the given
2761 trusted CA.
2762 **/
2763 BOOLEAN
2764 EFIAPI
2765 X509VerifyCertChain (
2766 IN CONST UINT8 *RootCert,
2767 IN UINTN RootCertLength,
2768 IN CONST UINT8 *CertChain,
2769 IN UINTN CertChainLength
2770 );
2771
2772 /**
2773 Get one X509 certificate from CertChain.
2774
2775 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
2776 where the first certificate is signed by the Root
2777 Certificate or is the Root Cerificate itself. and
2778 subsequent cerificate is signed by the preceding
2779 cerificate.
2780 @param[in] CertChainLength Total length of the certificate chain, in bytes.
2781
2782 @param[in] CertIndex Index of certificate. If index is -1 indecate the
2783 last certificate in CertChain.
2784
2785 @param[out] Cert The certificate at the index of CertChain.
2786 @param[out] CertLength The length certificate at the index of CertChain.
2787
2788 @retval TRUE Success.
2789 @retval FALSE Failed to get certificate from certificate chain.
2790 **/
2791 BOOLEAN
2792 EFIAPI
2793 X509GetCertFromCertChain (
2794 IN CONST UINT8 *CertChain,
2795 IN UINTN CertChainLength,
2796 IN CONST INT32 CertIndex,
2797 OUT CONST UINT8 **Cert,
2798 OUT UINTN *CertLength
2799 );
2800
2801 /**
2802 Retrieve the tag and length of the tag.
2803
2804 @param Ptr The position in the ASN.1 data
2805 @param End End of data
2806 @param Length The variable that will receive the length
2807 @param Tag The expected tag
2808
2809 @retval TRUE Get tag successful
2810 @retval FALSe Failed to get tag or tag not match
2811 **/
2812 BOOLEAN
2813 EFIAPI
2814 Asn1GetTag (
2815 IN OUT UINT8 **Ptr,
2816 IN CONST UINT8 *End,
2817 OUT UINTN *Length,
2818 IN UINT32 Tag
2819 );
2820
2821 /**
2822 Retrieve the basic constraints from one X.509 certificate.
2823
2824 @param[in] Cert Pointer to the DER-encoded X509 certificate.
2825 @param[in] CertSize size of the X509 certificate in bytes.
2826 @param[out] BasicConstraints basic constraints bytes.
2827 @param[in, out] BasicConstraintsSize basic constraints buffer sizs in bytes.
2828
2829 @retval TRUE The basic constraints retrieve successfully.
2830 @retval FALSE If cert is NULL.
2831 If cert_size is NULL.
2832 If basic_constraints is not NULL and *basic_constraints_size is 0.
2833 If cert is invalid.
2834 @retval FALSE The required buffer size is small.
2835 The return buffer size is basic_constraints_size parameter.
2836 @retval FALSE If no Extension entry match oid.
2837 @retval FALSE The operation is not supported.
2838 **/
2839 BOOLEAN
2840 EFIAPI
2841 X509GetExtendedBasicConstraints (
2842 CONST UINT8 *Cert,
2843 UINTN CertSize,
2844 UINT8 *BasicConstraints,
2845 UINTN *BasicConstraintsSize
2846 );
2847
2848 // =====================================================================================
2849 // DH Key Exchange Primitive
2850 // =====================================================================================
2851
2852 /**
2853 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2854
2855 @return Pointer to the Diffie-Hellman Context that has been initialized.
2856 If the allocations fails, DhNew() returns NULL.
2857 If the interface is not supported, DhNew() returns NULL.
2858
2859 **/
2860 VOID *
2861 EFIAPI
2862 DhNew (
2863 VOID
2864 );
2865
2866 /**
2867 Release the specified DH context.
2868
2869 If the interface is not supported, then ASSERT().
2870
2871 @param[in] DhContext Pointer to the DH context to be released.
2872
2873 **/
2874 VOID
2875 EFIAPI
2876 DhFree (
2877 IN VOID *DhContext
2878 );
2879
2880 /**
2881 Generates DH parameter.
2882
2883 Given generator g, and length of prime number p in bits, this function generates p,
2884 and sets DH context according to value of g and p.
2885
2886 Before this function can be invoked, pseudorandom number generator must be correctly
2887 initialized by RandomSeed().
2888
2889 If DhContext is NULL, then return FALSE.
2890 If Prime is NULL, then return FALSE.
2891 If this interface is not supported, then return FALSE.
2892
2893 @param[in, out] DhContext Pointer to the DH context.
2894 @param[in] Generator Value of generator.
2895 @param[in] PrimeLength Length in bits of prime to be generated.
2896 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2897
2898 @retval TRUE DH parameter generation succeeded.
2899 @retval FALSE Value of Generator is not supported.
2900 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2901 @retval FALSE This interface is not supported.
2902
2903 **/
2904 BOOLEAN
2905 EFIAPI
2906 DhGenerateParameter (
2907 IN OUT VOID *DhContext,
2908 IN UINTN Generator,
2909 IN UINTN PrimeLength,
2910 OUT UINT8 *Prime
2911 );
2912
2913 /**
2914 Sets generator and prime parameters for DH.
2915
2916 Given generator g, and prime number p, this function and sets DH
2917 context accordingly.
2918
2919 If DhContext is NULL, then return FALSE.
2920 If Prime is NULL, then return FALSE.
2921 If this interface is not supported, then return FALSE.
2922
2923 @param[in, out] DhContext Pointer to the DH context.
2924 @param[in] Generator Value of generator.
2925 @param[in] PrimeLength Length in bits of prime to be generated.
2926 @param[in] Prime Pointer to the prime number.
2927
2928 @retval TRUE DH parameter setting succeeded.
2929 @retval FALSE Value of Generator is not supported.
2930 @retval FALSE Value of Generator is not suitable for the Prime.
2931 @retval FALSE Value of Prime is not a prime number.
2932 @retval FALSE Value of Prime is not a safe prime number.
2933 @retval FALSE This interface is not supported.
2934
2935 **/
2936 BOOLEAN
2937 EFIAPI
2938 DhSetParameter (
2939 IN OUT VOID *DhContext,
2940 IN UINTN Generator,
2941 IN UINTN PrimeLength,
2942 IN CONST UINT8 *Prime
2943 );
2944
2945 /**
2946 Generates DH public key.
2947
2948 This function generates random secret exponent, and computes the public key, which is
2949 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2950 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2951 PublicKeySize is set to the required buffer size to obtain the public key.
2952
2953 If DhContext is NULL, then return FALSE.
2954 If PublicKeySize is NULL, then return FALSE.
2955 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2956 If this interface is not supported, then return FALSE.
2957
2958 @param[in, out] DhContext Pointer to the DH context.
2959 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2960 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2961 On output, the size of data returned in PublicKey buffer in bytes.
2962
2963 @retval TRUE DH public key generation succeeded.
2964 @retval FALSE DH public key generation failed.
2965 @retval FALSE PublicKeySize is not large enough.
2966 @retval FALSE This interface is not supported.
2967
2968 **/
2969 BOOLEAN
2970 EFIAPI
2971 DhGenerateKey (
2972 IN OUT VOID *DhContext,
2973 OUT UINT8 *PublicKey,
2974 IN OUT UINTN *PublicKeySize
2975 );
2976
2977 /**
2978 Computes exchanged common key.
2979
2980 Given peer's public key, this function computes the exchanged common key, based on its own
2981 context including value of prime modulus and random secret exponent.
2982
2983 If DhContext is NULL, then return FALSE.
2984 If PeerPublicKey is NULL, then return FALSE.
2985 If KeySize is NULL, then return FALSE.
2986 If Key is NULL, then return FALSE.
2987 If KeySize is not large enough, then return FALSE.
2988 If this interface is not supported, then return FALSE.
2989
2990 @param[in, out] DhContext Pointer to the DH context.
2991 @param[in] PeerPublicKey Pointer to the peer's public key.
2992 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2993 @param[out] Key Pointer to the buffer to receive generated key.
2994 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2995 On output, the size of data returned in Key buffer in bytes.
2996
2997 @retval TRUE DH exchanged key generation succeeded.
2998 @retval FALSE DH exchanged key generation failed.
2999 @retval FALSE KeySize is not large enough.
3000 @retval FALSE This interface is not supported.
3001
3002 **/
3003 BOOLEAN
3004 EFIAPI
3005 DhComputeKey (
3006 IN OUT VOID *DhContext,
3007 IN CONST UINT8 *PeerPublicKey,
3008 IN UINTN PeerPublicKeySize,
3009 OUT UINT8 *Key,
3010 IN OUT UINTN *KeySize
3011 );
3012
3013 // =====================================================================================
3014 // Pseudo-Random Generation Primitive
3015 // =====================================================================================
3016
3017 /**
3018 Sets up the seed value for the pseudorandom number generator.
3019
3020 This function sets up the seed value for the pseudorandom number generator.
3021 If Seed is not NULL, then the seed passed in is used.
3022 If Seed is NULL, then default seed is used.
3023 If this interface is not supported, then return FALSE.
3024
3025 @param[in] Seed Pointer to seed value.
3026 If NULL, default seed is used.
3027 @param[in] SeedSize Size of seed value.
3028 If Seed is NULL, this parameter is ignored.
3029
3030 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
3031 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
3032 @retval FALSE This interface is not supported.
3033
3034 **/
3035 BOOLEAN
3036 EFIAPI
3037 RandomSeed (
3038 IN CONST UINT8 *Seed OPTIONAL,
3039 IN UINTN SeedSize
3040 );
3041
3042 /**
3043 Generates a pseudorandom byte stream of the specified size.
3044
3045 If Output is NULL, then return FALSE.
3046 If this interface is not supported, then return FALSE.
3047
3048 @param[out] Output Pointer to buffer to receive random value.
3049 @param[in] Size Size of random bytes to generate.
3050
3051 @retval TRUE Pseudorandom byte stream generated successfully.
3052 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
3053 @retval FALSE This interface is not supported.
3054
3055 **/
3056 BOOLEAN
3057 EFIAPI
3058 RandomBytes (
3059 OUT UINT8 *Output,
3060 IN UINTN Size
3061 );
3062
3063 // =====================================================================================
3064 // Key Derivation Function Primitive
3065 // =====================================================================================
3066
3067 /**
3068 Derive key data using HMAC-SHA256 based KDF.
3069
3070 @param[in] Key Pointer to the user-supplied key.
3071 @param[in] KeySize Key size in bytes.
3072 @param[in] Salt Pointer to the salt(non-secret) value.
3073 @param[in] SaltSize Salt size in bytes.
3074 @param[in] Info Pointer to the application specific info.
3075 @param[in] InfoSize Info size in bytes.
3076 @param[out] Out Pointer to buffer to receive hkdf value.
3077 @param[in] OutSize Size of hkdf bytes to generate.
3078
3079 @retval TRUE Hkdf generated successfully.
3080 @retval FALSE Hkdf generation failed.
3081
3082 **/
3083 BOOLEAN
3084 EFIAPI
3085 HkdfSha256ExtractAndExpand (
3086 IN CONST UINT8 *Key,
3087 IN UINTN KeySize,
3088 IN CONST UINT8 *Salt,
3089 IN UINTN SaltSize,
3090 IN CONST UINT8 *Info,
3091 IN UINTN InfoSize,
3092 OUT UINT8 *Out,
3093 IN UINTN OutSize
3094 );
3095
3096 /**
3097 Derive SHA256 HMAC-based Extract key Derivation Function (HKDF).
3098
3099 @param[in] Key Pointer to the user-supplied key.
3100 @param[in] KeySize key size in bytes.
3101 @param[in] Salt Pointer to the salt(non-secret) value.
3102 @param[in] SaltSize salt size in bytes.
3103 @param[out] PrkOut Pointer to buffer to receive hkdf value.
3104 @param[in] PrkOutSize size of hkdf bytes to generate.
3105
3106 @retval true Hkdf generated successfully.
3107 @retval false Hkdf generation failed.
3108
3109 **/
3110 BOOLEAN
3111 EFIAPI
3112 HkdfSha256Extract (
3113 IN CONST UINT8 *Key,
3114 IN UINTN KeySize,
3115 IN CONST UINT8 *Salt,
3116 IN UINTN SaltSize,
3117 OUT UINT8 *PrkOut,
3118 UINTN PrkOutSize
3119 );
3120
3121 /**
3122 Derive SHA256 HMAC-based Expand Key Derivation Function (HKDF).
3123
3124 @param[in] Prk Pointer to the user-supplied key.
3125 @param[in] PrkSize Key size in bytes.
3126 @param[in] Info Pointer to the application specific info.
3127 @param[in] InfoSize Info size in bytes.
3128 @param[out] Out Pointer to buffer to receive hkdf value.
3129 @param[in] OutSize Size of hkdf bytes to generate.
3130
3131 @retval TRUE Hkdf generated successfully.
3132 @retval FALSE Hkdf generation failed.
3133
3134 **/
3135 BOOLEAN
3136 EFIAPI
3137 HkdfSha256Expand (
3138 IN CONST UINT8 *Prk,
3139 IN UINTN PrkSize,
3140 IN CONST UINT8 *Info,
3141 IN UINTN InfoSize,
3142 OUT UINT8 *Out,
3143 IN UINTN OutSize
3144 );
3145
3146 /**
3147 Derive SHA384 HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
3148
3149 @param[in] Key Pointer to the user-supplied key.
3150 @param[in] KeySize Key size in bytes.
3151 @param[in] Salt Pointer to the salt(non-secret) value.
3152 @param[in] SaltSize Salt size in bytes.
3153 @param[in] Info Pointer to the application specific info.
3154 @param[in] InfoSize Info size in bytes.
3155 @param[out] Out Pointer to buffer to receive hkdf value.
3156 @param[in] OutSize Size of hkdf bytes to generate.
3157
3158 @retval TRUE Hkdf generated successfully.
3159 @retval FALSE Hkdf generation failed.
3160
3161 **/
3162 BOOLEAN
3163 EFIAPI
3164 HkdfSha384ExtractAndExpand (
3165 IN CONST UINT8 *Key,
3166 IN UINTN KeySize,
3167 IN CONST UINT8 *Salt,
3168 IN UINTN SaltSize,
3169 IN CONST UINT8 *Info,
3170 IN UINTN InfoSize,
3171 OUT UINT8 *Out,
3172 IN UINTN OutSize
3173 );
3174
3175 /**
3176 Derive SHA384 HMAC-based Extract key Derivation Function (HKDF).
3177
3178 @param[in] Key Pointer to the user-supplied key.
3179 @param[in] KeySize key size in bytes.
3180 @param[in] Salt Pointer to the salt(non-secret) value.
3181 @param[in] SaltSize salt size in bytes.
3182 @param[out] PrkOut Pointer to buffer to receive hkdf value.
3183 @param[in] PrkOutSize size of hkdf bytes to generate.
3184
3185 @retval true Hkdf generated successfully.
3186 @retval false Hkdf generation failed.
3187
3188 **/
3189 BOOLEAN
3190 EFIAPI
3191 HkdfSha384Extract (
3192 IN CONST UINT8 *Key,
3193 IN UINTN KeySize,
3194 IN CONST UINT8 *Salt,
3195 IN UINTN SaltSize,
3196 OUT UINT8 *PrkOut,
3197 UINTN PrkOutSize
3198 );
3199
3200 /**
3201 Derive SHA384 HMAC-based Expand Key Derivation Function (HKDF).
3202
3203 @param[in] Prk Pointer to the user-supplied key.
3204 @param[in] PrkSize Key size in bytes.
3205 @param[in] Info Pointer to the application specific info.
3206 @param[in] InfoSize Info size in bytes.
3207 @param[out] Out Pointer to buffer to receive hkdf value.
3208 @param[in] OutSize Size of hkdf bytes to generate.
3209
3210 @retval TRUE Hkdf generated successfully.
3211 @retval FALSE Hkdf generation failed.
3212
3213 **/
3214 BOOLEAN
3215 EFIAPI
3216 HkdfSha384Expand (
3217 IN CONST UINT8 *Prk,
3218 IN UINTN PrkSize,
3219 IN CONST UINT8 *Info,
3220 IN UINTN InfoSize,
3221 OUT UINT8 *Out,
3222 IN UINTN OutSize
3223 );
3224
3225 // =====================================================================================
3226 // Big number primitives
3227 // =====================================================================================
3228
3229 /**
3230 Allocate new Big Number.
3231
3232 @retval New BigNum opaque structure or NULL on failure.
3233 **/
3234 VOID *
3235 EFIAPI
3236 BigNumInit (
3237 VOID
3238 );
3239
3240 /**
3241 Allocate new Big Number and assign the provided value to it.
3242
3243 @param[in] Buf Big endian encoded buffer.
3244 @param[in] Len Buffer length.
3245
3246 @retval New BigNum opaque structure or NULL on failure.
3247 **/
3248 VOID *
3249 EFIAPI
3250 BigNumFromBin (
3251 IN CONST UINT8 *Buf,
3252 IN UINTN Len
3253 );
3254
3255 /**
3256 Convert the absolute value of Bn into big-endian form and store it at Buf.
3257 The Buf array should have at least BigNumBytes() in it.
3258
3259 @param[in] Bn Big number to convert.
3260 @param[out] Buf Output buffer.
3261
3262 @retval The length of the big-endian number placed at Buf or -1 on error.
3263 **/
3264 INTN
3265 EFIAPI
3266 BigNumToBin (
3267 IN CONST VOID *Bn,
3268 OUT UINT8 *Buf
3269 );
3270
3271 /**
3272 Free the Big Number.
3273
3274 @param[in] Bn Big number to free.
3275 @param[in] Clear TRUE if the buffer should be cleared.
3276 **/
3277 VOID
3278 EFIAPI
3279 BigNumFree (
3280 IN VOID *Bn,
3281 IN BOOLEAN Clear
3282 );
3283
3284 /**
3285 Calculate the sum of two Big Numbers.
3286 Please note, all "out" Big number arguments should be properly initialized
3287 by calling to BigNumInit() or BigNumFromBin() functions.
3288
3289 @param[in] BnA Big number.
3290 @param[in] BnB Big number.
3291 @param[out] BnRes The result of BnA + BnB.
3292
3293 @retval TRUE On success.
3294 @retval FALSE Otherwise.
3295 **/
3296 BOOLEAN
3297 EFIAPI
3298 BigNumAdd (
3299 IN CONST VOID *BnA,
3300 IN CONST VOID *BnB,
3301 OUT VOID *BnRes
3302 );
3303
3304 /**
3305 Subtract two Big Numbers.
3306 Please note, all "out" Big number arguments should be properly initialized
3307 by calling to BigNumInit() or BigNumFromBin() functions.
3308
3309 @param[in] BnA Big number.
3310 @param[in] BnB Big number.
3311 @param[out] BnRes The result of BnA - BnB.
3312
3313 @retval TRUE On success.
3314 @retval FALSE Otherwise.
3315 **/
3316 BOOLEAN
3317 EFIAPI
3318 BigNumSub (
3319 IN CONST VOID *BnA,
3320 IN CONST VOID *BnB,
3321 OUT VOID *BnRes
3322 );
3323
3324 /**
3325 Calculate remainder: BnRes = BnA % BnB.
3326 Please note, all "out" Big number arguments should be properly initialized
3327 by calling to BigNumInit() or BigNumFromBin() functions.
3328
3329 @param[in] BnA Big number.
3330 @param[in] BnB Big number.
3331 @param[out] BnRes The result of BnA % BnB.
3332
3333 @retval TRUE On success.
3334 @retval FALSE Otherwise.
3335 **/
3336 BOOLEAN
3337 EFIAPI
3338 BigNumMod (
3339 IN CONST VOID *BnA,
3340 IN CONST VOID *BnB,
3341 OUT VOID *BnRes
3342 );
3343
3344 /**
3345 Compute BnA to the BnP-th power modulo BnM.
3346 Please note, all "out" Big number arguments should be properly initialized
3347 by calling to BigNumInit() or BigNumFromBin() functions.
3348
3349 @param[in] BnA Big number.
3350 @param[in] BnP Big number (power).
3351 @param[in] BnM Big number (modulo).
3352 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
3353
3354 @retval TRUE On success.
3355 @retval FALSE Otherwise.
3356 **/
3357 BOOLEAN
3358 EFIAPI
3359 BigNumExpMod (
3360 IN CONST VOID *BnA,
3361 IN CONST VOID *BnP,
3362 IN CONST VOID *BnM,
3363 OUT VOID *BnRes
3364 );
3365
3366 /**
3367 Compute BnA inverse modulo BnM.
3368 Please note, all "out" Big number arguments should be properly initialized
3369 by calling to BigNumInit() or BigNumFromBin() functions.
3370
3371 @param[in] BnA Big number.
3372 @param[in] BnM Big number (modulo).
3373 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
3374
3375 @retval TRUE On success.
3376 @retval FALSE Otherwise.
3377 **/
3378 BOOLEAN
3379 EFIAPI
3380 BigNumInverseMod (
3381 IN CONST VOID *BnA,
3382 IN CONST VOID *BnM,
3383 OUT VOID *BnRes
3384 );
3385
3386 /**
3387 Divide two Big Numbers.
3388 Please note, all "out" Big number arguments should be properly initialized
3389 by calling to BigNumInit() or BigNumFromBin() functions.
3390
3391 @param[in] BnA Big number.
3392 @param[in] BnB Big number.
3393 @param[out] BnRes The result, such that BnA / BnB.
3394
3395 @retval TRUE On success.
3396 @retval FALSE Otherwise.
3397 **/
3398 BOOLEAN
3399 EFIAPI
3400 BigNumDiv (
3401 IN CONST VOID *BnA,
3402 IN CONST VOID *BnB,
3403 OUT VOID *BnRes
3404 );
3405
3406 /**
3407 Multiply two Big Numbers modulo BnM.
3408 Please note, all "out" Big number arguments should be properly initialized
3409 by calling to BigNumInit() or BigNumFromBin() functions.
3410
3411 @param[in] BnA Big number.
3412 @param[in] BnB Big number.
3413 @param[in] BnM Big number (modulo).
3414 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
3415
3416 @retval TRUE On success.
3417 @retval FALSE Otherwise.
3418 **/
3419 BOOLEAN
3420 EFIAPI
3421 BigNumMulMod (
3422 IN CONST VOID *BnA,
3423 IN CONST VOID *BnB,
3424 IN CONST VOID *BnM,
3425 OUT VOID *BnRes
3426 );
3427
3428 /**
3429 Compare two Big Numbers.
3430
3431 @param[in] BnA Big number.
3432 @param[in] BnB Big number.
3433
3434 @retval 0 BnA == BnB.
3435 @retval 1 BnA > BnB.
3436 @retval -1 BnA < BnB.
3437 **/
3438 INTN
3439 EFIAPI
3440 BigNumCmp (
3441 IN CONST VOID *BnA,
3442 IN CONST VOID *BnB
3443 );
3444
3445 /**
3446 Get number of bits in Bn.
3447
3448 @param[in] Bn Big number.
3449
3450 @retval Number of bits.
3451 **/
3452
3453 UINTN
3454 EFIAPI
3455 BigNumBits (
3456 IN CONST VOID *Bn
3457 );
3458
3459 /**
3460 Get number of bytes in Bn.
3461
3462 @param[in] Bn Big number.
3463
3464 @retval Number of bytes.
3465 **/
3466 UINTN
3467 EFIAPI
3468 BigNumBytes (
3469 IN CONST VOID *Bn
3470 );
3471
3472 /**
3473 Checks if Big Number equals to the given Num.
3474
3475 @param[in] Bn Big number.
3476 @param[in] Num Number.
3477
3478 @retval TRUE iff Bn == Num.
3479 @retval FALSE otherwise.
3480 **/
3481 BOOLEAN
3482 EFIAPI
3483 BigNumIsWord (
3484 IN CONST VOID *Bn,
3485 IN UINTN Num
3486 );
3487
3488 /**
3489 Checks if Big Number is odd.
3490
3491 @param[in] Bn Big number.
3492
3493 @retval TRUE Bn is odd (Bn % 2 == 1).
3494 @retval FALSE otherwise.
3495 **/
3496 BOOLEAN
3497 EFIAPI
3498 BigNumIsOdd (
3499 IN CONST VOID *Bn
3500 );
3501
3502 /**
3503 Copy Big number.
3504
3505 @param[out] BnDst Destination.
3506 @param[in] BnSrc Source.
3507
3508 @retval BnDst on success.
3509 @retval NULL otherwise.
3510 **/
3511 VOID *
3512 EFIAPI
3513 BigNumCopy (
3514 OUT VOID *BnDst,
3515 IN CONST VOID *BnSrc
3516 );
3517
3518 /**
3519 Get constant Big number with value of "1".
3520 This may be used to save expensive allocations.
3521
3522 @retval Big Number with value of 1.
3523 **/
3524 CONST VOID *
3525 EFIAPI
3526 BigNumValueOne (
3527 VOID
3528 );
3529
3530 /**
3531 Shift right Big Number.
3532 Please note, all "out" Big number arguments should be properly initialized
3533 by calling to BigNumInit() or BigNumFromBin() functions.
3534
3535 @param[in] Bn Big number.
3536 @param[in] N Number of bits to shift.
3537 @param[out] BnRes The result.
3538
3539 @retval TRUE On success.
3540 @retval FALSE Otherwise.
3541 **/
3542 BOOLEAN
3543 EFIAPI
3544 BigNumRShift (
3545 IN CONST VOID *Bn,
3546 IN UINTN N,
3547 OUT VOID *BnRes
3548 );
3549
3550 /**
3551 Mark Big Number for constant time computations.
3552 This function should be called before any constant time computations are
3553 performed on the given Big number.
3554
3555 @param[in] Bn Big number.
3556 **/
3557 VOID
3558 EFIAPI
3559 BigNumConstTime (
3560 IN VOID *Bn
3561 );
3562
3563 /**
3564 Calculate square modulo.
3565 Please note, all "out" Big number arguments should be properly initialized
3566 by calling to BigNumInit() or BigNumFromBin() functions.
3567
3568 @param[in] BnA Big number.
3569 @param[in] BnM Big number (modulo).
3570 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
3571
3572 @retval TRUE On success.
3573 @retval FALSE Otherwise.
3574 **/
3575 BOOLEAN
3576 EFIAPI
3577 BigNumSqrMod (
3578 IN CONST VOID *BnA,
3579 IN CONST VOID *BnM,
3580 OUT VOID *BnRes
3581 );
3582
3583 /**
3584 Create new Big Number computation context. This is an opaque structure
3585 which should be passed to any function that requires it. The BN context is
3586 needed to optimize calculations and expensive allocations.
3587
3588 @retval Big Number context struct or NULL on failure.
3589 **/
3590 VOID *
3591 EFIAPI
3592 BigNumNewContext (
3593 VOID
3594 );
3595
3596 /**
3597 Free Big Number context that was allocated with BigNumNewContext().
3598
3599 @param[in] BnCtx Big number context to free.
3600 **/
3601 VOID
3602 EFIAPI
3603 BigNumContextFree (
3604 IN VOID *BnCtx
3605 );
3606
3607 /**
3608 Set Big Number to a given value.
3609
3610 @param[in] Bn Big number to set.
3611 @param[in] Val Value to set.
3612
3613 @retval TRUE On success.
3614 @retval FALSE Otherwise.
3615 **/
3616 BOOLEAN
3617 EFIAPI
3618 BigNumSetUint (
3619 IN VOID *Bn,
3620 IN UINTN Val
3621 );
3622
3623 /**
3624 Add two Big Numbers modulo BnM.
3625
3626 @param[in] BnA Big number.
3627 @param[in] BnB Big number.
3628 @param[in] BnM Big number (modulo).
3629 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
3630
3631 @retval TRUE On success.
3632 @retval FALSE Otherwise.
3633 **/
3634 BOOLEAN
3635 EFIAPI
3636 BigNumAddMod (
3637 IN CONST VOID *BnA,
3638 IN CONST VOID *BnB,
3639 IN CONST VOID *BnM,
3640 OUT VOID *BnRes
3641 );
3642
3643 // =====================================================================================
3644 // Basic Elliptic Curve Primitives
3645 // =====================================================================================
3646
3647 /**
3648 Initialize new opaque EcGroup object. This object represents an EC curve and
3649 and is used for calculation within this group. This object should be freed
3650 using EcGroupFree() function.
3651
3652 @param[in] CryptoNid Identifying number for the ECC curve (Defined in
3653 BaseCryptLib.h).
3654
3655 @retval EcGroup object On success.
3656 @retval NULL On failure.
3657 **/
3658 VOID *
3659 EFIAPI
3660 EcGroupInit (
3661 IN UINTN CryptoNid
3662 );
3663
3664 /**
3665 Get EC curve parameters. While elliptic curve equation is Y^2 mod P = (X^3 + AX + B) Mod P.
3666 This function will set the provided Big Number objects to the corresponding
3667 values. The caller needs to make sure all the "out" BigNumber parameters
3668 are properly initialized.
3669
3670 @param[in] EcGroup EC group object.
3671 @param[out] BnPrime Group prime number.
3672 @param[out] BnA A coefficient.
3673 @param[out] BnB B coefficient.
3674 @param[in] BnCtx BN context.
3675
3676 @retval TRUE On success.
3677 @retval FALSE Otherwise.
3678 **/
3679 BOOLEAN
3680 EFIAPI
3681 EcGroupGetCurve (
3682 IN CONST VOID *EcGroup,
3683 OUT VOID *BnPrime,
3684 OUT VOID *BnA,
3685 OUT VOID *BnB,
3686 IN VOID *BnCtx
3687 );
3688
3689 /**
3690 Get EC group order.
3691 This function will set the provided Big Number object to the corresponding
3692 value. The caller needs to make sure that the "out" BigNumber parameter
3693 is properly initialized.
3694
3695 @param[in] EcGroup EC group object.
3696 @param[out] BnOrder Group prime number.
3697
3698 @retval TRUE On success.
3699 @retval FALSE Otherwise.
3700 **/
3701 BOOLEAN
3702 EFIAPI
3703 EcGroupGetOrder (
3704 IN VOID *EcGroup,
3705 OUT VOID *BnOrder
3706 );
3707
3708 /**
3709 Free previously allocated EC group object using EcGroupInit().
3710
3711 @param[in] EcGroup EC group object to free.
3712 **/
3713 VOID
3714 EFIAPI
3715 EcGroupFree (
3716 IN VOID *EcGroup
3717 );
3718
3719 /**
3720 Initialize new opaque EC Point object. This object represents an EC point
3721 within the given EC group (curve).
3722
3723 @param[in] EC Group, properly initialized using EcGroupInit().
3724
3725 @retval EC Point object On success.
3726 @retval NULL On failure.
3727 **/
3728 VOID *
3729 EFIAPI
3730 EcPointInit (
3731 IN CONST VOID *EcGroup
3732 );
3733
3734 /**
3735 Free previously allocated EC Point object using EcPointInit().
3736
3737 @param[in] EcPoint EC Point to free.
3738 @param[in] Clear TRUE iff the memory should be cleared.
3739 **/
3740 VOID
3741 EFIAPI
3742 EcPointDeInit (
3743 IN VOID *EcPoint,
3744 IN BOOLEAN Clear
3745 );
3746
3747 /**
3748 Get EC point affine (x,y) coordinates.
3749 This function will set the provided Big Number objects to the corresponding
3750 values. The caller needs to make sure all the "out" BigNumber parameters
3751 are properly initialized.
3752
3753 @param[in] EcGroup EC group object.
3754 @param[in] EcPoint EC point object.
3755 @param[out] BnX X coordinate.
3756 @param[out] BnY Y coordinate.
3757 @param[in] BnCtx BN context, created with BigNumNewContext().
3758
3759 @retval TRUE On success.
3760 @retval FALSE Otherwise.
3761 **/
3762 BOOLEAN
3763 EFIAPI
3764 EcPointGetAffineCoordinates (
3765 IN CONST VOID *EcGroup,
3766 IN CONST VOID *EcPoint,
3767 OUT VOID *BnX,
3768 OUT VOID *BnY,
3769 IN VOID *BnCtx
3770 );
3771
3772 /**
3773 Set EC point affine (x,y) coordinates.
3774
3775 @param[in] EcGroup EC group object.
3776 @param[in] EcPoint EC point object.
3777 @param[in] BnX X coordinate.
3778 @param[in] BnY Y coordinate.
3779 @param[in] BnCtx BN context, created with BigNumNewContext().
3780
3781 @retval TRUE On success.
3782 @retval FALSE Otherwise.
3783 **/
3784 BOOLEAN
3785 EFIAPI
3786 EcPointSetAffineCoordinates (
3787 IN CONST VOID *EcGroup,
3788 IN VOID *EcPoint,
3789 IN CONST VOID *BnX,
3790 IN CONST VOID *BnY,
3791 IN VOID *BnCtx
3792 );
3793
3794 /**
3795 EC Point addition. EcPointResult = EcPointA + EcPointB.
3796
3797 @param[in] EcGroup EC group object.
3798 @param[out] EcPointResult EC point to hold the result. The point should
3799 be properly initialized.
3800 @param[in] EcPointA EC Point.
3801 @param[in] EcPointB EC Point.
3802 @param[in] BnCtx BN context, created with BigNumNewContext().
3803
3804 @retval TRUE On success.
3805 @retval FALSE Otherwise.
3806 **/
3807 BOOLEAN
3808 EFIAPI
3809 EcPointAdd (
3810 IN CONST VOID *EcGroup,
3811 OUT VOID *EcPointResult,
3812 IN CONST VOID *EcPointA,
3813 IN CONST VOID *EcPointB,
3814 IN VOID *BnCtx
3815 );
3816
3817 /**
3818 Variable EC point multiplication. EcPointResult = EcPoint * BnPScalar.
3819
3820 @param[in] EcGroup EC group object.
3821 @param[out] EcPointResult EC point to hold the result. The point should
3822 be properly initialized.
3823 @param[in] EcPoint EC Point.
3824 @param[in] BnPScalar P Scalar.
3825 @param[in] BnCtx BN context, created with BigNumNewContext().
3826
3827 @retval TRUE On success.
3828 @retval FALSE Otherwise.
3829 **/
3830 BOOLEAN
3831 EFIAPI
3832 EcPointMul (
3833 IN CONST VOID *EcGroup,
3834 OUT VOID *EcPointResult,
3835 IN CONST VOID *EcPoint,
3836 IN CONST VOID *BnPScalar,
3837 IN VOID *BnCtx
3838 );
3839
3840 /**
3841 Calculate the inverse of the supplied EC point.
3842
3843 @param[in] EcGroup EC group object.
3844 @param[in,out] EcPoint EC point to invert.
3845 @param[in] BnCtx BN context, created with BigNumNewContext().
3846
3847 @retval TRUE On success.
3848 @retval FALSE Otherwise.
3849 **/
3850 BOOLEAN
3851 EFIAPI
3852 EcPointInvert (
3853 IN CONST VOID *EcGroup,
3854 IN OUT VOID *EcPoint,
3855 IN VOID *BnCtx
3856 );
3857
3858 /**
3859 Check if the supplied point is on EC curve.
3860
3861 @param[in] EcGroup EC group object.
3862 @param[in] EcPoint EC point to check.
3863 @param[in] BnCtx BN context, created with BigNumNewContext().
3864
3865 @retval TRUE On curve.
3866 @retval FALSE Otherwise.
3867 **/
3868 BOOLEAN
3869 EFIAPI
3870 EcPointIsOnCurve (
3871 IN CONST VOID *EcGroup,
3872 IN CONST VOID *EcPoint,
3873 IN VOID *BnCtx
3874 );
3875
3876 /**
3877 Check if the supplied point is at infinity.
3878
3879 @param[in] EcGroup EC group object.
3880 @param[in] EcPoint EC point to check.
3881
3882 @retval TRUE At infinity.
3883 @retval FALSE Otherwise.
3884 **/
3885 BOOLEAN
3886 EFIAPI
3887 EcPointIsAtInfinity (
3888 IN CONST VOID *EcGroup,
3889 IN CONST VOID *EcPoint
3890 );
3891
3892 /**
3893 Check if EC points are equal.
3894
3895 @param[in] EcGroup EC group object.
3896 @param[in] EcPointA EC point A.
3897 @param[in] EcPointB EC point B.
3898 @param[in] BnCtx BN context, created with BigNumNewContext().
3899
3900 @retval TRUE A == B.
3901 @retval FALSE Otherwise.
3902 **/
3903 BOOLEAN
3904 EFIAPI
3905 EcPointEqual (
3906 IN CONST VOID *EcGroup,
3907 IN CONST VOID *EcPointA,
3908 IN CONST VOID *EcPointB,
3909 IN VOID *BnCtx
3910 );
3911
3912 /**
3913 Set EC point compressed coordinates. Points can be described in terms of
3914 their compressed coordinates. For a point (x, y), for any given value for x
3915 such that the point is on the curve there will only ever be two possible
3916 values for y. Therefore, a point can be set using this function where BnX is
3917 the x coordinate and YBit is a value 0 or 1 to identify which of the two
3918 possible values for y should be used.
3919
3920 @param[in] EcGroup EC group object.
3921 @param[in] EcPoint EC Point.
3922 @param[in] BnX X coordinate.
3923 @param[in] YBit 0 or 1 to identify which Y value is used.
3924 @param[in] BnCtx BN context, created with BigNumNewContext().
3925
3926 @retval TRUE On success.
3927 @retval FALSE Otherwise.
3928 **/
3929 BOOLEAN
3930 EFIAPI
3931 EcPointSetCompressedCoordinates (
3932 IN CONST VOID *EcGroup,
3933 IN VOID *EcPoint,
3934 IN CONST VOID *BnX,
3935 IN UINT8 YBit,
3936 IN VOID *BnCtx
3937 );
3938
3939 // =====================================================================================
3940 // Elliptic Curve Diffie Hellman Primitives
3941 // =====================================================================================
3942
3943 /**
3944 Allocates and Initializes one Elliptic Curve Context for subsequent use
3945 with the NID.
3946
3947 @param[in] Nid cipher NID
3948 @return Pointer to the Elliptic Curve Context that has been initialized.
3949 If the allocations fails, EcNewByNid() returns NULL.
3950 **/
3951 VOID *
3952 EFIAPI
3953 EcNewByNid (
3954 IN UINTN Nid
3955 );
3956
3957 /**
3958 Release the specified EC context.
3959
3960 @param[in] EcContext Pointer to the EC context to be released.
3961 **/
3962 VOID
3963 EFIAPI
3964 EcFree (
3965 IN VOID *EcContext
3966 );
3967
3968 /**
3969 Generates EC key and returns EC public key (X, Y), Please note, this function uses
3970 pseudo random number generator. The caller must make sure RandomSeed()
3971 function was properly called before.
3972 The Ec context should be correctly initialized by EcNewByNid.
3973 This function generates random secret, and computes the public key (X, Y), which is
3974 returned via parameter Public, PublicSize.
3975 X is the first half of Public with size being PublicSize / 2,
3976 Y is the second half of Public with size being PublicSize / 2.
3977 EC context is updated accordingly.
3978 If the Public buffer is too small to hold the public X, Y, FALSE is returned and
3979 PublicSize is set to the required buffer size to obtain the public X, Y.
3980 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
3981 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
3982 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
3983 If EcContext is NULL, then return FALSE.
3984 If PublicSize is NULL, then return FALSE.
3985 If PublicSize is large enough but Public is NULL, then return FALSE.
3986 @param[in, out] EcContext Pointer to the EC context.
3987 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
3988 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
3989 On output, the size of data returned in Public buffer in bytes.
3990 @retval TRUE EC public X,Y generation succeeded.
3991 @retval FALSE EC public X,Y generation failed.
3992 @retval FALSE PublicKeySize is not large enough.
3993 **/
3994 BOOLEAN
3995 EFIAPI
3996 EcGenerateKey (
3997 IN OUT VOID *EcContext,
3998 OUT UINT8 *PublicKey,
3999 IN OUT UINTN *PublicKeySize
4000 );
4001
4002 /**
4003 Gets the public key component from the established EC context.
4004 The Ec context should be correctly initialized by EcNewByNid, and successfully
4005 generate key pair from EcGenerateKey().
4006 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
4007 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
4008 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
4009 @param[in, out] EcContext Pointer to EC context being set.
4010 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
4011 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
4012 On output, the size of data returned in Public buffer in bytes.
4013 @retval TRUE EC key component was retrieved successfully.
4014 @retval FALSE Invalid EC key component.
4015 **/
4016 BOOLEAN
4017 EFIAPI
4018 EcGetPubKey (
4019 IN OUT VOID *EcContext,
4020 OUT UINT8 *PublicKey,
4021 IN OUT UINTN *PublicKeySize
4022 );
4023
4024 /**
4025 Computes exchanged common key.
4026 Given peer's public key (X, Y), this function computes the exchanged common key,
4027 based on its own context including value of curve parameter and random secret.
4028 X is the first half of PeerPublic with size being PeerPublicSize / 2,
4029 Y is the second half of PeerPublic with size being PeerPublicSize / 2.
4030 If EcContext is NULL, then return FALSE.
4031 If PeerPublic is NULL, then return FALSE.
4032 If PeerPublicSize is 0, then return FALSE.
4033 If Key is NULL, then return FALSE.
4034 If KeySize is not large enough, then return FALSE.
4035 For P-256, the PeerPublicSize is 64. First 32-byte is X, Second 32-byte is Y.
4036 For P-384, the PeerPublicSize is 96. First 48-byte is X, Second 48-byte is Y.
4037 For P-521, the PeerPublicSize is 132. First 66-byte is X, Second 66-byte is Y.
4038 @param[in, out] EcContext Pointer to the EC context.
4039 @param[in] PeerPublic Pointer to the peer's public X,Y.
4040 @param[in] PeerPublicSize Size of peer's public X,Y in bytes.
4041 @param[in] CompressFlag Flag of PeerPublic is compressed or not.
4042 @param[out] Key Pointer to the buffer to receive generated key.
4043 @param[in, out] KeySize On input, the size of Key buffer in bytes.
4044 On output, the size of data returned in Key buffer in bytes.
4045 @retval TRUE EC exchanged key generation succeeded.
4046 @retval FALSE EC exchanged key generation failed.
4047 @retval FALSE KeySize is not large enough.
4048 **/
4049 BOOLEAN
4050 EFIAPI
4051 EcDhComputeKey (
4052 IN OUT VOID *EcContext,
4053 IN CONST UINT8 *PeerPublic,
4054 IN UINTN PeerPublicSize,
4055 IN CONST INT32 *CompressFlag,
4056 OUT UINT8 *Key,
4057 IN OUT UINTN *KeySize
4058 );
4059
4060 /**
4061 Retrieve the EC Private Key from the password-protected PEM key data.
4062
4063 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
4064 @param[in] PemSize Size of the PEM key data in bytes.
4065 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
4066 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
4067 EC private key component. Use EcFree() function to free the
4068 resource.
4069
4070 If PemData is NULL, then return FALSE.
4071 If EcContext is NULL, then return FALSE.
4072
4073 @retval TRUE EC Private Key was retrieved successfully.
4074 @retval FALSE Invalid PEM key data or incorrect password.
4075
4076 **/
4077 BOOLEAN
4078 EFIAPI
4079 EcGetPrivateKeyFromPem (
4080 IN CONST UINT8 *PemData,
4081 IN UINTN PemSize,
4082 IN CONST CHAR8 *Password,
4083 OUT VOID **EcContext
4084 );
4085
4086 /**
4087 Retrieve the EC Public Key from one DER-encoded X509 certificate.
4088
4089 @param[in] Cert Pointer to the DER-encoded X509 certificate.
4090 @param[in] CertSize Size of the X509 certificate in bytes.
4091 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
4092 EC public key component. Use EcFree() function to free the
4093 resource.
4094
4095 If Cert is NULL, then return FALSE.
4096 If EcContext is NULL, then return FALSE.
4097
4098 @retval TRUE EC Public Key was retrieved successfully.
4099 @retval FALSE Fail to retrieve EC public key from X509 certificate.
4100
4101 **/
4102 BOOLEAN
4103 EFIAPI
4104 EcGetPublicKeyFromX509 (
4105 IN CONST UINT8 *Cert,
4106 IN UINTN CertSize,
4107 OUT VOID **EcContext
4108 );
4109
4110 /**
4111 Carries out the EC-DSA signature.
4112
4113 This function carries out the EC-DSA signature.
4114 If the Signature buffer is too small to hold the contents of signature, FALSE
4115 is returned and SigSize is set to the required buffer size to obtain the signature.
4116
4117 If EcContext is NULL, then return FALSE.
4118 If MessageHash is NULL, then return FALSE.
4119 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
4120 If SigSize is large enough but Signature is NULL, then return FALSE.
4121
4122 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
4123 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
4124 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
4125
4126 @param[in] EcContext Pointer to EC context for signature generation.
4127 @param[in] HashNid hash NID
4128 @param[in] MessageHash Pointer to octet message hash to be signed.
4129 @param[in] HashSize Size of the message hash in bytes.
4130 @param[out] Signature Pointer to buffer to receive EC-DSA signature.
4131 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
4132 On output, the size of data returned in Signature buffer in bytes.
4133
4134 @retval TRUE Signature successfully generated in EC-DSA.
4135 @retval FALSE Signature generation failed.
4136 @retval FALSE SigSize is too small.
4137
4138 **/
4139 BOOLEAN
4140 EFIAPI
4141 EcDsaSign (
4142 IN VOID *EcContext,
4143 IN UINTN HashNid,
4144 IN CONST UINT8 *MessageHash,
4145 IN UINTN HashSize,
4146 OUT UINT8 *Signature,
4147 IN OUT UINTN *SigSize
4148 );
4149
4150 /**
4151 Verifies the EC-DSA signature.
4152
4153 If EcContext is NULL, then return FALSE.
4154 If MessageHash is NULL, then return FALSE.
4155 If Signature is NULL, then return FALSE.
4156 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
4157
4158 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
4159 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
4160 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
4161
4162 @param[in] EcContext Pointer to EC context for signature verification.
4163 @param[in] HashNid hash NID
4164 @param[in] MessageHash Pointer to octet message hash to be checked.
4165 @param[in] HashSize Size of the message hash in bytes.
4166 @param[in] Signature Pointer to EC-DSA signature to be verified.
4167 @param[in] SigSize Size of signature in bytes.
4168
4169 @retval TRUE Valid signature encoded in EC-DSA.
4170 @retval FALSE Invalid signature or invalid EC context.
4171
4172 **/
4173 BOOLEAN
4174 EFIAPI
4175 EcDsaVerify (
4176 IN VOID *EcContext,
4177 IN UINTN HashNid,
4178 IN CONST UINT8 *MessageHash,
4179 IN UINTN HashSize,
4180 IN CONST UINT8 *Signature,
4181 IN UINTN SigSize
4182 );
4183
4184 #endif // __BASE_CRYPT_LIB_H__