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