CryptoPkg: add AES_encrypt/AES_decrpyt for TPM reference library

This is preparation patch to build TCG TPM v2.0 implementation [0].
TCG TPM v2.0 uses AES_encrypt()/AES_decrpyt() in openssl library to
implement its crypto operation.

For this, add wrapper for AES_encrypt()/AES_decrpyt().

Link: https://github.com/TrustedComputingGroup/TPM[0]
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
This commit is contained in:
Levi Yun 2025-12-19 13:39:03 +00:00 committed by mergify[bot]
parent b3e693dbeb
commit 11d93e980c
10 changed files with 607 additions and 1 deletions

View file

@ -2384,6 +2384,70 @@ CryptoServiceAesCbcDecrypt (
return CALL_BASECRYPTLIB (Aes.Services.CbcDecrypt, AesCbcDecrypt, (AesContext, Input, InputSize, Ivec, Output), FALSE);
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
CryptoServiceAesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
return CALL_BASECRYPTLIB (Aes.Services.Encrypt, AesEncrypt, (AesContext, Input, Output), FALSE);
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
CryptoServiceAesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
return CALL_BASECRYPTLIB (Aes.Services.Decrypt, AesDecrypt, (AesContext, Input, Output), FALSE);
}
/**
ARC4 is deprecated and unsupported any longer.
Keep the function field for binary compability.
@ -7164,4 +7228,7 @@ const EDKII_CRYPTO_PROTOCOL mEdkiiCrypto = {
/// TLS Set (Continued)
CryptoServiceTlsSetServerName,
CryptoServiceTlsSetSecurityLevel,
/// AES (Continued)
CryptoServiceAesEncrypt,
CryptoServiceAesDecrypt,
};

View file

@ -1373,6 +1373,64 @@ AesCbcDecrypt (
OUT UINT8 *Output
);
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
);
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
);
// =====================================================================================
// Authenticated Encryption with Associated Data (AEAD) Cryptography Primitive
// =====================================================================================

View file

@ -256,6 +256,8 @@ typedef struct {
UINT8 EcbDecrypt : 1; // Deprecated
UINT8 CbcEncrypt : 1;
UINT8 CbcDecrypt : 1;
UINT8 Encrypt : 1;
UINT8 Decrypt : 1;
} Services;
UINT32 Family;
} Aes;

View file

@ -203,3 +203,93 @@ AesCbcDecrypt (
return TRUE;
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
AES_KEY *AesKey;
//
// Check parameters.
//
if ((AesContext == NULL) || (Input == NULL) || (Output == NULL)) {
return FALSE;
}
AesKey = (AES_KEY *)AesContext;
AES_encrypt (Input, Output, AesKey);
return TRUE;
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
AES_KEY *AesKey;
//
// Check parameters.
//
if ((AesContext == NULL) || (Input == NULL) || (Output == NULL)) {
return FALSE;
}
AesKey = (AES_KEY *)AesContext;
AES_decrypt (Input, Output, AesKey + 1);
return TRUE;
}

View file

@ -105,3 +105,69 @@ AesCbcDecrypt (
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}

View file

@ -223,3 +223,69 @@ AesCbcDecrypt (
return TRUE;
}
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}

View file

@ -157,3 +157,69 @@ AesCbcDecrypt (
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}

View file

@ -105,3 +105,69 @@ AesCbcDecrypt (
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
ASSERT (FALSE);
return FALSE;
}

View file

@ -1552,6 +1552,70 @@ AesCbcDecrypt (
CALL_CRYPTO_SERVICE (AesCbcDecrypt, (AesContext, Input, InputSize, Ivec, Output), FALSE);
}
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
BOOLEAN
EFIAPI
AesEncrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
CALL_CRYPTO_SERVICE (AesEncrypt, (AesContext, Input, Output), FALSE);
}
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
BOOLEAN
EFIAPI
AesDecrypt (
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
)
{
CALL_CRYPTO_SERVICE (AesDecrypt, (AesContext, Input, Output), FALSE);
}
// =====================================================================================
// Authenticated Encryption with Associated Data (AEAD) Cryptography Primitive
// =====================================================================================

View file

@ -20,7 +20,7 @@
/// the EDK II Crypto Protocol is extended, this version define must be
/// increased.
///
#define EDKII_CRYPTO_VERSION 19
#define EDKII_CRYPTO_VERSION 20
///
/// EDK II Crypto Protocol forward declaration
@ -3036,6 +3036,64 @@ BOOLEAN
OUT UINT8 *Output
);
/**
Performs AES encryption on single block (AES_BLOCK_SIZE)
This function performs AES encryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block data
@param[out] Output Pointer to a buffer that receives the AES encryption output.
@retval TRUE AES encryption succeeded.
@retval FALSE AES encryption failed.
**/
typedef
BOOLEAN
(EFIAPI *EDKII_CRYPTO_AES_ENCRYPT)(
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
);
/**
Performs AES decryption on single block (AES_BLOCK_SIZE)
This function performs AES decryption on single block pointed by Input.
Caller must perform padding, if necessary, to ensure single block size.
AesContext should be already correctly initialized by AesInit().
Behavior with invalid AES context is undefined.
If AesContext is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in] AesContext Pointer to the AES context.
@param[in] Input Pointer to the buffer containing single block encrpyted data.
@param[out] Output Pointer to a buffer that receives the AES decryption output.
@retval TRUE AES decryption succeeded.
@retval FALSE AES decryption failed.
**/
typedef
BOOLEAN
(EFIAPI *EDKII_CRYPTO_AES_DECRYPT)(
IN VOID *AesContext,
IN CONST UINT8 *Input,
OUT UINT8 *Output
);
/**
ARC4 is deprecated and unsupported any longer.
Keep the function field for binary compability.
@ -5750,6 +5808,9 @@ struct _EDKII_CRYPTO_PROTOCOL {
/// TLS Set (Continued)
EDKII_CRYPTO_TLS_SET_SERVER_NAME TlsSetServerName;
EDKII_CRYPTO_TLS_SET_SECURITY_LEVEL TlsSetSecurityLevel;
/// AES (Continued)
EDKII_CRYPTO_AES_ENCRYPT AesEncrypt;
EDKII_CRYPTO_AES_DECRYPT AesDecrypt;
};
extern GUID gEdkiiCryptoProtocolGuid;