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