]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg: Add EC key retrieving and signature interface.
[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 // DH Key Exchange Primitive
2476 // =====================================================================================
2477
2478 /**
2479 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2480
2481 @return Pointer to the Diffie-Hellman Context that has been initialized.
2482 If the allocations fails, DhNew() returns NULL.
2483 If the interface is not supported, DhNew() returns NULL.
2484
2485 **/
2486 VOID *
2487 EFIAPI
2488 DhNew (
2489 VOID
2490 );
2491
2492 /**
2493 Release the specified DH context.
2494
2495 If the interface is not supported, then ASSERT().
2496
2497 @param[in] DhContext Pointer to the DH context to be released.
2498
2499 **/
2500 VOID
2501 EFIAPI
2502 DhFree (
2503 IN VOID *DhContext
2504 );
2505
2506 /**
2507 Generates DH parameter.
2508
2509 Given generator g, and length of prime number p in bits, this function generates p,
2510 and sets DH context according to value of g and p.
2511
2512 Before this function can be invoked, pseudorandom number generator must be correctly
2513 initialized by RandomSeed().
2514
2515 If DhContext is NULL, then return FALSE.
2516 If Prime is NULL, then return FALSE.
2517 If this interface is not supported, then return FALSE.
2518
2519 @param[in, out] DhContext Pointer to the DH context.
2520 @param[in] Generator Value of generator.
2521 @param[in] PrimeLength Length in bits of prime to be generated.
2522 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2523
2524 @retval TRUE DH parameter generation succeeded.
2525 @retval FALSE Value of Generator is not supported.
2526 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2527 @retval FALSE This interface is not supported.
2528
2529 **/
2530 BOOLEAN
2531 EFIAPI
2532 DhGenerateParameter (
2533 IN OUT VOID *DhContext,
2534 IN UINTN Generator,
2535 IN UINTN PrimeLength,
2536 OUT UINT8 *Prime
2537 );
2538
2539 /**
2540 Sets generator and prime parameters for DH.
2541
2542 Given generator g, and prime number p, this function and sets DH
2543 context accordingly.
2544
2545 If DhContext is NULL, then return FALSE.
2546 If Prime is NULL, then return FALSE.
2547 If this interface is not supported, then return FALSE.
2548
2549 @param[in, out] DhContext Pointer to the DH context.
2550 @param[in] Generator Value of generator.
2551 @param[in] PrimeLength Length in bits of prime to be generated.
2552 @param[in] Prime Pointer to the prime number.
2553
2554 @retval TRUE DH parameter setting succeeded.
2555 @retval FALSE Value of Generator is not supported.
2556 @retval FALSE Value of Generator is not suitable for the Prime.
2557 @retval FALSE Value of Prime is not a prime number.
2558 @retval FALSE Value of Prime is not a safe prime number.
2559 @retval FALSE This interface is not supported.
2560
2561 **/
2562 BOOLEAN
2563 EFIAPI
2564 DhSetParameter (
2565 IN OUT VOID *DhContext,
2566 IN UINTN Generator,
2567 IN UINTN PrimeLength,
2568 IN CONST UINT8 *Prime
2569 );
2570
2571 /**
2572 Generates DH public key.
2573
2574 This function generates random secret exponent, and computes the public key, which is
2575 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2576 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2577 PublicKeySize is set to the required buffer size to obtain the public key.
2578
2579 If DhContext is NULL, then return FALSE.
2580 If PublicKeySize is NULL, then return FALSE.
2581 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2582 If this interface is not supported, then return FALSE.
2583
2584 @param[in, out] DhContext Pointer to the DH context.
2585 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2586 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2587 On output, the size of data returned in PublicKey buffer in bytes.
2588
2589 @retval TRUE DH public key generation succeeded.
2590 @retval FALSE DH public key generation failed.
2591 @retval FALSE PublicKeySize is not large enough.
2592 @retval FALSE This interface is not supported.
2593
2594 **/
2595 BOOLEAN
2596 EFIAPI
2597 DhGenerateKey (
2598 IN OUT VOID *DhContext,
2599 OUT UINT8 *PublicKey,
2600 IN OUT UINTN *PublicKeySize
2601 );
2602
2603 /**
2604 Computes exchanged common key.
2605
2606 Given peer's public key, this function computes the exchanged common key, based on its own
2607 context including value of prime modulus and random secret exponent.
2608
2609 If DhContext is NULL, then return FALSE.
2610 If PeerPublicKey is NULL, then return FALSE.
2611 If KeySize is NULL, then return FALSE.
2612 If Key is NULL, then return FALSE.
2613 If KeySize is not large enough, then return FALSE.
2614 If this interface is not supported, then return FALSE.
2615
2616 @param[in, out] DhContext Pointer to the DH context.
2617 @param[in] PeerPublicKey Pointer to the peer's public key.
2618 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2619 @param[out] Key Pointer to the buffer to receive generated key.
2620 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2621 On output, the size of data returned in Key buffer in bytes.
2622
2623 @retval TRUE DH exchanged key generation succeeded.
2624 @retval FALSE DH exchanged key generation failed.
2625 @retval FALSE KeySize is not large enough.
2626 @retval FALSE This interface is not supported.
2627
2628 **/
2629 BOOLEAN
2630 EFIAPI
2631 DhComputeKey (
2632 IN OUT VOID *DhContext,
2633 IN CONST UINT8 *PeerPublicKey,
2634 IN UINTN PeerPublicKeySize,
2635 OUT UINT8 *Key,
2636 IN OUT UINTN *KeySize
2637 );
2638
2639 // =====================================================================================
2640 // Pseudo-Random Generation Primitive
2641 // =====================================================================================
2642
2643 /**
2644 Sets up the seed value for the pseudorandom number generator.
2645
2646 This function sets up the seed value for the pseudorandom number generator.
2647 If Seed is not NULL, then the seed passed in is used.
2648 If Seed is NULL, then default seed is used.
2649 If this interface is not supported, then return FALSE.
2650
2651 @param[in] Seed Pointer to seed value.
2652 If NULL, default seed is used.
2653 @param[in] SeedSize Size of seed value.
2654 If Seed is NULL, this parameter is ignored.
2655
2656 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2657 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2658 @retval FALSE This interface is not supported.
2659
2660 **/
2661 BOOLEAN
2662 EFIAPI
2663 RandomSeed (
2664 IN CONST UINT8 *Seed OPTIONAL,
2665 IN UINTN SeedSize
2666 );
2667
2668 /**
2669 Generates a pseudorandom byte stream of the specified size.
2670
2671 If Output is NULL, then return FALSE.
2672 If this interface is not supported, then return FALSE.
2673
2674 @param[out] Output Pointer to buffer to receive random value.
2675 @param[in] Size Size of random bytes to generate.
2676
2677 @retval TRUE Pseudorandom byte stream generated successfully.
2678 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2679 @retval FALSE This interface is not supported.
2680
2681 **/
2682 BOOLEAN
2683 EFIAPI
2684 RandomBytes (
2685 OUT UINT8 *Output,
2686 IN UINTN Size
2687 );
2688
2689 // =====================================================================================
2690 // Key Derivation Function Primitive
2691 // =====================================================================================
2692
2693 /**
2694 Derive key data using HMAC-SHA256 based KDF.
2695
2696 @param[in] Key Pointer to the user-supplied key.
2697 @param[in] KeySize Key size in bytes.
2698 @param[in] Salt Pointer to the salt(non-secret) value.
2699 @param[in] SaltSize Salt size in bytes.
2700 @param[in] Info Pointer to the application specific info.
2701 @param[in] InfoSize Info size in bytes.
2702 @param[out] Out Pointer to buffer to receive hkdf value.
2703 @param[in] OutSize Size of hkdf bytes to generate.
2704
2705 @retval TRUE Hkdf generated successfully.
2706 @retval FALSE Hkdf generation failed.
2707
2708 **/
2709 BOOLEAN
2710 EFIAPI
2711 HkdfSha256ExtractAndExpand (
2712 IN CONST UINT8 *Key,
2713 IN UINTN KeySize,
2714 IN CONST UINT8 *Salt,
2715 IN UINTN SaltSize,
2716 IN CONST UINT8 *Info,
2717 IN UINTN InfoSize,
2718 OUT UINT8 *Out,
2719 IN UINTN OutSize
2720 );
2721
2722 /**
2723 Derive SHA256 HMAC-based Extract key Derivation Function (HKDF).
2724
2725 @param[in] Key Pointer to the user-supplied key.
2726 @param[in] KeySize key size in bytes.
2727 @param[in] Salt Pointer to the salt(non-secret) value.
2728 @param[in] SaltSize salt size in bytes.
2729 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2730 @param[in] PrkOutSize size of hkdf bytes to generate.
2731
2732 @retval true Hkdf generated successfully.
2733 @retval false Hkdf generation failed.
2734
2735 **/
2736 BOOLEAN
2737 EFIAPI
2738 HkdfSha256Extract (
2739 IN CONST UINT8 *Key,
2740 IN UINTN KeySize,
2741 IN CONST UINT8 *Salt,
2742 IN UINTN SaltSize,
2743 OUT UINT8 *PrkOut,
2744 UINTN PrkOutSize
2745 );
2746
2747 /**
2748 Derive SHA256 HMAC-based Expand Key Derivation Function (HKDF).
2749
2750 @param[in] Prk Pointer to the user-supplied key.
2751 @param[in] PrkSize Key size in bytes.
2752 @param[in] Info Pointer to the application specific info.
2753 @param[in] InfoSize Info size in bytes.
2754 @param[out] Out Pointer to buffer to receive hkdf value.
2755 @param[in] OutSize Size of hkdf bytes to generate.
2756
2757 @retval TRUE Hkdf generated successfully.
2758 @retval FALSE Hkdf generation failed.
2759
2760 **/
2761 BOOLEAN
2762 EFIAPI
2763 HkdfSha256Expand (
2764 IN CONST UINT8 *Prk,
2765 IN UINTN PrkSize,
2766 IN CONST UINT8 *Info,
2767 IN UINTN InfoSize,
2768 OUT UINT8 *Out,
2769 IN UINTN OutSize
2770 );
2771
2772 /**
2773 Derive SHA384 HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
2774
2775 @param[in] Key Pointer to the user-supplied key.
2776 @param[in] KeySize Key size in bytes.
2777 @param[in] Salt Pointer to the salt(non-secret) value.
2778 @param[in] SaltSize Salt size in bytes.
2779 @param[in] Info Pointer to the application specific info.
2780 @param[in] InfoSize Info size in bytes.
2781 @param[out] Out Pointer to buffer to receive hkdf value.
2782 @param[in] OutSize Size of hkdf bytes to generate.
2783
2784 @retval TRUE Hkdf generated successfully.
2785 @retval FALSE Hkdf generation failed.
2786
2787 **/
2788 BOOLEAN
2789 EFIAPI
2790 HkdfSha384ExtractAndExpand (
2791 IN CONST UINT8 *Key,
2792 IN UINTN KeySize,
2793 IN CONST UINT8 *Salt,
2794 IN UINTN SaltSize,
2795 IN CONST UINT8 *Info,
2796 IN UINTN InfoSize,
2797 OUT UINT8 *Out,
2798 IN UINTN OutSize
2799 );
2800
2801 /**
2802 Derive SHA384 HMAC-based Extract key Derivation Function (HKDF).
2803
2804 @param[in] Key Pointer to the user-supplied key.
2805 @param[in] KeySize key size in bytes.
2806 @param[in] Salt Pointer to the salt(non-secret) value.
2807 @param[in] SaltSize salt size in bytes.
2808 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2809 @param[in] PrkOutSize size of hkdf bytes to generate.
2810
2811 @retval true Hkdf generated successfully.
2812 @retval false Hkdf generation failed.
2813
2814 **/
2815 BOOLEAN
2816 EFIAPI
2817 HkdfSha384Extract (
2818 IN CONST UINT8 *Key,
2819 IN UINTN KeySize,
2820 IN CONST UINT8 *Salt,
2821 IN UINTN SaltSize,
2822 OUT UINT8 *PrkOut,
2823 UINTN PrkOutSize
2824 );
2825
2826 /**
2827 Derive SHA384 HMAC-based Expand Key Derivation Function (HKDF).
2828
2829 @param[in] Prk Pointer to the user-supplied key.
2830 @param[in] PrkSize Key size in bytes.
2831 @param[in] Info Pointer to the application specific info.
2832 @param[in] InfoSize Info size in bytes.
2833 @param[out] Out Pointer to buffer to receive hkdf value.
2834 @param[in] OutSize Size of hkdf bytes to generate.
2835
2836 @retval TRUE Hkdf generated successfully.
2837 @retval FALSE Hkdf generation failed.
2838
2839 **/
2840 BOOLEAN
2841 EFIAPI
2842 HkdfSha384Expand (
2843 IN CONST UINT8 *Prk,
2844 IN UINTN PrkSize,
2845 IN CONST UINT8 *Info,
2846 IN UINTN InfoSize,
2847 OUT UINT8 *Out,
2848 IN UINTN OutSize
2849 );
2850
2851 // =====================================================================================
2852 // Big number primitives
2853 // =====================================================================================
2854
2855 /**
2856 Allocate new Big Number.
2857
2858 @retval New BigNum opaque structure or NULL on failure.
2859 **/
2860 VOID *
2861 EFIAPI
2862 BigNumInit (
2863 VOID
2864 );
2865
2866 /**
2867 Allocate new Big Number and assign the provided value to it.
2868
2869 @param[in] Buf Big endian encoded buffer.
2870 @param[in] Len Buffer length.
2871
2872 @retval New BigNum opaque structure or NULL on failure.
2873 **/
2874 VOID *
2875 EFIAPI
2876 BigNumFromBin (
2877 IN CONST UINT8 *Buf,
2878 IN UINTN Len
2879 );
2880
2881 /**
2882 Convert the absolute value of Bn into big-endian form and store it at Buf.
2883 The Buf array should have at least BigNumBytes() in it.
2884
2885 @param[in] Bn Big number to convert.
2886 @param[out] Buf Output buffer.
2887
2888 @retval The length of the big-endian number placed at Buf or -1 on error.
2889 **/
2890 INTN
2891 EFIAPI
2892 BigNumToBin (
2893 IN CONST VOID *Bn,
2894 OUT UINT8 *Buf
2895 );
2896
2897 /**
2898 Free the Big Number.
2899
2900 @param[in] Bn Big number to free.
2901 @param[in] Clear TRUE if the buffer should be cleared.
2902 **/
2903 VOID
2904 EFIAPI
2905 BigNumFree (
2906 IN VOID *Bn,
2907 IN BOOLEAN Clear
2908 );
2909
2910 /**
2911 Calculate the sum of two Big Numbers.
2912 Please note, all "out" Big number arguments should be properly initialized
2913 by calling to BigNumInit() or BigNumFromBin() functions.
2914
2915 @param[in] BnA Big number.
2916 @param[in] BnB Big number.
2917 @param[out] BnRes The result of BnA + BnB.
2918
2919 @retval TRUE On success.
2920 @retval FALSE Otherwise.
2921 **/
2922 BOOLEAN
2923 EFIAPI
2924 BigNumAdd (
2925 IN CONST VOID *BnA,
2926 IN CONST VOID *BnB,
2927 OUT VOID *BnRes
2928 );
2929
2930 /**
2931 Subtract two Big Numbers.
2932 Please note, all "out" Big number arguments should be properly initialized
2933 by calling to BigNumInit() or BigNumFromBin() functions.
2934
2935 @param[in] BnA Big number.
2936 @param[in] BnB Big number.
2937 @param[out] BnRes The result of BnA - BnB.
2938
2939 @retval TRUE On success.
2940 @retval FALSE Otherwise.
2941 **/
2942 BOOLEAN
2943 EFIAPI
2944 BigNumSub (
2945 IN CONST VOID *BnA,
2946 IN CONST VOID *BnB,
2947 OUT VOID *BnRes
2948 );
2949
2950 /**
2951 Calculate remainder: BnRes = BnA % BnB.
2952 Please note, all "out" Big number arguments should be properly initialized
2953 by calling to BigNumInit() or BigNumFromBin() functions.
2954
2955 @param[in] BnA Big number.
2956 @param[in] BnB Big number.
2957 @param[out] BnRes The result of BnA % BnB.
2958
2959 @retval TRUE On success.
2960 @retval FALSE Otherwise.
2961 **/
2962 BOOLEAN
2963 EFIAPI
2964 BigNumMod (
2965 IN CONST VOID *BnA,
2966 IN CONST VOID *BnB,
2967 OUT VOID *BnRes
2968 );
2969
2970 /**
2971 Compute BnA to the BnP-th power modulo BnM.
2972 Please note, all "out" Big number arguments should be properly initialized
2973 by calling to BigNumInit() or BigNumFromBin() functions.
2974
2975 @param[in] BnA Big number.
2976 @param[in] BnP Big number (power).
2977 @param[in] BnM Big number (modulo).
2978 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
2979
2980 @retval TRUE On success.
2981 @retval FALSE Otherwise.
2982 **/
2983 BOOLEAN
2984 EFIAPI
2985 BigNumExpMod (
2986 IN CONST VOID *BnA,
2987 IN CONST VOID *BnP,
2988 IN CONST VOID *BnM,
2989 OUT VOID *BnRes
2990 );
2991
2992 /**
2993 Compute BnA inverse modulo BnM.
2994 Please note, all "out" Big number arguments should be properly initialized
2995 by calling to BigNumInit() or BigNumFromBin() functions.
2996
2997 @param[in] BnA Big number.
2998 @param[in] BnM Big number (modulo).
2999 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
3000
3001 @retval TRUE On success.
3002 @retval FALSE Otherwise.
3003 **/
3004 BOOLEAN
3005 EFIAPI
3006 BigNumInverseMod (
3007 IN CONST VOID *BnA,
3008 IN CONST VOID *BnM,
3009 OUT VOID *BnRes
3010 );
3011
3012 /**
3013 Divide two Big Numbers.
3014 Please note, all "out" Big number arguments should be properly initialized
3015 by calling to BigNumInit() or BigNumFromBin() functions.
3016
3017 @param[in] BnA Big number.
3018 @param[in] BnB Big number.
3019 @param[out] BnRes The result, such that BnA / BnB.
3020
3021 @retval TRUE On success.
3022 @retval FALSE Otherwise.
3023 **/
3024 BOOLEAN
3025 EFIAPI
3026 BigNumDiv (
3027 IN CONST VOID *BnA,
3028 IN CONST VOID *BnB,
3029 OUT VOID *BnRes
3030 );
3031
3032 /**
3033 Multiply two Big Numbers modulo BnM.
3034 Please note, all "out" Big number arguments should be properly initialized
3035 by calling to BigNumInit() or BigNumFromBin() functions.
3036
3037 @param[in] BnA Big number.
3038 @param[in] BnB Big number.
3039 @param[in] BnM Big number (modulo).
3040 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
3041
3042 @retval TRUE On success.
3043 @retval FALSE Otherwise.
3044 **/
3045 BOOLEAN
3046 EFIAPI
3047 BigNumMulMod (
3048 IN CONST VOID *BnA,
3049 IN CONST VOID *BnB,
3050 IN CONST VOID *BnM,
3051 OUT VOID *BnRes
3052 );
3053
3054 /**
3055 Compare two Big Numbers.
3056
3057 @param[in] BnA Big number.
3058 @param[in] BnB Big number.
3059
3060 @retval 0 BnA == BnB.
3061 @retval 1 BnA > BnB.
3062 @retval -1 BnA < BnB.
3063 **/
3064 INTN
3065 EFIAPI
3066 BigNumCmp (
3067 IN CONST VOID *BnA,
3068 IN CONST VOID *BnB
3069 );
3070
3071 /**
3072 Get number of bits in Bn.
3073
3074 @param[in] Bn Big number.
3075
3076 @retval Number of bits.
3077 **/
3078
3079 UINTN
3080 EFIAPI
3081 BigNumBits (
3082 IN CONST VOID *Bn
3083 );
3084
3085 /**
3086 Get number of bytes in Bn.
3087
3088 @param[in] Bn Big number.
3089
3090 @retval Number of bytes.
3091 **/
3092 UINTN
3093 EFIAPI
3094 BigNumBytes (
3095 IN CONST VOID *Bn
3096 );
3097
3098 /**
3099 Checks if Big Number equals to the given Num.
3100
3101 @param[in] Bn Big number.
3102 @param[in] Num Number.
3103
3104 @retval TRUE iff Bn == Num.
3105 @retval FALSE otherwise.
3106 **/
3107 BOOLEAN
3108 EFIAPI
3109 BigNumIsWord (
3110 IN CONST VOID *Bn,
3111 IN UINTN Num
3112 );
3113
3114 /**
3115 Checks if Big Number is odd.
3116
3117 @param[in] Bn Big number.
3118
3119 @retval TRUE Bn is odd (Bn % 2 == 1).
3120 @retval FALSE otherwise.
3121 **/
3122 BOOLEAN
3123 EFIAPI
3124 BigNumIsOdd (
3125 IN CONST VOID *Bn
3126 );
3127
3128 /**
3129 Copy Big number.
3130
3131 @param[out] BnDst Destination.
3132 @param[in] BnSrc Source.
3133
3134 @retval BnDst on success.
3135 @retval NULL otherwise.
3136 **/
3137 VOID *
3138 EFIAPI
3139 BigNumCopy (
3140 OUT VOID *BnDst,
3141 IN CONST VOID *BnSrc
3142 );
3143
3144 /**
3145 Get constant Big number with value of "1".
3146 This may be used to save expensive allocations.
3147
3148 @retval Big Number with value of 1.
3149 **/
3150 CONST VOID *
3151 EFIAPI
3152 BigNumValueOne (
3153 VOID
3154 );
3155
3156 /**
3157 Shift right Big Number.
3158 Please note, all "out" Big number arguments should be properly initialized
3159 by calling to BigNumInit() or BigNumFromBin() functions.
3160
3161 @param[in] Bn Big number.
3162 @param[in] N Number of bits to shift.
3163 @param[out] BnRes The result.
3164
3165 @retval TRUE On success.
3166 @retval FALSE Otherwise.
3167 **/
3168 BOOLEAN
3169 EFIAPI
3170 BigNumRShift (
3171 IN CONST VOID *Bn,
3172 IN UINTN N,
3173 OUT VOID *BnRes
3174 );
3175
3176 /**
3177 Mark Big Number for constant time computations.
3178 This function should be called before any constant time computations are
3179 performed on the given Big number.
3180
3181 @param[in] Bn Big number.
3182 **/
3183 VOID
3184 EFIAPI
3185 BigNumConstTime (
3186 IN VOID *Bn
3187 );
3188
3189 /**
3190 Calculate square modulo.
3191 Please note, all "out" Big number arguments should be properly initialized
3192 by calling to BigNumInit() or BigNumFromBin() functions.
3193
3194 @param[in] BnA Big number.
3195 @param[in] BnM Big number (modulo).
3196 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
3197
3198 @retval TRUE On success.
3199 @retval FALSE Otherwise.
3200 **/
3201 BOOLEAN
3202 EFIAPI
3203 BigNumSqrMod (
3204 IN CONST VOID *BnA,
3205 IN CONST VOID *BnM,
3206 OUT VOID *BnRes
3207 );
3208
3209 /**
3210 Create new Big Number computation context. This is an opaque structure
3211 which should be passed to any function that requires it. The BN context is
3212 needed to optimize calculations and expensive allocations.
3213
3214 @retval Big Number context struct or NULL on failure.
3215 **/
3216 VOID *
3217 EFIAPI
3218 BigNumNewContext (
3219 VOID
3220 );
3221
3222 /**
3223 Free Big Number context that was allocated with BigNumNewContext().
3224
3225 @param[in] BnCtx Big number context to free.
3226 **/
3227 VOID
3228 EFIAPI
3229 BigNumContextFree (
3230 IN VOID *BnCtx
3231 );
3232
3233 /**
3234 Set Big Number to a given value.
3235
3236 @param[in] Bn Big number to set.
3237 @param[in] Val Value to set.
3238
3239 @retval TRUE On success.
3240 @retval FALSE Otherwise.
3241 **/
3242 BOOLEAN
3243 EFIAPI
3244 BigNumSetUint (
3245 IN VOID *Bn,
3246 IN UINTN Val
3247 );
3248
3249 /**
3250 Add two Big Numbers modulo BnM.
3251
3252 @param[in] BnA Big number.
3253 @param[in] BnB Big number.
3254 @param[in] BnM Big number (modulo).
3255 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
3256
3257 @retval TRUE On success.
3258 @retval FALSE Otherwise.
3259 **/
3260 BOOLEAN
3261 EFIAPI
3262 BigNumAddMod (
3263 IN CONST VOID *BnA,
3264 IN CONST VOID *BnB,
3265 IN CONST VOID *BnM,
3266 OUT VOID *BnRes
3267 );
3268
3269 // =====================================================================================
3270 // Basic Elliptic Curve Primitives
3271 // =====================================================================================
3272
3273 /**
3274 Initialize new opaque EcGroup object. This object represents an EC curve and
3275 and is used for calculation within this group. This object should be freed
3276 using EcGroupFree() function.
3277
3278 @param[in] CryptoNid Identifying number for the ECC curve (Defined in
3279 BaseCryptLib.h).
3280
3281 @retval EcGroup object On success.
3282 @retval NULL On failure.
3283 **/
3284 VOID *
3285 EFIAPI
3286 EcGroupInit (
3287 IN UINTN CryptoNid
3288 );
3289
3290 /**
3291 Get EC curve parameters. While elliptic curve equation is Y^2 mod P = (X^3 + AX + B) Mod P.
3292 This function will set the provided Big Number objects to the corresponding
3293 values. The caller needs to make sure all the "out" BigNumber parameters
3294 are properly initialized.
3295
3296 @param[in] EcGroup EC group object.
3297 @param[out] BnPrime Group prime number.
3298 @param[out] BnA A coefficient.
3299 @param[out] BnB B coefficient.
3300 @param[in] BnCtx BN context.
3301
3302 @retval TRUE On success.
3303 @retval FALSE Otherwise.
3304 **/
3305 BOOLEAN
3306 EFIAPI
3307 EcGroupGetCurve (
3308 IN CONST VOID *EcGroup,
3309 OUT VOID *BnPrime,
3310 OUT VOID *BnA,
3311 OUT VOID *BnB,
3312 IN VOID *BnCtx
3313 );
3314
3315 /**
3316 Get EC group order.
3317 This function will set the provided Big Number object to the corresponding
3318 value. The caller needs to make sure that the "out" BigNumber parameter
3319 is properly initialized.
3320
3321 @param[in] EcGroup EC group object.
3322 @param[out] BnOrder Group prime number.
3323
3324 @retval TRUE On success.
3325 @retval FALSE Otherwise.
3326 **/
3327 BOOLEAN
3328 EFIAPI
3329 EcGroupGetOrder (
3330 IN VOID *EcGroup,
3331 OUT VOID *BnOrder
3332 );
3333
3334 /**
3335 Free previously allocated EC group object using EcGroupInit().
3336
3337 @param[in] EcGroup EC group object to free.
3338 **/
3339 VOID
3340 EFIAPI
3341 EcGroupFree (
3342 IN VOID *EcGroup
3343 );
3344
3345 /**
3346 Initialize new opaque EC Point object. This object represents an EC point
3347 within the given EC group (curve).
3348
3349 @param[in] EC Group, properly initialized using EcGroupInit().
3350
3351 @retval EC Point object On success.
3352 @retval NULL On failure.
3353 **/
3354 VOID *
3355 EFIAPI
3356 EcPointInit (
3357 IN CONST VOID *EcGroup
3358 );
3359
3360 /**
3361 Free previously allocated EC Point object using EcPointInit().
3362
3363 @param[in] EcPoint EC Point to free.
3364 @param[in] Clear TRUE iff the memory should be cleared.
3365 **/
3366 VOID
3367 EFIAPI
3368 EcPointDeInit (
3369 IN VOID *EcPoint,
3370 IN BOOLEAN Clear
3371 );
3372
3373 /**
3374 Get EC point affine (x,y) coordinates.
3375 This function will set the provided Big Number objects to the corresponding
3376 values. The caller needs to make sure all the "out" BigNumber parameters
3377 are properly initialized.
3378
3379 @param[in] EcGroup EC group object.
3380 @param[in] EcPoint EC point object.
3381 @param[out] BnX X coordinate.
3382 @param[out] BnY Y coordinate.
3383 @param[in] BnCtx BN context, created with BigNumNewContext().
3384
3385 @retval TRUE On success.
3386 @retval FALSE Otherwise.
3387 **/
3388 BOOLEAN
3389 EFIAPI
3390 EcPointGetAffineCoordinates (
3391 IN CONST VOID *EcGroup,
3392 IN CONST VOID *EcPoint,
3393 OUT VOID *BnX,
3394 OUT VOID *BnY,
3395 IN VOID *BnCtx
3396 );
3397
3398 /**
3399 Set EC point affine (x,y) coordinates.
3400
3401 @param[in] EcGroup EC group object.
3402 @param[in] EcPoint EC point object.
3403 @param[in] BnX X coordinate.
3404 @param[in] BnY Y coordinate.
3405 @param[in] BnCtx BN context, created with BigNumNewContext().
3406
3407 @retval TRUE On success.
3408 @retval FALSE Otherwise.
3409 **/
3410 BOOLEAN
3411 EFIAPI
3412 EcPointSetAffineCoordinates (
3413 IN CONST VOID *EcGroup,
3414 IN VOID *EcPoint,
3415 IN CONST VOID *BnX,
3416 IN CONST VOID *BnY,
3417 IN VOID *BnCtx
3418 );
3419
3420 /**
3421 EC Point addition. EcPointResult = EcPointA + EcPointB.
3422
3423 @param[in] EcGroup EC group object.
3424 @param[out] EcPointResult EC point to hold the result. The point should
3425 be properly initialized.
3426 @param[in] EcPointA EC Point.
3427 @param[in] EcPointB EC Point.
3428 @param[in] BnCtx BN context, created with BigNumNewContext().
3429
3430 @retval TRUE On success.
3431 @retval FALSE Otherwise.
3432 **/
3433 BOOLEAN
3434 EFIAPI
3435 EcPointAdd (
3436 IN CONST VOID *EcGroup,
3437 OUT VOID *EcPointResult,
3438 IN CONST VOID *EcPointA,
3439 IN CONST VOID *EcPointB,
3440 IN VOID *BnCtx
3441 );
3442
3443 /**
3444 Variable EC point multiplication. EcPointResult = EcPoint * BnPScalar.
3445
3446 @param[in] EcGroup EC group object.
3447 @param[out] EcPointResult EC point to hold the result. The point should
3448 be properly initialized.
3449 @param[in] EcPoint EC Point.
3450 @param[in] BnPScalar P Scalar.
3451 @param[in] BnCtx BN context, created with BigNumNewContext().
3452
3453 @retval TRUE On success.
3454 @retval FALSE Otherwise.
3455 **/
3456 BOOLEAN
3457 EFIAPI
3458 EcPointMul (
3459 IN CONST VOID *EcGroup,
3460 OUT VOID *EcPointResult,
3461 IN CONST VOID *EcPoint,
3462 IN CONST VOID *BnPScalar,
3463 IN VOID *BnCtx
3464 );
3465
3466 /**
3467 Calculate the inverse of the supplied EC point.
3468
3469 @param[in] EcGroup EC group object.
3470 @param[in,out] EcPoint EC point to invert.
3471 @param[in] BnCtx BN context, created with BigNumNewContext().
3472
3473 @retval TRUE On success.
3474 @retval FALSE Otherwise.
3475 **/
3476 BOOLEAN
3477 EFIAPI
3478 EcPointInvert (
3479 IN CONST VOID *EcGroup,
3480 IN OUT VOID *EcPoint,
3481 IN VOID *BnCtx
3482 );
3483
3484 /**
3485 Check if the supplied point is on EC curve.
3486
3487 @param[in] EcGroup EC group object.
3488 @param[in] EcPoint EC point to check.
3489 @param[in] BnCtx BN context, created with BigNumNewContext().
3490
3491 @retval TRUE On curve.
3492 @retval FALSE Otherwise.
3493 **/
3494 BOOLEAN
3495 EFIAPI
3496 EcPointIsOnCurve (
3497 IN CONST VOID *EcGroup,
3498 IN CONST VOID *EcPoint,
3499 IN VOID *BnCtx
3500 );
3501
3502 /**
3503 Check if the supplied point is at infinity.
3504
3505 @param[in] EcGroup EC group object.
3506 @param[in] EcPoint EC point to check.
3507
3508 @retval TRUE At infinity.
3509 @retval FALSE Otherwise.
3510 **/
3511 BOOLEAN
3512 EFIAPI
3513 EcPointIsAtInfinity (
3514 IN CONST VOID *EcGroup,
3515 IN CONST VOID *EcPoint
3516 );
3517
3518 /**
3519 Check if EC points are equal.
3520
3521 @param[in] EcGroup EC group object.
3522 @param[in] EcPointA EC point A.
3523 @param[in] EcPointB EC point B.
3524 @param[in] BnCtx BN context, created with BigNumNewContext().
3525
3526 @retval TRUE A == B.
3527 @retval FALSE Otherwise.
3528 **/
3529 BOOLEAN
3530 EFIAPI
3531 EcPointEqual (
3532 IN CONST VOID *EcGroup,
3533 IN CONST VOID *EcPointA,
3534 IN CONST VOID *EcPointB,
3535 IN VOID *BnCtx
3536 );
3537
3538 /**
3539 Set EC point compressed coordinates. Points can be described in terms of
3540 their compressed coordinates. For a point (x, y), for any given value for x
3541 such that the point is on the curve there will only ever be two possible
3542 values for y. Therefore, a point can be set using this function where BnX is
3543 the x coordinate and YBit is a value 0 or 1 to identify which of the two
3544 possible values for y should be used.
3545
3546 @param[in] EcGroup EC group object.
3547 @param[in] EcPoint EC Point.
3548 @param[in] BnX X coordinate.
3549 @param[in] YBit 0 or 1 to identify which Y value is used.
3550 @param[in] BnCtx BN context, created with BigNumNewContext().
3551
3552 @retval TRUE On success.
3553 @retval FALSE Otherwise.
3554 **/
3555 BOOLEAN
3556 EFIAPI
3557 EcPointSetCompressedCoordinates (
3558 IN CONST VOID *EcGroup,
3559 IN VOID *EcPoint,
3560 IN CONST VOID *BnX,
3561 IN UINT8 YBit,
3562 IN VOID *BnCtx
3563 );
3564
3565 // =====================================================================================
3566 // Elliptic Curve Diffie Hellman Primitives
3567 // =====================================================================================
3568
3569 /**
3570 Allocates and Initializes one Elliptic Curve Context for subsequent use
3571 with the NID.
3572
3573 @param[in] Nid cipher NID
3574 @return Pointer to the Elliptic Curve Context that has been initialized.
3575 If the allocations fails, EcNewByNid() returns NULL.
3576 **/
3577 VOID *
3578 EFIAPI
3579 EcNewByNid (
3580 IN UINTN Nid
3581 );
3582
3583 /**
3584 Release the specified EC context.
3585
3586 @param[in] EcContext Pointer to the EC context to be released.
3587 **/
3588 VOID
3589 EFIAPI
3590 EcFree (
3591 IN VOID *EcContext
3592 );
3593
3594 /**
3595 Generates EC key and returns EC public key (X, Y), Please note, this function uses
3596 pseudo random number generator. The caller must make sure RandomSeed()
3597 function was properly called before.
3598 The Ec context should be correctly initialized by EcNewByNid.
3599 This function generates random secret, and computes the public key (X, Y), which is
3600 returned via parameter Public, PublicSize.
3601 X is the first half of Public with size being PublicSize / 2,
3602 Y is the second half of Public with size being PublicSize / 2.
3603 EC context is updated accordingly.
3604 If the Public buffer is too small to hold the public X, Y, FALSE is returned and
3605 PublicSize is set to the required buffer size to obtain the public X, Y.
3606 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
3607 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
3608 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
3609 If EcContext is NULL, then return FALSE.
3610 If PublicSize is NULL, then return FALSE.
3611 If PublicSize is large enough but Public is NULL, then return FALSE.
3612 @param[in, out] EcContext Pointer to the EC context.
3613 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
3614 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
3615 On output, the size of data returned in Public buffer in bytes.
3616 @retval TRUE EC public X,Y generation succeeded.
3617 @retval FALSE EC public X,Y generation failed.
3618 @retval FALSE PublicKeySize is not large enough.
3619 **/
3620 BOOLEAN
3621 EFIAPI
3622 EcGenerateKey (
3623 IN OUT VOID *EcContext,
3624 OUT UINT8 *PublicKey,
3625 IN OUT UINTN *PublicKeySize
3626 );
3627
3628 /**
3629 Gets the public key component from the established EC context.
3630 The Ec context should be correctly initialized by EcNewByNid, and successfully
3631 generate key pair from EcGenerateKey().
3632 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
3633 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
3634 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
3635 @param[in, out] EcContext Pointer to EC context being set.
3636 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
3637 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
3638 On output, the size of data returned in Public buffer in bytes.
3639 @retval TRUE EC key component was retrieved successfully.
3640 @retval FALSE Invalid EC key component.
3641 **/
3642 BOOLEAN
3643 EFIAPI
3644 EcGetPubKey (
3645 IN OUT VOID *EcContext,
3646 OUT UINT8 *PublicKey,
3647 IN OUT UINTN *PublicKeySize
3648 );
3649
3650 /**
3651 Computes exchanged common key.
3652 Given peer's public key (X, Y), this function computes the exchanged common key,
3653 based on its own context including value of curve parameter and random secret.
3654 X is the first half of PeerPublic with size being PeerPublicSize / 2,
3655 Y is the second half of PeerPublic with size being PeerPublicSize / 2.
3656 If EcContext is NULL, then return FALSE.
3657 If PeerPublic is NULL, then return FALSE.
3658 If PeerPublicSize is 0, then return FALSE.
3659 If Key is NULL, then return FALSE.
3660 If KeySize is not large enough, then return FALSE.
3661 For P-256, the PeerPublicSize is 64. First 32-byte is X, Second 32-byte is Y.
3662 For P-384, the PeerPublicSize is 96. First 48-byte is X, Second 48-byte is Y.
3663 For P-521, the PeerPublicSize is 132. First 66-byte is X, Second 66-byte is Y.
3664 @param[in, out] EcContext Pointer to the EC context.
3665 @param[in] PeerPublic Pointer to the peer's public X,Y.
3666 @param[in] PeerPublicSize Size of peer's public X,Y in bytes.
3667 @param[in] CompressFlag Flag of PeerPublic is compressed or not.
3668 @param[out] Key Pointer to the buffer to receive generated key.
3669 @param[in, out] KeySize On input, the size of Key buffer in bytes.
3670 On output, the size of data returned in Key buffer in bytes.
3671 @retval TRUE EC exchanged key generation succeeded.
3672 @retval FALSE EC exchanged key generation failed.
3673 @retval FALSE KeySize is not large enough.
3674 **/
3675 BOOLEAN
3676 EFIAPI
3677 EcDhComputeKey (
3678 IN OUT VOID *EcContext,
3679 IN CONST UINT8 *PeerPublic,
3680 IN UINTN PeerPublicSize,
3681 IN CONST INT32 *CompressFlag,
3682 OUT UINT8 *Key,
3683 IN OUT UINTN *KeySize
3684 );
3685
3686 /**
3687 Retrieve the EC Private Key from the password-protected PEM key data.
3688
3689 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
3690 @param[in] PemSize Size of the PEM key data in bytes.
3691 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
3692 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
3693 EC private key component. Use EcFree() function to free the
3694 resource.
3695
3696 If PemData is NULL, then return FALSE.
3697 If EcContext is NULL, then return FALSE.
3698
3699 @retval TRUE EC Private Key was retrieved successfully.
3700 @retval FALSE Invalid PEM key data or incorrect password.
3701
3702 **/
3703 BOOLEAN
3704 EFIAPI
3705 EcGetPrivateKeyFromPem (
3706 IN CONST UINT8 *PemData,
3707 IN UINTN PemSize,
3708 IN CONST CHAR8 *Password,
3709 OUT VOID **EcContext
3710 );
3711
3712 /**
3713 Retrieve the EC Public Key from one DER-encoded X509 certificate.
3714
3715 @param[in] Cert Pointer to the DER-encoded X509 certificate.
3716 @param[in] CertSize Size of the X509 certificate in bytes.
3717 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
3718 EC public key component. Use EcFree() function to free the
3719 resource.
3720
3721 If Cert is NULL, then return FALSE.
3722 If EcContext is NULL, then return FALSE.
3723
3724 @retval TRUE EC Public Key was retrieved successfully.
3725 @retval FALSE Fail to retrieve EC public key from X509 certificate.
3726
3727 **/
3728 BOOLEAN
3729 EFIAPI
3730 EcGetPublicKeyFromX509 (
3731 IN CONST UINT8 *Cert,
3732 IN UINTN CertSize,
3733 OUT VOID **EcContext
3734 );
3735
3736 /**
3737 Carries out the EC-DSA signature.
3738
3739 This function carries out the EC-DSA signature.
3740 If the Signature buffer is too small to hold the contents of signature, FALSE
3741 is returned and SigSize is set to the required buffer size to obtain the signature.
3742
3743 If EcContext is NULL, then return FALSE.
3744 If MessageHash is NULL, then return FALSE.
3745 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
3746 If SigSize is large enough but Signature is NULL, then return FALSE.
3747
3748 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
3749 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
3750 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
3751
3752 @param[in] EcContext Pointer to EC context for signature generation.
3753 @param[in] HashNid hash NID
3754 @param[in] MessageHash Pointer to octet message hash to be signed.
3755 @param[in] HashSize Size of the message hash in bytes.
3756 @param[out] Signature Pointer to buffer to receive EC-DSA signature.
3757 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
3758 On output, the size of data returned in Signature buffer in bytes.
3759
3760 @retval TRUE Signature successfully generated in EC-DSA.
3761 @retval FALSE Signature generation failed.
3762 @retval FALSE SigSize is too small.
3763
3764 **/
3765 BOOLEAN
3766 EFIAPI
3767 EcDsaSign (
3768 IN VOID *EcContext,
3769 IN UINTN HashNid,
3770 IN CONST UINT8 *MessageHash,
3771 IN UINTN HashSize,
3772 OUT UINT8 *Signature,
3773 IN OUT UINTN *SigSize
3774 );
3775
3776 /**
3777 Verifies the EC-DSA signature.
3778
3779 If EcContext is NULL, then return FALSE.
3780 If MessageHash is NULL, then return FALSE.
3781 If Signature is NULL, then return FALSE.
3782 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
3783
3784 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
3785 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
3786 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
3787
3788 @param[in] EcContext Pointer to EC context for signature verification.
3789 @param[in] HashNid hash NID
3790 @param[in] MessageHash Pointer to octet message hash to be checked.
3791 @param[in] HashSize Size of the message hash in bytes.
3792 @param[in] Signature Pointer to EC-DSA signature to be verified.
3793 @param[in] SigSize Size of signature in bytes.
3794
3795 @retval TRUE Valid signature encoded in EC-DSA.
3796 @retval FALSE Invalid signature or invalid EC context.
3797
3798 **/
3799 BOOLEAN
3800 EFIAPI
3801 EcDsaVerify (
3802 IN VOID *EcContext,
3803 IN UINTN HashNid,
3804 IN CONST UINT8 *MessageHash,
3805 IN UINTN HashSize,
3806 IN CONST UINT8 *Signature,
3807 IN UINTN SigSize
3808 );
3809
3810 #endif // __BASE_CRYPT_LIB_H__