2 Definitions related to the Cryptographic Operations in IPsec.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #ifndef _EFI_IPSEC_CRYPTIO_H_
10 #define _EFI_IPSEC_CRYPTIO_H_
12 #include <Protocol/IpSecConfig.h>
13 #include <Library/DebugLib.h>
14 #include <Library/BaseCryptLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/MemoryAllocationLib.h>
18 #include "IpSecImpl.h"
19 #include "IkeCommon.h"
21 #define IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE 4
22 #define IPSEC_AUTH_ALGORITHM_LIST_SIZE 3
23 #define IPSEC_HASH_ALGORITHM_LIST_SIZE 3
26 /// Authentication Algorithm Definition
27 /// The number value definition is aligned to IANA assignment
29 #define IKE_AALG_NONE 0x00
30 #define IKE_AALG_SHA1HMAC 0x02
31 #define IKE_AALG_NULL 0xFB
34 /// Encryption Algorithm Definition
35 /// The number value definition is aligned to IANA assignment
37 #define IKE_EALG_NONE 0x00
38 #define IKE_EALG_3DESCBC 0x03
39 #define IKE_EALG_NULL 0x0B
40 #define IKE_EALG_AESCBC 0x0C
43 Prototype of HMAC GetContextSize.
45 Retrieves the size, in bytes, of the context buffer required.
47 @return The size, in bytes, of the context buffer required.
52 (EFIAPI
*CRYPTO_HMAC_GETCONTEXTSIZE
)(
57 Prototype of HMAC Operation Initiating.
59 Initialization with a new context.
61 @param[out] Context Input Context.
62 @param[in] Key Pointer to the key for HMAC.
63 @param[in] KeySize The length of the Key in bytes.
65 @retval TRUE Initialization Successfully.
70 (EFIAPI
*CRYPTO_HMAC_INIT
)(
77 Prototype of HMAC update.
78 HMAC update operation. Continue an HMAC message digest operation, processing
79 another message block, and updating the HMAC context.
81 If Context is NULL, then ASSERT().
82 If Data is NULL, then ASSERT().
84 @param[in,out] Context The Specified Context.
85 @param[in,out] Data The Input Data to be digested.
86 @param[in] DataLength The length, in bytes, of Data.
88 @retval TRUE Update data successfully.
89 @retval FALSE The Context has been finalized.
94 (EFIAPI
*CRYPTO_HMAC_UPDATE
)(
101 Prototype of HMAC finalization.
102 Terminate a HMAC message digest operation and output the message digest.
104 If Context is NULL, then ASSERT().
105 If HashValue is NULL, then ASSERT().
107 @param[in,out] Context The specified Context.
108 @param[out] HmacValue Pointer to a 16-byte message digest output buffer.
110 @retval TRUE Finalized successfully.
115 (EFIAPI
*CRYPTO_HMAC_FINAL
)(
116 IN OUT VOID
*Context
,
121 Prototype of Block Cipher GetContextSize.
123 Retrieves the size, in bytes, of the context buffer required.
125 @return The size, in bytes, of the context buffer required.
130 (EFIAPI
*CRYPTO_CIPHER_GETCONTEXTSIZE
)(
135 Prototype of Block Cipher initiation.
136 Initializes the user-supplied key as the specified context (key materials) for both
137 encryption and decryption operations.
139 If Context is NULL, then ASSERT().
140 If Key is NULL, then generate random key for usage.
142 @param[in,out] Context The specified Context.
143 @param[in] Key User-supplied cipher key.
144 @param[in] KeyBits Key length in bits.
146 @retval TRUE Block Cipher Initialization was successful.
151 (EFIAPI
*CRYPTO_CIPHER_INIT
)(
152 IN OUT VOID
*Context
,
158 Prototype of Cipher encryption.
159 Encrypts plaintext message with the specified cipher.
161 If Context is NULL, then ASSERT().
162 If InData is NULL, then ASSERT().
163 If Size of input data is not multiple of Cipher algorithm related block size,
166 @param[in] Context The specified Context.
167 @param[in] InData The input plaintext data to be encrypted.
168 @param[in] InputSize The size of input data.
169 @param[in] Ivec Pointer to Initial Vector data for encryption.
170 @param[out] OutData The resultant encrypted ciphertext.
172 @retval TRUE Encryption successful.
177 (EFIAPI
*CRYPTO_CIPHER_ENCRYPT
)(
179 IN CONST UINT8
*InData
,
181 IN CONST UINT8
*Ivec
,
186 Prototype of Cipher decryption.
187 Decrypts cipher message with specified cipher.
189 If Context is NULL, then ASSERT().
190 If InData is NULL, then ASSERT().
191 If Size of input data is not a multiple of a certaion block size , then ASSERT().
193 @param[in] Context The specified Context.
194 @param[in] InData The input ciphertext data to be decrypted.
195 @param[in] InputSize The InData size.
196 @param[in] Ivec Pointer to the Initial Vector data for decryption.
197 @param[out] OutData The resultant decrypted plaintext.
199 @retval TRUE Decryption successful.
204 (EFIAPI
*CRYPTO_CIPHER_DECRYPT
)(
206 IN CONST UINT8
*InData
,
208 IN CONST UINT8
*Ivec
,
213 Prototype of Hash ContextSize.
215 Retrieves the size, in bytes, of the context buffer required for specified hash operations.
217 @return The size, in bytes, of the context buffer required for certain hash operations.
222 (EFIAPI
*CRYPTO_HASH_GETCONTEXTSIZE
)(
227 Prototype of Hash Initiate.
229 Initializes user-supplied memory pointed by Context as specified hash context for
232 If Context is NULL, then ASSERT().
234 @param[out] Context Pointer to specified context being initialized.
236 @retval TRUE context initialization succeeded.
237 @retval FALSE context initialization failed.
242 (EFIAPI
*CRYPTO_HASH_INIT
)(
247 Prototype of Hash Update
249 Digests the input data and updates hash context.
251 This function performs digest on a data buffer of the specified size.
252 It can be called multiple times to compute the digest of long or discontinuous data streams.
253 Context should be already correctly initialized by HashInit(), and should not be finalized
254 by HashFinal(). Behavior with invalid context is undefined.
256 If Context is NULL, then ASSERT().
258 @param[in, out] Context Pointer to the specified context.
259 @param[in] Data Pointer to the buffer containing the data to be hashed.
260 @param[in] DataSize Size of Data buffer in bytes.
262 @retval TRUE data digest succeeded.
263 @retval FALSE data digest failed.
268 (EFIAPI
*CRYPTO_HASH_UPDATE
)(
269 IN OUT VOID
*Context
,
275 Prototype of Hash Finalization.
277 Completes computation of the digest value.
279 This function completes hash computation and retrieves the digest value into
280 the specified memory. After this function has been called, the context cannot
282 context should be already correctly initialized by HashInit(), and should not be
283 finalized by HashFinal(). Behavior with invalid context is undefined.
285 If Context is NULL, then ASSERT().
286 If HashValue is NULL, then ASSERT().
288 @param[in, out] Context Pointer to the specified context.
289 @param[out] HashValue Pointer to a buffer that receives the digest
292 @retval TRUE digest computation succeeded.
293 @retval FALSE digest computation failed.
298 (EFIAPI
*CRYPTO_HASH_FINAL
)(
299 IN OUT VOID
*Context
,
304 // The struct used to store the information and operation of Block Cipher algorithm.
306 typedef struct _ENCRYPT_ALGORITHM
{
308 // The ID of the Algorithm
312 // The Key length of the Algorithm
316 // Iv Size of the Algorithm
320 // The Block Size of the Algorithm
324 // The Function pointer of GetContextSize.
326 CRYPTO_CIPHER_GETCONTEXTSIZE CipherGetContextSize
;
328 // The Function pointer of Cipher initiation.
330 CRYPTO_CIPHER_INIT CipherInitiate
;
332 // The Function pointer of Cipher Encryption.
334 CRYPTO_CIPHER_ENCRYPT CipherEncrypt
;
336 // The Function pointer of Cipher Decryption.
338 CRYPTO_CIPHER_DECRYPT CipherDecrypt
;
342 // The struct used to store the information and operation of Authentication algorithm.
344 typedef struct _AUTH_ALGORITHM
{
346 // ID of the Algorithm
350 // The Key length of the Algorithm
354 // The ICV length of the Algorithm
358 // The block size of the Algorithm
362 // The function pointer of GetContextSize.
364 CRYPTO_HMAC_GETCONTEXTSIZE HmacGetContextSize
;
366 // The function pointer of Initiation
368 CRYPTO_HMAC_INIT HmacInitiate
;
370 // The function pointer of HMAC Update.
372 CRYPTO_HMAC_UPDATE HmacUpdate
;
374 // The fucntion pointer of HMAC Final
376 CRYPTO_HMAC_FINAL HmacFinal
;
380 // The struct used to store the information and operation of Hash algorithm.
382 typedef struct _HASH_ALGORITHM
{
384 // ID of the Algorithm
388 // The Key length of the Algorithm
392 // The ICV length of the Algorithm
396 // The block size of the Algorithm
400 // The function pointer of GetContextSize
402 CRYPTO_HASH_GETCONTEXTSIZE HashGetContextSize
;
404 // The function pointer of Initiation
406 CRYPTO_HASH_INIT HashInitiate
;
408 // The function pointer of Hash Update
410 CRYPTO_HASH_UPDATE HashUpdate
;
412 // The fucntion pointer of Hash Final
414 CRYPTO_HASH_FINAL HashFinal
;
418 Get the IV size of specified encryption algorithm.
420 @param[in] AlgorithmId The encryption algorithm ID.
422 @return The value of IV size.
426 IpSecGetEncryptIvLength (
431 Get the block size of specified encryption algorithm.
433 @param[in] AlgorithmId The encryption algorithm ID.
435 @return The value of block size.
439 IpSecGetEncryptBlockSize (
444 Get the required key length of the specified encryption algorithm.
446 @param[in] AlgorithmId The encryption algorithm ID.
448 @return The value of key length.
452 IpSecGetEncryptKeyLength (
457 Get the ICV size of the specified Authentication algorithm.
459 @param[in] AlgorithmId The Authentication algorithm ID.
461 @return The value of ICV size.
470 Get the HMAC digest length by the specified Algorithm ID.
472 @param[in] AlgorithmId The specified Algorithm ID.
474 @return The digest length of the specified Authentication Algorithm ID.
478 IpSecGetHmacDigestLength (
483 Generate a random data for IV. If the IvSize is zero, not needed to create
484 IV and return EFI_SUCCESS.
486 @param[in] IvBuffer The pointer of the IV buffer.
487 @param[in] IvSize The IV size in bytes.
489 @retval EFI_SUCCESS Create random data for IV.
501 This function calls relevant encryption interface from CryptoLib according to
502 the input algorithm ID. The InData should be multiple of block size. This function
503 doesn't perform the padding. If it has the Ivec data, the length of it should be
504 same with the block size. The block size is different from the different algorithm.
506 @param[in] AlgorithmId The Algorithm identification defined in RFC.
507 @param[in] Key Pointer to the buffer containing encrypting key.
508 @param[in] KeyBits The length of the key in bits.
509 @param[in] Ivec Point to the buffer containing the Initialization
511 @param[in] InData Point to the buffer containing the data to be
513 @param[in] InDataLength The length of InData in Bytes.
514 @param[out] OutData Point to the buffer that receives the encryption
517 @retval EFI_UNSUPPORTED The input Algorithm is not supported.
518 @retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
519 @retval EFI_SUCCESS The operation completed successfully.
523 IpSecCryptoIoEncrypt (
524 IN CONST UINT8 AlgorithmId
,
526 IN CONST UINTN KeyBits
,
527 IN CONST UINT8
*Ivec
, OPTIONAL
529 IN UINTN InDataLength
,
536 This function calls relevant Decryption interface from CryptoLib according to
537 the input algorithm ID. The InData should be multiple of block size. This function
538 doesn't perform the padding. If it has the Ivec data, the length of it should be
539 same with the block size. The block size is different from the different algorithm.
541 @param[in] AlgorithmId The Algorithm identification defined in RFC.
542 @param[in] Key Pointer to the buffer containing encrypting key.
543 @param[in] KeyBits The length of the key in bits.
544 @param[in] Ivec Point to the buffer containing the Initialization
546 @param[in] InData Point to the buffer containing the data to be
548 @param[in] InDataLength The length of InData in Bytes.
549 @param[out] OutData Pointer to the buffer that receives the decryption
552 @retval EFI_UNSUPPORTED The input Algorithm is not supported.
553 @retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
554 @retval EFI_SUCCESS The operation completed successfully.
558 IpSecCryptoIoDecrypt (
559 IN CONST UINT8 AlgorithmId
,
561 IN CONST UINTN KeyBits
,
562 IN CONST UINT8
*Ivec
, OPTIONAL
564 IN UINTN InDataLength
,
569 Digests the Payload with key and store the result into the OutData.
571 This function calls relevant Hmac interface from CryptoLib according to
572 the input algorithm ID. It computes all datas from InDataFragment and output
573 the result into the OutData buffer. If the OutDataSize is larger than the related
574 HMAC algorithm output size, return EFI_INVALID_PARAMETER.
576 @param[in] AlgorithmId The authentication Identification.
577 @param[in] Key Pointer of the authentication key.
578 @param[in] KeyLength The length of the Key in bytes.
579 @param[in] InDataFragment The list contains all data to be authenticated.
580 @param[in] FragmentCount The size of the InDataFragment.
581 @param[out] OutData For in, the buffer to receive the output data.
582 For out, the buffer contains the authenticated data.
583 @param[in] OutDataSize The size of the buffer of OutData.
585 @retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
586 @retval EFI_INVALID_PARAMETER The OutData buffer size is larger than algorithm digest size.
587 @retval EFI_SUCCESS Authenticate the payload successfully.
588 @retval otherwise Authentication of the payload fails.
593 IN CONST UINT8 AlgorithmId
,
596 IN HASH_DATA_FRAGMENT
*InDataFragment
,
597 IN UINTN FragmentCount
,
603 Digests the Payload and store the result into the OutData.
605 This function calls relevant Hash interface from CryptoLib according to
606 the input algorithm ID. It computes all datas from InDataFragment and output
607 the result into the OutData buffer. If the OutDataSize is larger than the related
608 Hash algorithm output size, return EFI_INVALID_PARAMETER.
610 @param[in] AlgorithmId The authentication Identification.
611 @param[in] InDataFragment A list contains all data to be authenticated.
612 @param[in] FragmentCount The size of the InDataFragment.
613 @param[out] OutData For in, the buffer to receive the output data.
614 For out, the buffer contains the authenticated data.
615 @param[in] OutDataSize The size of the buffer of OutData.
617 @retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
618 @retval EFI_SUCCESS Authenticated the payload successfully.
619 @retval EFI_INVALID_PARAMETER If the OutDataSize is larger than the related Hash
620 algorithm could handle.
621 @retval otherwise Authentication of the payload failed.
626 IN CONST UINT8 AlgorithmId
,
627 IN HASH_DATA_FRAGMENT
*InDataFragment
,
628 IN UINTN FragmentCount
,
634 Generates the Diffie-Hellman public key.
636 This function first initiate a DHContext, then call the DhSetParameter() to set
637 the prime and primelength, at end call the DhGenerateKey() to generates random
638 secret exponent, and computes the public key. The output returned via parameter
639 PublicKey and PublicKeySize. DH context is updated accordingly. If the PublicKey
640 buffer is too small to hold the public key, EFI_INVALID_PARAMETER is returned
641 and PublicKeySize is set to the required buffer size to obtain the public key.
643 @param[in, out] DhContext Pointer to the DH context.
644 @param[in] Generator Value of generator.
645 @param[in] PrimeLength Length in bits of prime to be generated.
646 @param[in] Prime Pointer to the buffer to receive the generated
648 @param[out] PublicKey Pointer to the buffer to receive generated public key.
649 @param[in, out] PublicKeySize For in, the size of PublicKey buffer in bytes.
650 For out, the size of data returned in PublicKey
653 @retval EFI_SUCCESS The operation performs successfully.
654 @retval Otherwise The operation is failed.
658 IpSecCryptoIoDhGetPublicKey (
659 IN OUT UINT8
**DhContext
,
661 IN UINTN PrimeLength
,
662 IN CONST UINT8
*Prime
,
663 OUT UINT8
*PublicKey
,
664 IN OUT UINTN
*PublicKeySize
668 Generates exchanged common key.
670 Given peer's public key, this function computes the exchanged common key, based
671 on its own context including value of prime modulus and random secret exponent.
673 @param[in, out] DhContext Pointer to the DH context.
674 @param[in] PeerPublicKey Pointer to the peer's Public Key.
675 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
676 @param[out] Key Pointer to the buffer to receive generated key.
677 @param[in, out] KeySize For in, the size of Key buffer in bytes.
678 For out, the size of data returned in Key
681 @retval EFI_SUCCESS The operation performs successfully.
682 @retval Otherwise The operation is failed.
686 IpSecCryptoIoDhComputeKey (
687 IN OUT UINT8
*DhContext
,
688 IN CONST UINT8
*PeerPublicKey
,
689 IN UINTN PeerPublicKeySize
,
691 IN OUT UINTN
*KeySize
695 Releases the DH context. If DhContext is NULL, return EFI_INVALID_PARAMETER.
697 @param[in, out] DhContext Pointer to the DH context to be freed.
699 @retval EFI_SUCCESS The operation performs successfully.
700 @retval EFI_INVALID_PARAMETER The DhContext is NULL.
704 IpSecCryptoIoFreeDh (
705 IN OUT UINT8
**DhContext
709 Generates random numbers of specified size.
711 If the Random Generator wasn't initiated, initiate it first, then call RandomBytes.
713 @param[out] OutBuffer Pointer to buffer to receive random value.
714 @param[in] Bytes Size of random bytes to generate.
716 @retval EFI_SUCCESS The operation performs successfully.
717 @retval Otherwise The operation is failed.
721 IpSecCryptoIoGenerateRandomBytes (
722 OUT UINT8
* OutBuffer
,
727 Authenticate data with the certificate.
729 @param[in] InData Pointer to the Data to be signed.
730 @param[in] InDataSize InData size in bytes.
731 @param[in] PrivateKey Pointer to the private key.
732 @param[in] PrivateKeySize The size of Private Key in bytes.
733 @param[in] KeyPassWord Pointer to the password for retrieving private key.
734 @param[in] KeyPwdSize The size of Key Password in bytes.
735 @param[out] OutData The pointer to the signed data.
736 @param[in, out] OutDataSize Pointer to contain the size of out data.
740 IpSecCryptoIoAuthDataWithCertificate (
743 IN UINT8
*PrivateKey
,
744 IN UINTN PrivateKeySize
,
745 IN UINT8
*KeyPassWord
,
748 IN OUT UINTN
*OutDataSize
752 Verify the singed data with the public key which is contained in a certificate.
754 @param[in] InCert Pointer to the Certificate which contains the
756 @param[in] CertLen The size of Certificate in bytes.
757 @param[in] InCa Pointer to the CA certificate
758 @param[in] CaLen The size of CA certificate in bytes.
759 @param[in] InData Pointer to octet message hash to be checked.
760 @param[in] InDataSize Size of the message hash in bytes.
761 @param[in] Singnature The pointer to the RSA PKCS1-V1_5 signature to be verified.
762 @param[in] SigSize Size of signature in bytes.
764 @retval TRUE Valid signature encoded in PKCS1-v1_5.
765 @retval FALSE Invalid signature or invalid RSA context.
769 IpSecCryptoIoVerifySignDataByCertificate (
776 IN UINT8
*Singnature
,
781 Retrieves the RSA Public Key from one X509 certificate (DER format only).
783 @param[in] InCert Pointer to the certificate.
784 @param[in] CertLen The size of the certificate in bytes.
785 @param[out] PublicKey Pointer to the retrieved public key.
786 @param[out] PublicKeyLen Size of Public Key in bytes.
788 @retval EFI_SUCCESS Successfully get the public Key.
789 @retval EFI_INVALID_PARAMETER The CA certificate is malformed.
793 IpSecCryptoIoGetPublicKeyFromCert (
796 OUT UINT8
**PublicKey
,
797 OUT UINTN
*PublicKeyLen
801 Retrieves the subject name from one X509 certificate (DER format only).
803 @param[in] InCert Pointer to the X509 certificate.
804 @param[in] CertSize The size of the X509 certificate in bytes.
805 @param[out] CertSubject Pointer to the retrieved certificate subject.
806 @param[out] SubjectSize The size of Certificate Subject in bytes.
808 @retval EFI_SUCCESS Retrieved the certificate subject successfully.
809 @retval EFI_INVALID_PARAMETER The certificate is malformed.
813 IpSecCryptoIoGetSubjectFromCert (
816 OUT UINT8
**CertSubject
,
817 OUT UINTN
*SubjectSize