]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/IpSecDxe/IpSecCryptIo.h
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecCryptIo.h
CommitLineData
a3bcde70 1/** @file\r
9166f840 2 Definitions related to the Cryptographic Operations in IPsec.\r
a3bcde70 3\r
f75a7f56 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
a3bcde70 5\r
ecf98fbc 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a3bcde70
HT
7\r
8**/\r
a3bcde70
HT
9#ifndef _EFI_IPSEC_CRYPTIO_H_\r
10#define _EFI_IPSEC_CRYPTIO_H_\r
11\r
12#include <Protocol/IpSecConfig.h>\r
13#include <Library/DebugLib.h>\r
9166f840 14#include <Library/BaseCryptLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16#include <Library/MemoryAllocationLib.h>\r
17\r
18#include "IpSecImpl.h"\r
19#include "IkeCommon.h"\r
a3bcde70 20\r
9166f840 21#define IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE 4\r
a3bcde70 22#define IPSEC_AUTH_ALGORITHM_LIST_SIZE 3\r
9166f840 23#define IPSEC_HASH_ALGORITHM_LIST_SIZE 3\r
a3bcde70 24\r
68d3f2fb 25///\r
26/// Authentication Algorithm Definition\r
27/// The number value definition is aligned to IANA assignment\r
28///\r
29#define IKE_AALG_NONE 0x00\r
30#define IKE_AALG_SHA1HMAC 0x02\r
31#define IKE_AALG_NULL 0xFB\r
32\r
33///\r
34/// Encryption Algorithm Definition\r
35/// The number value definition is aligned to IANA assignment\r
36///\r
37#define IKE_EALG_NONE 0x00\r
38#define IKE_EALG_3DESCBC 0x03\r
39#define IKE_EALG_NULL 0x0B\r
40#define IKE_EALG_AESCBC 0x0C\r
41\r
a3bcde70 42/**\r
9166f840 43 Prototype of HMAC GetContextSize.\r
f75a7f56 44\r
a3bcde70 45 Retrieves the size, in bytes, of the context buffer required.\r
f75a7f56 46\r
a3bcde70
HT
47 @return The size, in bytes, of the context buffer required.\r
48\r
49**/\r
50typedef\r
51UINTN\r
2e7120cd 52(EFIAPI *CRYPTO_HMAC_GETCONTEXTSIZE)(\r
a3bcde70
HT
53 VOID\r
54 );\r
55\r
56/**\r
9166f840 57 Prototype of HMAC Operation Initiating.\r
f75a7f56 58\r
a3bcde70
HT
59 Initialization with a new context.\r
60\r
9166f840 61 @param[out] Context Input Context.\r
62 @param[in] Key Pointer to the key for HMAC.\r
63 @param[in] KeySize The length of the Key in bytes.\r
f75a7f56 64\r
a3bcde70
HT
65 @retval TRUE Initialization Successfully.\r
66\r
67**/\r
68typedef\r
9166f840 69BOOLEAN\r
2e7120cd 70(EFIAPI *CRYPTO_HMAC_INIT)(\r
9166f840 71 OUT VOID *Context,\r
72 IN CONST UINT8 *Key,\r
73 IN UINTN KeySize\r
a3bcde70
HT
74 );\r
75\r
76/**\r
9166f840 77 Prototype of HMAC update.\r
78 HMAC update operation. Continue an HMAC message digest operation, processing\r
79 another message block, and updating the HMAC context.\r
a3bcde70
HT
80\r
81 If Context is NULL, then ASSERT().\r
82 If Data is NULL, then ASSERT().\r
83\r
84 @param[in,out] Context The Specified Context.\r
9166f840 85 @param[in,out] Data The Input Data to be digested.\r
a3bcde70
HT
86 @param[in] DataLength The length, in bytes, of Data.\r
87\r
88 @retval TRUE Update data successfully.\r
89 @retval FALSE The Context has been finalized.\r
90\r
91**/\r
92typedef\r
93BOOLEAN\r
2e7120cd 94(EFIAPI *CRYPTO_HMAC_UPDATE)(\r
a3bcde70
HT
95 IN OUT VOID *Context,\r
96 IN CONST VOID *Data,\r
97 IN UINTN DataLength\r
98 );\r
99\r
100/**\r
d1c85a17 101 Prototype of HMAC finalization.\r
9166f840 102 Terminate a HMAC message digest operation and output the message digest.\r
a3bcde70
HT
103\r
104 If Context is NULL, then ASSERT().\r
105 If HashValue is NULL, then ASSERT().\r
106\r
107 @param[in,out] Context The specified Context.\r
9166f840 108 @param[out] HmacValue Pointer to a 16-byte message digest output buffer.\r
a3bcde70
HT
109\r
110 @retval TRUE Finalized successfully.\r
111\r
112**/\r
113typedef\r
114BOOLEAN\r
2e7120cd 115(EFIAPI *CRYPTO_HMAC_FINAL)(\r
a3bcde70 116 IN OUT VOID *Context,\r
9166f840 117 OUT UINT8 *HmacValue\r
a3bcde70
HT
118 );\r
119\r
120/**\r
9166f840 121 Prototype of Block Cipher GetContextSize.\r
a3bcde70
HT
122\r
123 Retrieves the size, in bytes, of the context buffer required.\r
124\r
125 @return The size, in bytes, of the context buffer required.\r
126\r
127**/\r
128typedef\r
129UINTN\r
2e7120cd 130(EFIAPI *CRYPTO_CIPHER_GETCONTEXTSIZE)(\r
a3bcde70
HT
131 VOID\r
132 );\r
133\r
134/**\r
9166f840 135 Prototype of Block Cipher initiation.\r
d1c85a17 136 Initializes the user-supplied key as the specified context (key materials) for both\r
a3bcde70
HT
137 encryption and decryption operations.\r
138\r
139 If Context is NULL, then ASSERT().\r
140 If Key is NULL, then generate random key for usage.\r
141\r
142 @param[in,out] Context The specified Context.\r
9166f840 143 @param[in] Key User-supplied cipher key.\r
a3bcde70
HT
144 @param[in] KeyBits Key length in bits.\r
145\r
9166f840 146 @retval TRUE Block Cipher Initialization was successful.\r
a3bcde70
HT
147\r
148**/\r
149typedef\r
150BOOLEAN\r
2e7120cd 151(EFIAPI *CRYPTO_CIPHER_INIT)(\r
a3bcde70
HT
152 IN OUT VOID *Context,\r
153 IN CONST UINT8 *Key,\r
9166f840 154 IN UINTN KeyBits\r
a3bcde70
HT
155 );\r
156\r
a3bcde70
HT
157/**\r
158 Prototype of Cipher encryption.\r
159 Encrypts plaintext message with the specified cipher.\r
160\r
161 If Context is NULL, then ASSERT().\r
d1c85a17 162 If InData is NULL, then ASSERT().\r
a3bcde70
HT
163 If Size of input data is not multiple of Cipher algorithm related block size,\r
164 then ASSERT().\r
165\r
166 @param[in] Context The specified Context.\r
167 @param[in] InData The input plaintext data to be encrypted.\r
9166f840 168 @param[in] InputSize The size of input data.\r
169 @param[in] Ivec Pointer to Initial Vector data for encryption.\r
a3bcde70 170 @param[out] OutData The resultant encrypted ciphertext.\r
a3bcde70
HT
171\r
172 @retval TRUE Encryption successful.\r
173\r
174**/\r
175typedef\r
176BOOLEAN\r
2e7120cd 177(EFIAPI *CRYPTO_CIPHER_ENCRYPT)(\r
a3bcde70
HT
178 IN VOID *Context,\r
179 IN CONST UINT8 *InData,\r
9166f840 180 IN UINTN InputSize,\r
181 IN CONST UINT8 *Ivec,\r
182 OUT UINT8 *OutData\r
a3bcde70
HT
183 );\r
184\r
a3bcde70
HT
185/**\r
186 Prototype of Cipher decryption.\r
187 Decrypts cipher message with specified cipher.\r
188\r
189 If Context is NULL, then ASSERT().\r
d1c85a17 190 If InData is NULL, then ASSERT().\r
a3bcde70
HT
191 If Size of input data is not a multiple of a certaion block size , then ASSERT().\r
192\r
193 @param[in] Context The specified Context.\r
194 @param[in] InData The input ciphertext data to be decrypted.\r
9166f840 195 @param[in] InputSize The InData size.\r
196 @param[in] Ivec Pointer to the Initial Vector data for decryption.\r
a3bcde70 197 @param[out] OutData The resultant decrypted plaintext.\r
a3bcde70
HT
198\r
199 @retval TRUE Decryption successful.\r
200\r
201**/\r
202typedef\r
203BOOLEAN\r
2e7120cd 204(EFIAPI *CRYPTO_CIPHER_DECRYPT)(\r
9166f840 205 IN VOID *Context,\r
a3bcde70 206 IN CONST UINT8 *InData,\r
9166f840 207 IN UINTN InputSize,\r
208 IN CONST UINT8 *Ivec,\r
209 OUT UINT8 *OutData\r
210 );\r
211\r
212/**\r
213 Prototype of Hash ContextSize.\r
214\r
215 Retrieves the size, in bytes, of the context buffer required for specified hash operations.\r
216\r
217 @return The size, in bytes, of the context buffer required for certain hash operations.\r
218\r
219**/\r
220typedef\r
221UINTN\r
2e7120cd 222(EFIAPI *CRYPTO_HASH_GETCONTEXTSIZE)(\r
9166f840 223 VOID\r
224 );\r
225\r
226/**\r
227 Prototype of Hash Initiate.\r
228\r
229 Initializes user-supplied memory pointed by Context as specified hash context for\r
230 subsequent use.\r
231\r
232 If Context is NULL, then ASSERT().\r
233\r
234 @param[out] Context Pointer to specified context being initialized.\r
235\r
236 @retval TRUE context initialization succeeded.\r
237 @retval FALSE context initialization failed.\r
238\r
239**/\r
240typedef\r
241BOOLEAN\r
2e7120cd 242(EFIAPI *CRYPTO_HASH_INIT)(\r
9166f840 243 OUT VOID *Context\r
244 );\r
245\r
246/**\r
247 Prototype of Hash Update\r
f75a7f56 248\r
9166f840 249 Digests the input data and updates hash context.\r
250\r
251 This function performs digest on a data buffer of the specified size.\r
252 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
d1c85a17 253 Context should be already correctly initialized by HashInit(), and should not be finalized\r
9166f840 254 by HashFinal(). Behavior with invalid context is undefined.\r
255\r
256 If Context is NULL, then ASSERT().\r
257\r
258 @param[in, out] Context Pointer to the specified context.\r
259 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
260 @param[in] DataSize Size of Data buffer in bytes.\r
261\r
262 @retval TRUE data digest succeeded.\r
263 @retval FALSE data digest failed.\r
264\r
265**/\r
266typedef\r
267BOOLEAN\r
2e7120cd 268(EFIAPI *CRYPTO_HASH_UPDATE)(\r
9166f840 269 IN OUT VOID *Context,\r
270 IN CONST VOID *Data,\r
271 IN UINTN DataSize\r
272 );\r
273\r
274/**\r
275 Prototype of Hash Finalization.\r
276\r
277 Completes computation of the digest value.\r
278\r
279 This function completes hash computation and retrieves the digest value into\r
280 the specified memory. After this function has been called, the context cannot\r
281 be used again.\r
d1c85a17 282 context should be already correctly initialized by HashInit(), and should not be\r
9166f840 283 finalized by HashFinal(). Behavior with invalid context is undefined.\r
284\r
285 If Context is NULL, then ASSERT().\r
286 If HashValue is NULL, then ASSERT().\r
287\r
288 @param[in, out] Context Pointer to the specified context.\r
289 @param[out] HashValue Pointer to a buffer that receives the digest\r
290 value.\r
291\r
292 @retval TRUE digest computation succeeded.\r
293 @retval FALSE digest computation failed.\r
294\r
295**/\r
296typedef\r
297BOOLEAN\r
2e7120cd 298(EFIAPI *CRYPTO_HASH_FINAL)(\r
9166f840 299 IN OUT VOID *Context,\r
300 OUT UINT8 *HashValue\r
a3bcde70
HT
301 );\r
302\r
303//\r
9166f840 304// The struct used to store the information and operation of Block Cipher algorithm.\r
a3bcde70
HT
305//\r
306typedef struct _ENCRYPT_ALGORITHM {\r
9166f840 307 //\r
308 // The ID of the Algorithm\r
309 //\r
310 UINT8 AlgorithmId;\r
311 //\r
312 // The Key length of the Algorithm\r
313 //\r
314 UINTN KeyLength;\r
315 //\r
316 // Iv Size of the Algorithm\r
317 //\r
318 UINTN IvLength;\r
319 //\r
320 // The Block Size of the Algorithm\r
321 //\r
322 UINTN BlockSize;\r
323 //\r
324 // The Function pointer of GetContextSize.\r
325 //\r
326 CRYPTO_CIPHER_GETCONTEXTSIZE CipherGetContextSize;\r
327 //\r
328 // The Function pointer of Cipher initiation.\r
329 //\r
330 CRYPTO_CIPHER_INIT CipherInitiate;\r
331 //\r
332 // The Function pointer of Cipher Encryption.\r
333 //\r
334 CRYPTO_CIPHER_ENCRYPT CipherEncrypt;\r
335 //\r
d1c85a17 336 // The Function pointer of Cipher Decryption.\r
9166f840 337 //\r
338 CRYPTO_CIPHER_DECRYPT CipherDecrypt;\r
a3bcde70
HT
339} ENCRYPT_ALGORITHM;\r
340\r
341//\r
d1c85a17 342// The struct used to store the information and operation of Authentication algorithm.\r
a3bcde70
HT
343//\r
344typedef struct _AUTH_ALGORITHM {\r
345 //\r
346 // ID of the Algorithm\r
347 //\r
348 UINT8 AlgorithmId;\r
349 //\r
350 // The Key length of the Algorithm\r
f75a7f56 351 //\r
9166f840 352 UINTN DigestLength;\r
a3bcde70
HT
353 //\r
354 // The ICV length of the Algorithm\r
355 //\r
356 UINTN IcvLength;\r
357 //\r
358 // The block size of the Algorithm\r
359 //\r
360 UINTN BlockSize;\r
361 //\r
362 // The function pointer of GetContextSize.\r
363 //\r
9166f840 364 CRYPTO_HMAC_GETCONTEXTSIZE HmacGetContextSize;\r
a3bcde70 365 //\r
9166f840 366 // The function pointer of Initiation\r
a3bcde70 367 //\r
9166f840 368 CRYPTO_HMAC_INIT HmacInitiate;\r
a3bcde70 369 //\r
9166f840 370 // The function pointer of HMAC Update.\r
a3bcde70 371 //\r
9166f840 372 CRYPTO_HMAC_UPDATE HmacUpdate;\r
a3bcde70 373 //\r
9166f840 374 // The fucntion pointer of HMAC Final\r
a3bcde70 375 //\r
9166f840 376 CRYPTO_HMAC_FINAL HmacFinal;\r
a3bcde70
HT
377} AUTH_ALGORITHM;\r
378\r
9166f840 379//\r
d1c85a17 380// The struct used to store the information and operation of Hash algorithm.\r
9166f840 381//\r
382typedef struct _HASH_ALGORITHM {\r
383 //\r
384 // ID of the Algorithm\r
385 //\r
386 UINT8 AlgorithmId;\r
387 //\r
388 // The Key length of the Algorithm\r
389 //\r
390 UINTN DigestLength;\r
391 //\r
392 // The ICV length of the Algorithm\r
393 //\r
394 UINTN IcvLength;\r
395 //\r
396 // The block size of the Algorithm\r
397 //\r
398 UINTN BlockSize;\r
399 //\r
400 // The function pointer of GetContextSize\r
401 //\r
402 CRYPTO_HASH_GETCONTEXTSIZE HashGetContextSize;\r
403 //\r
404 // The function pointer of Initiation\r
405 //\r
406 CRYPTO_HASH_INIT HashInitiate;\r
407 //\r
408 // The function pointer of Hash Update\r
409 //\r
410 CRYPTO_HASH_UPDATE HashUpdate;\r
411 //\r
412 // The fucntion pointer of Hash Final\r
413 //\r
414 CRYPTO_HASH_FINAL HashFinal;\r
415} HASH_ALGORITHM;\r
416\r
a3bcde70 417/**\r
d1c85a17 418 Get the IV size of specified encryption algorithm.\r
a3bcde70 419\r
9166f840 420 @param[in] AlgorithmId The encryption algorithm ID.\r
a3bcde70
HT
421\r
422 @return The value of IV size.\r
423\r
424**/\r
425UINTN\r
426IpSecGetEncryptIvLength (\r
427 IN UINT8 AlgorithmId\r
428 );\r
429\r
430/**\r
d1c85a17 431 Get the block size of specified encryption algorithm.\r
a3bcde70 432\r
9166f840 433 @param[in] AlgorithmId The encryption algorithm ID.\r
a3bcde70
HT
434\r
435 @return The value of block size.\r
436\r
437**/\r
438UINTN\r
439IpSecGetEncryptBlockSize (\r
440 IN UINT8 AlgorithmId\r
441 );\r
442\r
443/**\r
d1c85a17 444 Get the required key length of the specified encryption algorithm.\r
a3bcde70 445\r
9166f840 446 @param[in] AlgorithmId The encryption algorithm ID.\r
447\r
448 @return The value of key length.\r
449\r
450**/\r
451UINTN\r
452IpSecGetEncryptKeyLength (\r
453 IN UINT8 AlgorithmId\r
454 );\r
455\r
456/**\r
d1c85a17 457 Get the ICV size of the specified Authentication algorithm.\r
9166f840 458\r
459 @param[in] AlgorithmId The Authentication algorithm ID.\r
a3bcde70
HT
460\r
461 @return The value of ICV size.\r
462\r
463**/\r
464UINTN\r
465IpSecGetIcvLength (\r
9166f840 466 IN UINT8 AlgorithmId\r
467 );\r
468\r
469/**\r
470 Get the HMAC digest length by the specified Algorithm ID.\r
471\r
d1c85a17 472 @param[in] AlgorithmId The specified Algorithm ID.\r
9166f840 473\r
474 @return The digest length of the specified Authentication Algorithm ID.\r
475\r
476**/\r
477UINTN\r
478IpSecGetHmacDigestLength (\r
479 IN UINT8 AlgorithmId\r
a3bcde70
HT
480 );\r
481\r
482/**\r
483 Generate a random data for IV. If the IvSize is zero, not needed to create\r
484 IV and return EFI_SUCCESS.\r
485\r
486 @param[in] IvBuffer The pointer of the IV buffer.\r
9166f840 487 @param[in] IvSize The IV size in bytes.\r
a3bcde70
HT
488\r
489 @retval EFI_SUCCESS Create random data for IV.\r
490\r
491**/\r
492EFI_STATUS\r
493IpSecGenerateIv (\r
494 IN UINT8 *IvBuffer,\r
495 IN UINTN IvSize\r
496 );\r
497\r
9166f840 498/**\r
499 Encrypt the buffer.\r
500\r
501 This function calls relevant encryption interface from CryptoLib according to\r
d1c85a17 502 the input algorithm ID. The InData should be multiple of block size. This function\r
9166f840 503 doesn't perform the padding. If it has the Ivec data, the length of it should be\r
504 same with the block size. The block size is different from the different algorithm.\r
505\r
d1c85a17 506 @param[in] AlgorithmId The Algorithm identification defined in RFC.\r
9166f840 507 @param[in] Key Pointer to the buffer containing encrypting key.\r
76389e18 508 @param[in] KeyBits The length of the key in bits.\r
d1c85a17 509 @param[in] Ivec Point to the buffer containing the Initialization\r
9166f840 510 Vector (IV) data.\r
511 @param[in] InData Point to the buffer containing the data to be\r
512 encrypted.\r
513 @param[in] InDataLength The length of InData in Bytes.\r
514 @param[out] OutData Point to the buffer that receives the encryption\r
515 output.\r
516\r
517 @retval EFI_UNSUPPORTED The input Algorithm is not supported.\r
518 @retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.\r
519 @retval EFI_SUCCESS The operation completed successfully.\r
520\r
521**/\r
522EFI_STATUS\r
523IpSecCryptoIoEncrypt (\r
524 IN CONST UINT8 AlgorithmId,\r
525 IN CONST UINT8 *Key,\r
526 IN CONST UINTN KeyBits,\r
527 IN CONST UINT8 *Ivec, OPTIONAL\r
528 IN UINT8 *InData,\r
529 IN UINTN InDataLength,\r
530 OUT UINT8 *OutData\r
531 );\r
532\r
533/**\r
534 Decrypts the buffer.\r
535\r
536 This function calls relevant Decryption interface from CryptoLib according to\r
d1c85a17 537 the input algorithm ID. The InData should be multiple of block size. This function\r
9166f840 538 doesn't perform the padding. If it has the Ivec data, the length of it should be\r
539 same with the block size. The block size is different from the different algorithm.\r
540\r
d1c85a17 541 @param[in] AlgorithmId The Algorithm identification defined in RFC.\r
9166f840 542 @param[in] Key Pointer to the buffer containing encrypting key.\r
76389e18 543 @param[in] KeyBits The length of the key in bits.\r
d1c85a17 544 @param[in] Ivec Point to the buffer containing the Initialization\r
9166f840 545 Vector (IV) data.\r
546 @param[in] InData Point to the buffer containing the data to be\r
76389e18 547 decrypted.\r
9166f840 548 @param[in] InDataLength The length of InData in Bytes.\r
549 @param[out] OutData Pointer to the buffer that receives the decryption\r
550 output.\r
551\r
552 @retval EFI_UNSUPPORTED The input Algorithm is not supported.\r
553 @retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.\r
554 @retval EFI_SUCCESS The operation completed successfully.\r
555\r
556**/\r
557EFI_STATUS\r
558IpSecCryptoIoDecrypt (\r
559 IN CONST UINT8 AlgorithmId,\r
560 IN CONST UINT8 *Key,\r
561 IN CONST UINTN KeyBits,\r
562 IN CONST UINT8 *Ivec, OPTIONAL\r
563 IN UINT8 *InData,\r
564 IN UINTN InDataLength,\r
565 OUT UINT8 *OutData\r
566 );\r
567\r
568/**\r
569 Digests the Payload with key and store the result into the OutData.\r
570\r
571 This function calls relevant Hmac interface from CryptoLib according to\r
d1c85a17 572 the input algorithm ID. It computes all datas from InDataFragment and output\r
9166f840 573 the result into the OutData buffer. If the OutDataSize is larger than the related\r
d1c85a17 574 HMAC algorithm output size, return EFI_INVALID_PARAMETER.\r
f75a7f56 575\r
9166f840 576 @param[in] AlgorithmId The authentication Identification.\r
577 @param[in] Key Pointer of the authentication key.\r
578 @param[in] KeyLength The length of the Key in bytes.\r
579 @param[in] InDataFragment The list contains all data to be authenticated.\r
580 @param[in] FragmentCount The size of the InDataFragment.\r
581 @param[out] OutData For in, the buffer to receive the output data.\r
582 For out, the buffer contains the authenticated data.\r
583 @param[in] OutDataSize The size of the buffer of OutData.\r
584\r
585 @retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.\r
586 @retval EFI_INVALID_PARAMETER The OutData buffer size is larger than algorithm digest size.\r
587 @retval EFI_SUCCESS Authenticate the payload successfully.\r
588 @retval otherwise Authentication of the payload fails.\r
589\r
590**/\r
591EFI_STATUS\r
592IpSecCryptoIoHmac (\r
593 IN CONST UINT8 AlgorithmId,\r
594 IN CONST UINT8 *Key,\r
595 IN UINTN KeyLength,\r
596 IN HASH_DATA_FRAGMENT *InDataFragment,\r
597 IN UINTN FragmentCount,\r
598 OUT UINT8 *OutData,\r
599 IN UINTN OutDataSize\r
600 );\r
601\r
602/**\r
603 Digests the Payload and store the result into the OutData.\r
604\r
605 This function calls relevant Hash interface from CryptoLib according to\r
d1c85a17 606 the input algorithm ID. It computes all datas from InDataFragment and output\r
9166f840 607 the result into the OutData buffer. If the OutDataSize is larger than the related\r
d1c85a17 608 Hash algorithm output size, return EFI_INVALID_PARAMETER.\r
9166f840 609\r
610 @param[in] AlgorithmId The authentication Identification.\r
611 @param[in] InDataFragment A list contains all data to be authenticated.\r
612 @param[in] FragmentCount The size of the InDataFragment.\r
613 @param[out] OutData For in, the buffer to receive the output data.\r
614 For out, the buffer contains the authenticated data.\r
615 @param[in] OutDataSize The size of the buffer of OutData.\r
616\r
617 @retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.\r
618 @retval EFI_SUCCESS Authenticated the payload successfully.\r
619 @retval EFI_INVALID_PARAMETER If the OutDataSize is larger than the related Hash\r
620 algorithm could handle.\r
621 @retval otherwise Authentication of the payload failed.\r
622\r
623**/\r
624EFI_STATUS\r
625IpSecCryptoIoHash (\r
626 IN CONST UINT8 AlgorithmId,\r
627 IN HASH_DATA_FRAGMENT *InDataFragment,\r
628 IN UINTN FragmentCount,\r
629 OUT UINT8 *OutData,\r
630 IN UINTN OutDataSize\r
631 );\r
632\r
633/**\r
634 Generates the Diffie-Hellman public key.\r
635\r
636 This function first initiate a DHContext, then call the DhSetParameter() to set\r
d1c85a17 637 the prime and primelength, at end call the DhGenerateKey() to generates random\r
9166f840 638 secret exponent, and computes the public key. The output returned via parameter\r
639 PublicKey and PublicKeySize. DH context is updated accordingly. If the PublicKey\r
640 buffer is too small to hold the public key, EFI_INVALID_PARAMETER is returned\r
641 and PublicKeySize is set to the required buffer size to obtain the public key.\r
642\r
643 @param[in, out] DhContext Pointer to the DH context.\r
d1c85a17 644 @param[in] Generator Value of generator.\r
9166f840 645 @param[in] PrimeLength Length in bits of prime to be generated.\r
646 @param[in] Prime Pointer to the buffer to receive the generated\r
647 prime number.\r
648 @param[out] PublicKey Pointer to the buffer to receive generated public key.\r
649 @param[in, out] PublicKeySize For in, the size of PublicKey buffer in bytes.\r
650 For out, the size of data returned in PublicKey\r
651 buffer in bytes.\r
652\r
d1c85a17 653 @retval EFI_SUCCESS The operation performs successfully.\r
9166f840 654 @retval Otherwise The operation is failed.\r
655\r
656**/\r
657EFI_STATUS\r
658IpSecCryptoIoDhGetPublicKey (\r
659 IN OUT UINT8 **DhContext,\r
660 IN UINTN Generator,\r
661 IN UINTN PrimeLength,\r
662 IN CONST UINT8 *Prime,\r
663 OUT UINT8 *PublicKey,\r
664 IN OUT UINTN *PublicKeySize\r
665 );\r
666\r
667/**\r
668 Generates exchanged common key.\r
669\r
670 Given peer's public key, this function computes the exchanged common key, based\r
671 on its own context including value of prime modulus and random secret exponent.\r
672\r
673 @param[in, out] DhContext Pointer to the DH context.\r
674 @param[in] PeerPublicKey Pointer to the peer's Public Key.\r
675 @param[in] PeerPublicKeySize Size of peer's public key in bytes.\r
676 @param[out] Key Pointer to the buffer to receive generated key.\r
677 @param[in, out] KeySize For in, the size of Key buffer in bytes.\r
678 For out, the size of data returned in Key\r
679 buffer in bytes.\r
680\r
d1c85a17 681 @retval EFI_SUCCESS The operation performs successfully.\r
9166f840 682 @retval Otherwise The operation is failed.\r
683\r
684**/\r
685EFI_STATUS\r
686IpSecCryptoIoDhComputeKey (\r
687 IN OUT UINT8 *DhContext,\r
688 IN CONST UINT8 *PeerPublicKey,\r
689 IN UINTN PeerPublicKeySize,\r
690 OUT UINT8 *Key,\r
691 IN OUT UINTN *KeySize\r
692 );\r
693\r
694/**\r
695 Releases the DH context. If DhContext is NULL, return EFI_INVALID_PARAMETER.\r
696\r
697 @param[in, out] DhContext Pointer to the DH context to be freed.\r
698\r
d1c85a17 699 @retval EFI_SUCCESS The operation performs successfully.\r
9166f840 700 @retval EFI_INVALID_PARAMETER The DhContext is NULL.\r
f75a7f56 701\r
9166f840 702**/\r
703EFI_STATUS\r
704IpSecCryptoIoFreeDh (\r
705 IN OUT UINT8 **DhContext\r
706 );\r
707\r
708/**\r
709 Generates random numbers of specified size.\r
710\r
711 If the Random Generator wasn't initiated, initiate it first, then call RandomBytes.\r
712\r
713 @param[out] OutBuffer Pointer to buffer to receive random value.\r
d1c85a17 714 @param[in] Bytes Size of random bytes to generate.\r
9166f840 715\r
d1c85a17 716 @retval EFI_SUCCESS The operation performs successfully.\r
9166f840 717 @retval Otherwise The operation is failed.\r
718\r
719**/\r
720EFI_STATUS\r
721IpSecCryptoIoGenerateRandomBytes (\r
722 OUT UINT8* OutBuffer,\r
723 IN UINTN Bytes\r
724 );\r
725\r
726/**\r
727 Authenticate data with the certificate.\r
728\r
729 @param[in] InData Pointer to the Data to be signed.\r
730 @param[in] InDataSize InData size in bytes.\r
731 @param[in] PrivateKey Pointer to the private key.\r
732 @param[in] PrivateKeySize The size of Private Key in bytes.\r
733 @param[in] KeyPassWord Pointer to the password for retrieving private key.\r
734 @param[in] KeyPwdSize The size of Key Password in bytes.\r
735 @param[out] OutData The pointer to the signed data.\r
736 @param[in, out] OutDataSize Pointer to contain the size of out data.\r
f75a7f56 737\r
9166f840 738**/\r
739VOID\r
740IpSecCryptoIoAuthDataWithCertificate (\r
741 IN UINT8 *InData,\r
742 IN UINTN InDataSize,\r
743 IN UINT8 *PrivateKey,\r
744 IN UINTN PrivateKeySize,\r
745 IN UINT8 *KeyPassWord,\r
746 IN UINTN KeyPwdSize,\r
747 OUT UINT8 **OutData,\r
748 IN OUT UINTN *OutDataSize\r
749 );\r
750\r
751/**\r
752 Verify the singed data with the public key which is contained in a certificate.\r
753\r
754 @param[in] InCert Pointer to the Certificate which contains the\r
755 public key.\r
76389e18 756 @param[in] CertLen The size of Certificate in bytes.\r
9166f840 757 @param[in] InCa Pointer to the CA certificate\r
758 @param[in] CaLen The size of CA certificate in bytes.\r
d1c85a17 759 @param[in] InData Pointer to octet message hash to be checked.\r
9166f840 760 @param[in] InDataSize Size of the message hash in bytes.\r
d1c85a17 761 @param[in] Singnature The pointer to the RSA PKCS1-V1_5 signature to be verified.\r
9166f840 762 @param[in] SigSize Size of signature in bytes.\r
763\r
764 @retval TRUE Valid signature encoded in PKCS1-v1_5.\r
765 @retval FALSE Invalid signature or invalid RSA context.\r
f75a7f56 766\r
9166f840 767**/\r
768BOOLEAN\r
769IpSecCryptoIoVerifySignDataByCertificate (\r
770 IN UINT8 *InCert,\r
771 IN UINTN CertLen,\r
772 IN UINT8 *InCa,\r
773 IN UINTN CaLen,\r
774 IN UINT8 *InData,\r
775 IN UINTN InDataSize,\r
776 IN UINT8 *Singnature,\r
777 IN UINTN SigSize\r
778 );\r
779\r
780/**\r
781 Retrieves the RSA Public Key from one X509 certificate (DER format only).\r
782\r
783 @param[in] InCert Pointer to the certificate.\r
784 @param[in] CertLen The size of the certificate in bytes.\r
785 @param[out] PublicKey Pointer to the retrieved public key.\r
786 @param[out] PublicKeyLen Size of Public Key in bytes.\r
787\r
788 @retval EFI_SUCCESS Successfully get the public Key.\r
789 @retval EFI_INVALID_PARAMETER The CA certificate is malformed.\r
790\r
791**/\r
792EFI_STATUS\r
793IpSecCryptoIoGetPublicKeyFromCert (\r
794 IN UINT8 *InCert,\r
795 IN UINTN CertLen,\r
796 OUT UINT8 **PublicKey,\r
797 OUT UINTN *PublicKeyLen\r
798 );\r
799\r
800/**\r
801 Retrieves the subject name from one X509 certificate (DER format only).\r
802\r
803 @param[in] InCert Pointer to the X509 certificate.\r
804 @param[in] CertSize The size of the X509 certificate in bytes.\r
805 @param[out] CertSubject Pointer to the retrieved certificate subject.\r
806 @param[out] SubjectSize The size of Certificate Subject in bytes.\r
f75a7f56 807\r
9166f840 808 @retval EFI_SUCCESS Retrieved the certificate subject successfully.\r
809 @retval EFI_INVALID_PARAMETER The certificate is malformed.\r
f75a7f56 810\r
9166f840 811**/\r
812EFI_STATUS\r
813IpSecCryptoIoGetSubjectFromCert (\r
814 IN UINT8 *InCert,\r
815 IN UINTN CertSize,\r
816 OUT UINT8 **CertSubject,\r
817 OUT UINTN *SubjectSize\r
818 );\r
819\r
a3bcde70
HT
820#endif\r
821\r