IpSecCryptIo.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
Common interfaces to call Security library.
Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "IpSecCryptIo.h"
//
//
GLOBAL_REMOVE_IF_UNREFERENCED ENCRYPT_ALGORITHM mIpsecEncryptAlgorithmList[IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE] = {
};
//
// The informations for the supported Authentication algorithm
//
GLOBAL_REMOVE_IF_UNREFERENCED AUTH_ALGORITHM mIpsecAuthAlgorithmList[IPSEC_AUTH_ALGORITHM_LIST_SIZE] = {
{IKE_AALG_SHA1HMAC, 20, 12, 64, HmacSha1GetContextSize, HmacSha1Init, HmacSha1Update, HmacSha1Final}
};
//
// The information for the supported Hash aglorithm
//
GLOBAL_REMOVE_IF_UNREFERENCED HASH_ALGORITHM mIpsecHashAlgorithmList[IPSEC_HASH_ALGORITHM_LIST_SIZE] = {
};
/**
Get the block size of specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of block size.
**/
)
{
}
}
return (UINTN) -1;
}
/**
Get the key length of the specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of key length.
**/
)
{
}
}
return (UINTN) -1;
}
/**
Get the IV size of the specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of IV size.
**/
)
{
}
}
return (UINTN) -1;
}
/**
Get the HMAC digest length by the specified Algorithm ID.
@param[in] AlgorithmId The specified Alogrithm ID.
@return The digest length of the specified Authentication Algorithm ID.
**/
)
{
//
// Return the Digest Length of the Algorithm.
//
}
}
return 0;
}
/**
Get the ICV size of the specified Authenticaion alogrithm.
@param[in] AlgorithmId The Authentication algorithm ID.
@return The value of ICV size.
**/
)
{
}
}
return (UINTN) -1;
}
/**
Generate a random data for IV. If the IvSize is zero, not needed to create
IV and return EFI_SUCCESS.
@param[in] IvBuffer The pointer of the IV buffer.
@param[in] IvSize The IV size in bytes.
@retval EFI_SUCCESS Create a random data for IV.
**/
)
{
if (IvSize != 0) {
}
return EFI_SUCCESS;
}
/**
Get index of the specified encryption alogrithm from the mIpsecEncryptAlgorithemList.
@param[in] AlgorithmId The encryption algorithm ID.
@return the index.
**/
)
{
return Index;
}
}
return (UINTN) -1;
}
/**
Get index of the specified encryption alogrithm from the mIpsecAuthAlgorithemList.
@param[in] AlgorithmId The encryption algorithm ID.
@return the index.
**/
)
{
//
// The BlockSize is same with IvSize.
//
return Index;
}
}
return (UINTN) -1;
}
/**
Encrypt the buffer.
This function calls relevant encryption interface from CryptoLib according to
the input alogrithm ID. The InData should be multiple of block size. This function
doesn't perform the padding. If it has the Ivec data, the length of it should be
same with the block size. The block size is different from the different algorithm.
@param[in] AlgorithmId The Alogrithem identification defined in RFC.
@param[in] Key Pointer to the buffer containing encrypting key.
@param[in] KeyBits The length of the key in bits.
@param[in] Ivec Point to the buffer containning the Initializeion
Vector (IV) data.
@param[in] InData Point to the buffer containing the data to be
encrypted.
@param[in] InDataLength The length of InData in Bytes.
@param[out] OutData Point to the buffer that receives the encryption
output.
@retval EFI_UNSUPPORTED The input Algorithm is not supported.
@retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
@retval EFI_SUCCESS The operation completed successfully.
**/
)
{
switch (AlgorithmId) {
case IKE_EALG_NULL:
case IKE_EALG_NONE:
return EFI_SUCCESS;
case IKE_EALG_3DESCBC:
case IKE_EALG_AESCBC:
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
return EFI_OUT_OF_RESOURCES;
}
//
// Initiate Context
//
if (mIpsecEncryptAlgorithmList[Index].CipherEncrypt (Context, InData, InDataLength, Ivec, OutData)) {
}
}
break;
default:
return Status;
}
}
return Status;
}
/**
Decrypts the buffer.
This function calls relevant Decryption interface from CryptoLib according to
the input alogrithm ID. The InData should be multiple of block size. This function
doesn't perform the padding. If it has the Ivec data, the length of it should be
same with the block size. The block size is different from the different algorithm.
@param[in] AlgorithmId The Alogrithem identification defined in RFC.
@param[in] Key Pointer to the buffer containing encrypting key.
@param[in] KeyBits The length of the key in bits.
@param[in] Ivec Point to the buffer containning the Initializeion
Vector (IV) data.
@param[in] InData Point to the buffer containing the data to be
decrypted.
@param[in] InDataLength The length of InData in Bytes.
@param[out] OutData Pointer to the buffer that receives the decryption
output.
@retval EFI_UNSUPPORTED The input Algorithm is not supported.
@retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
@retval EFI_SUCCESS The operation completed successfully.
**/
)
{
switch (AlgorithmId) {
case IKE_EALG_NULL:
case IKE_EALG_NONE:
return EFI_SUCCESS;
case IKE_EALG_3DESCBC:
case IKE_EALG_AESCBC:
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
return EFI_OUT_OF_RESOURCES;
}
//
// Initiate Context
//
if (mIpsecEncryptAlgorithmList[Index].CipherDecrypt (Context, InData, InDataLength, Ivec, OutData)) {
}
}
break;
default:
return Status;
}
}
return Status;
}
/**
Digests the Payload with key and store the result into the OutData.
This function calls relevant Hmac interface from CryptoLib according to
the input alogrithm ID. It computes all datas from InDataFragment and output
the result into the OutData buffer. If the OutDataSize is larger than the related
HMAC alogrithm output size, return EFI_INVALID_PARAMETER.
@param[in] AlgorithmId The authentication Identification.
@param[in] Key Pointer of the authentication key.
@param[in] KeyLength The length of the Key in bytes.
@param[in] InDataFragment The list contains all data to be authenticated.
@param[in] FragmentCount The size of the InDataFragment.
@param[out] OutData For in, the buffer to receive the output data.
For out, the buffer contains the authenticated data.
@param[in] OutDataSize The size of the buffer of OutData.
@retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
@retval EFI_INVALID_PARAMETER The OutData buffer size is larger than algorithm digest size.
@retval EFI_SUCCESS Authenticate the payload successfully.
@retval otherwise Authentication of the payload fails.
**/
)
{
OutHashData = NULL;
//
// If the expected hash data size is larger than the related Hash algorithm
// output length, return EFI_INVALID_PARAMETER.
//
if (OutDataSize > OutHashSize) {
return EFI_INVALID_PARAMETER;
}
if (OutHashData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
switch (AlgorithmId) {
case IKE_AALG_NONE :
case IKE_AALG_NULL :
return EFI_SUCCESS;
case IKE_AALG_SHA1HMAC:
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
if (HashContext == NULL) {
goto Exit;
}
//
// Initiate HMAC context and hash the input data.
//
)
) {
goto Exit;
}
}
//
// In some cases, like the Icv computing, the Icv size might be less than
// the key length size, so copy the part of hash data to the OutData.
//
}
goto Exit;
}
default:
return Status;
}
Exit:
if (HashContext != NULL) {
}
if (OutHashData != NULL) {
}
return Status;
}
/**
Digests the Payload and store the result into the OutData.
This function calls relevant Hash interface from CryptoLib according to
the input alogrithm ID. It computes all datas from InDataFragment and output
the result into the OutData buffer. If the OutDataSize is larger than the related
Hash alogrithm output size, return EFI_INVALID_PARAMETER.
@param[in] AlgorithmId The authentication Identification.
@param[in] InDataFragment A list contains all data to be authenticated.
@param[in] FragmentCount The size of the InDataFragment.
@param[out] OutData For in, the buffer to receive the output data.
For out, the buffer contains the authenticated data.
@param[in] OutDataSize The size of the buffer of OutData.
@retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
@retval EFI_SUCCESS Authenticated the payload successfully.
@retval EFI_INVALID_PARAMETER If the OutDataSize is larger than the related Hash
algorithm could handle.
@retval otherwise Authentication of the payload failed.
**/
)
{
OutHashData = NULL;
//
// If the expected hash data size is larger than the related Hash algorithm
// output length, return EFI_INVALID_PARAMETER.
//
if (OutDataSize > OutHashSize) {
return EFI_INVALID_PARAMETER;
}
if (OutHashData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
switch (AlgorithmId) {
case IKE_AALG_NONE:
case IKE_AALG_NULL:
return EFI_SUCCESS;
case IKE_AALG_SHA1HMAC:
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
if (HashContext == NULL) {
goto Exit;
}
//
// Initiate Hash context and hash the input data.
//
)
) {
goto Exit;
}
}
//
// In some cases, like the Icv computing, the Icv size might be less than
// the key length size, so copy the part of hash data to the OutData.
//
}
goto Exit;
}
default:
return Status;
}
Exit:
if (HashContext != NULL) {
}
if (OutHashData != NULL) {
}
return Status;
}
/**
Generates the Diffie-Hellman public key.
This function first initiate a DHContext, then call the DhSetParameter() to set
the prime and primelenght, at end call the DhGenerateKey() to generates random
secret exponent, and computes the public key. The output returned via parameter
PublicKey and PublicKeySize. DH context is updated accordingly. If the PublicKey
buffer is too small to hold the public key, EFI_INVALID_PARAMETER is returned
and PublicKeySize is set to the required buffer size to obtain the public key.
@param[in, out] DhContext Pointer to the DH context.
@param[in] Generator Vlaue of generator.
@param[in] PrimeLength Length in bits of prime to be generated.
@param[in] Prime Pointer to the buffer to receive the generated
prime number.
@param[out] PublicKey Pointer to the buffer to receive generated public key.
@param[in, out] PublicKeySize For in, the size of PublicKey buffer in bytes.
For out, the size of data returned in PublicKey
buffer in bytes.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
)
{
goto Exit;
}
goto Exit;
}
return EFI_SUCCESS;
Exit:
}
return Status;
}
/**
Generates exchanged common key.
Given peer's public key, this function computes the exchanged common key, based
on its own context including value of prime modulus and random secret exponent.
@param[in, out] DhContext Pointer to the DH context.
@param[in] PeerPublicKey Pointer to the peer's Public Key.
@param[in] PeerPublicKeySize Size of peer's public key in bytes.
@param[out] Key Pointer to the buffer to receive generated key.
@param[in, out] KeySize For in, the size of Key buffer in bytes.
For out, the size of data returned in Key
buffer in bytes.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Releases the DH context. If DhContext is NULL, return EFI_INVALID_PARAMETER.
@param[in, out] DhContext Pointer to the DH context to be freed.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval EFI_INVALID_PARAMETER The DhContext is NULL.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Generates random numbers of specified size.
If the Random Generator wasn't initiated, initiate it first, then call RandomBytes.
@param[out] OutBuffer Pointer to buffer to receive random value.
@param[in] Bytes Size of randome bytes to generate.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
)
{
if (!mInitialRandomSeed) {
RandomSeed (NULL, 0);
}
return EFI_SUCCESS;
} else {
return EFI_INVALID_PARAMETER;
}
}
/**
Authenticate data with the certificate.
@param[in] InData Pointer to the Data to be signed.
@param[in] InDataSize InData size in bytes.
@param[in] PrivateKey Pointer to the private key.
@param[in] PrivateKeySize The size of Private Key in bytes.
@param[in] KeyPassWord Pointer to the password for retrieving private key.
@param[in] KeyPwdSize The size of Key Password in bytes.
@param[out] OutData The pointer to the signed data.
@param[in, out] OutDataSize Pointer to contain the size of out data.
**/
)
{
SigSize = 0;
//
// Retrieve RSA Private Key from password-protected PEM data
//
(VOID **) &RsaContext
);
if (RsaContext == NULL) {
return;
}
//
// Sign data
//
} else {
return;
}
*OutDataSize = SigSize;
if (RsaContext != NULL) {
}
}
/**
Verify the singed data with the public key which is contained in a certificate.
@param[in] InCert Pointer to the Certificate which contains the
public key.
@param[in] CertLen The size of Certificate in bytes.
@param[in] InCa Pointer to the CA certificate
@param[in] CaLen The size of CA certificate in bytes.
@param[in] InData Pointer to octect message hash to be checked.
@param[in] InDataSize Size of the message hash in bytes.
@param[in] Singnature The pointer to the RSA PKCS1-V1_5 signature to be verifed.
@param[in] SigSize Size of signature in bytes.
@retval TRUE Valid signature encoded in PKCS1-v1_5.
@retval FALSE Invalid signature or invalid RSA context.
**/
)
{
//
// Create the RSA Context
//
RsaContext = RsaNew ();
if (RsaContext == NULL) {
return FALSE;
}
//
// Verify the validity of X509 Certificate
//
return FALSE;
}
//
// Retrieve the RSA public Key from Certificate
//
//
// Verify data
//
if (RsaContext != NULL) {
}
return Status;
}
/**
Retrieves the RSA Public Key from one X509 certificate (DER format only).
@param[in] InCert Pointer to the certificate.
@param[in] CertLen The size of the certificate in bytes.
@param[out] PublicKey Pointer to the retrieved public key.
@param[out] PublicKeyLen Size of Public Key in bytes.
@retval EFI_SUCCESS Successfully get the public Key.
@retval EFI_INVALID_PARAMETER The certificate is malformed.
**/
)
{
//
// Create the RSA Context
//
RsaContext = RsaNew ();
//
// Retrieve the RSA public key from CA Certificate
//
goto EXIT;
}
*PublicKeyLen = 0;
}
EXIT:
if (RsaContext != NULL) {
}
return Status;
}
/**
Retrieves the subject name from one X509 certificate (DER format only).
@param[in] InCert Pointer to the X509 certificate.
@param[in] CertSize The size of the X509 certificate in bytes.
@param[out] CertSubject Pointer to the retrieved certificate subject.
@param[out] SubjectSize The size of Certificate Subject in bytes.
@retval EFI_SUCCESS Retrieved the certificate subject successfully.
@retval EFI_INVALID_PARAMETER The certificate is malformed.
**/
)
{
*SubjectSize = 0;
}
return Status;
}