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