11#include "InternalCryptLib.h"
12#include <openssl/objects.h>
13#include <openssl/rsa.h>
14#include <openssl/x509.h>
88 IN CONST UINT8 *PrngSeed OPTIONAL,
90 IN UINT16 DigestLen OPTIONAL,
91 OUT UINT8 **EncryptedData,
96 EVP_PKEY_CTX *PkeyCtx;
99 CONST EVP_MD *HashAlg;
104 if ((Pkey ==
NULL) || (InData ==
NULL) ||
105 (EncryptedData ==
NULL) || (EncryptedDataSize ==
NULL))
110 *EncryptedData =
NULL;
111 *EncryptedDataSize = 0;
122 if (PrngSeed !=
NULL) {
131 PkeyCtx = EVP_PKEY_CTX_new (Pkey,
NULL);
132 if (PkeyCtx ==
NULL) {
142 if ((EVP_PKEY_encrypt_init (PkeyCtx) <= 0) ||
143 (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0))
151 if (DigestLen != 0) {
153 if (HashAlg ==
NULL) {
157 if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) {
161 if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) {
169 if (EVP_PKEY_encrypt (PkeyCtx,
NULL, &OutDataSize, InData, InDataSize) <= 0) {
180 if (OutData ==
NULL) {
190 if (EVP_PKEY_encrypt (PkeyCtx, OutData, &OutDataSize, InData, InDataSize) <= 0) {
203 *EncryptedData = OutData;
204 *EncryptedDataSize = OutDataSize;
211 if (PkeyCtx !=
NULL) {
212 EVP_PKEY_CTX_free (PkeyCtx);
254 IN CONST UINT8 *PrngSeed OPTIONAL,
255 IN UINTN PrngSeedSize OPTIONAL,
256 OUT UINT8 **EncryptedData,
261 CONST UINT8 *TempPointer;
268 if ((PublicKey ==
NULL) || (InData ==
NULL) ||
269 (EncryptedData ==
NULL) || (EncryptedDataSize ==
NULL))
277 if (PublicKeySize > 0xFFFFFFFF) {
284 *EncryptedData =
NULL;
285 *EncryptedDataSize = 0;
294 TempPointer = PublicKey;
295 CertData = d2i_X509 (&CertData, &TempPointer, (UINT32)PublicKeySize);
296 if (CertData ==
NULL) {
307 Pkey = X509_get_pubkey (CertData);
315 Result =
InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, 0, EncryptedData, EncryptedDataSize);
321 if (CertData !=
NULL) {
322 X509_free (CertData);
326 EVP_PKEY_free (Pkey);
370 IN CONST UINT8 *PrngSeed OPTIONAL,
371 IN UINTN PrngSeedSize OPTIONAL,
372 IN UINT16 DigestLen OPTIONAL,
373 OUT UINT8 **EncryptedData,
383 if (((RsaContext ==
NULL) || (InData ==
NULL)) ||
384 (EncryptedData ==
NULL) || (EncryptedDataSize ==
NULL))
389 *EncryptedData =
NULL;
390 *EncryptedDataSize = 0;
394 Pkey = EVP_PKEY_new ();
399 if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) {
403 Result =
InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize);
410 EVP_PKEY_free (Pkey);
446 IN UINT8 *EncryptedData,
448 IN UINT16 DigestLen OPTIONAL,
454 EVP_PKEY_CTX *PkeyCtx;
458 CONST EVP_MD *HashAlg;
463 if ((Pkey ==
NULL) || (EncryptedData ==
NULL) ||
464 (OutData ==
NULL) || (OutDataSize ==
NULL))
477 PkeyCtx = EVP_PKEY_CTX_new (Pkey,
NULL);
478 if (PkeyCtx ==
NULL) {
482 DEBUG ((DEBUG_ERROR,
"[%a] EVP_PKEY_CTK_new() failed\n", __func__));
489 if ((EVP_PKEY_decrypt_init (PkeyCtx) <= 0) ||
490 (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0))
495 DEBUG ((DEBUG_ERROR,
"[%a] EVP_PKEY_decrypt_init() failed\n", __func__));
499 if (DigestLen != 0) {
501 if (HashAlg ==
NULL) {
505 if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) {
509 if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) {
517 ReturnCode = EVP_PKEY_decrypt (PkeyCtx,
NULL, &TempDataSize, EncryptedData, EncryptedDataSize);
518 if (ReturnCode <= 0) {
522 DEBUG ((DEBUG_ERROR,
"[%a] EVP_PKEY_decrypt() failed to determine output buffer size (rc=%d)\n", __func__, ReturnCode));
530 if (TempData ==
NULL) {
540 ReturnCode = EVP_PKEY_decrypt (PkeyCtx, TempData, &TempDataSize, EncryptedData, EncryptedDataSize);
541 if (ReturnCode <= 0) {
549 DEBUG ((DEBUG_ERROR,
"[%a] EVP_PKEY_decrypt(TempData) failed to decrypt (rc=%d)\n", __func__, ReturnCode));
557 *OutDataSize = TempDataSize;
561 if (PkeyCtx !=
NULL) {
562 EVP_PKEY_CTX_free (PkeyCtx);
594 IN UINT8 *EncryptedData,
602 CONST UINT8 *TempPointer;
607 if ((PrivateKey ==
NULL) || (EncryptedData ==
NULL) ||
608 (OutData ==
NULL) || (OutDataSize ==
NULL))
620 TempPointer = PrivateKey;
621 Pkey = d2i_PrivateKey (EVP_PKEY_RSA, &Pkey, &TempPointer, (UINT32)PrivateKeySize);
626 DEBUG ((DEBUG_ERROR,
"[%a] d2i_PrivateKey() failed\n", __func__));
634 EVP_PKEY_free (Pkey);
671 IN UINT8 *EncryptedData,
673 IN UINT16 DigestLen OPTIONAL,
684 if ((RsaContext ==
NULL) || (EncryptedData ==
NULL) ||
685 (OutData ==
NULL) || (OutDataSize ==
NULL))
697 Pkey = EVP_PKEY_new ();
702 if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) {
706 Result =
InternalPkcs1v2Decrypt (Pkey, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize);
710 EVP_PKEY_free (Pkey);
#define SHA512_DIGEST_SIZE
#define SHA256_DIGEST_SIZE
BOOLEAN EFIAPI RandomSeed(IN CONST UINT8 *Seed OPTIONAL, IN UINTN SeedSize)
#define SHA384_DIGEST_SIZE
VOID EFIAPI FreePool(IN VOID *Buffer)
#define DEBUG(Expression)
BOOLEAN EFIAPI InternalPkcs1v2Decrypt(EVP_PKEY *Pkey, IN UINT8 *EncryptedData, IN UINTN EncryptedDataSize, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **OutData, OUT UINTN *OutDataSize)
BOOLEAN EFIAPI Pkcs1v2Encrypt(IN CONST UINT8 *PublicKey, IN UINTN PublicKeySize, IN UINT8 *InData, IN UINTN InDataSize, IN CONST UINT8 *PrngSeed OPTIONAL, IN UINTN PrngSeedSize OPTIONAL, OUT UINT8 **EncryptedData, OUT UINTN *EncryptedDataSize)
BOOLEAN EFIAPI RsaOaepEncrypt(IN VOID *RsaContext, IN UINT8 *InData, IN UINTN InDataSize, IN CONST UINT8 *PrngSeed OPTIONAL, IN UINTN PrngSeedSize OPTIONAL, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **EncryptedData, OUT UINTN *EncryptedDataSize)
BOOLEAN EFIAPI RsaOaepDecrypt(IN VOID *RsaContext, IN UINT8 *EncryptedData, IN UINTN EncryptedDataSize, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **OutData, OUT UINTN *OutDataSize)
BOOLEAN EFIAPI InternalPkcs1v2Encrypt(EVP_PKEY *Pkey, IN UINT8 *InData, IN UINTN InDataSize, IN CONST UINT8 *PrngSeed OPTIONAL, IN UINTN PrngSeedSize OPTIONAL, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **EncryptedData, OUT UINTN *EncryptedDataSize)
BOOLEAN EFIAPI Pkcs1v2Decrypt(IN CONST UINT8 *PrivateKey, IN UINTN PrivateKeySize, IN UINT8 *EncryptedData, IN UINTN EncryptedDataSize, OUT UINT8 **OutData, OUT UINTN *OutDataSize)
STATIC const EVP_MD * GetEvpMD(IN UINT16 DigestLen)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)