Changeset 105670 in vbox for trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg
- Timestamp:
- Aug 14, 2024 1:16:30 PM (6 months ago)
- svn:sync-xref-src-repo-rev:
- 164367
- Location:
- trunk/src/VBox/Devices/EFI/FirmwareNew
- Files:
-
- 78 added
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/EFI/FirmwareNew
-
Property svn:mergeinfo
changed from (toggle deleted branches)
to (toggle deleted branches)/vendor/edk2/current 103735-103757,103769-103776,129194-159268 /vendor/edk2/current 103735-103757,103769-103776,129194-164365
-
Property svn:mergeinfo
changed from (toggle deleted branches)
-
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/CryptoPkg.ci.yaml
r101291 r105670 7 7 ## 8 8 { 9 "PrEval": { 10 "DscPath": "CryptoPkg.dsc", 11 }, 9 12 "LicenseCheck": { 10 13 "IgnoreFiles": [ … … 39 42 "Library/BaseCryptLib/Hash/CryptParallelHash.h", 40 43 "Library/Include/fcntl.h", 44 # This has Mbedtls interfaces that aren't UEFI spec compliant 45 "Library/Include/stdint.h", 46 "Library/Include/stubs-32.h", 47 "Library/Include/inttypes.h", 41 48 # These directories contain auto-generated OpenSSL content 42 49 "Library/OpensslLib", 43 50 "Library/IntrinsicLib", 44 "Library/BaseCryptLib/Pk/CryptPkcs7VerifyBase.c" 51 "Library/BaseCryptLib/Pk/CryptPkcs7VerifyBase.c", 52 # mbedtls 53 "Library/MbedTlsLib/mbedtls", 54 # This has mbedtls interfaces that aren't UEFI spec compliant 55 "Library/MbedTlsLib/EcSm2Null.c", 56 "Library/MbedTlsLib/CrtWrapper.c", 57 "Library/MbedTlsLib/Include/mbedtls/mbedtls_config.h", 58 "Library/BaseCryptLibMbedTls/SysCall" 45 59 ] 46 60 }, -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/CryptoPkg.dec
r101291 r105670 28 28 Library/OpensslLib/OpensslGen/include 29 29 Library/OpensslLib/OpensslGen/providers/common/include 30 # Library/MbedTlsLib/Include 31 # Library/MbedTlsLib/mbedtls/include 32 # Library/MbedTlsLib/mbedtls/include/mbedtls 33 # Library/MbedTlsLib/mbedtls/library 30 34 31 35 [LibraryClasses] … … 46 50 # 47 51 OpensslLib|Private/Library/OpensslLib.h 52 MbedTlsLib|Private/Library/MbedTlsLib.h 48 53 49 54 ## @libraryclass Provides compiler intrinsic functions required to link openssl project. -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/CryptoPkg.dsc
r101291 r105670 365 365 CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf 366 366 CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf 367 CryptoPkg/Library/BaseCryptLibMbedTls/BaseCryptLib.inf 368 CryptoPkg/Library/BaseCryptLibMbedTls/SecCryptLib.inf 369 CryptoPkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf 370 CryptoPkg/Library/BaseCryptLibMbedTls/SmmCryptLib.inf 371 CryptoPkg/Library/BaseCryptLibMbedTls/RuntimeCryptLib.inf 372 CryptoPkg/Library/BaseCryptLibMbedTls/TestBaseCryptLib.inf 373 CryptoPkg/Library/MbedTlsLib/MbedTlsLib.inf 374 CryptoPkg/Library/MbedTlsLib/MbedTlsLibFull.inf 367 375 CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf 368 376 CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Driver/Crypto.c
r99404 r105670 3588 3588 { 3589 3589 return CALL_BASECRYPTLIB (Pkcs.Services.Pkcs1v2Encrypt, Pkcs1v2Encrypt, (PublicKey, PublicKeySize, InData, InDataSize, PrngSeed, PrngSeedSize, EncryptedData, EncryptedDataSize), FALSE); 3590 } 3591 3592 /** 3593 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 3594 encrypted message in a newly allocated buffer. 3595 3596 Things that can cause a failure include: 3597 - X509 key size does not match any known key size. 3598 - Fail to allocate an intermediate buffer. 3599 - Null pointer provided for a non-optional parameter. 3600 - Data size is too large for the provided key size (max size is a function of key size 3601 and hash digest size). 3602 3603 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 3604 provisioned with a public key using RsaSetKey(). 3605 @param[in] InData Data to be encrypted. 3606 @param[in] InDataSize Size of the data buffer. 3607 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 3608 to be used when initializing the PRNG. NULL otherwise. 3609 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 3610 0 otherwise. 3611 @param[in] DigestLen [Optional] If provided, size of the hash used: 3612 SHA1_DIGEST_SIZE 3613 SHA256_DIGEST_SIZE 3614 SHA384_DIGEST_SIZE 3615 SHA512_DIGEST_SIZE 3616 0 to use default (SHA1) 3617 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 3618 message. 3619 @param[out] EncryptedDataSize Size of the encrypted message buffer. 3620 3621 @retval TRUE Encryption was successful. 3622 @retval FALSE Encryption failed. 3623 3624 **/ 3625 BOOLEAN 3626 EFIAPI 3627 CryptoServiceRsaOaepEncrypt ( 3628 IN VOID *RsaContext, 3629 IN UINT8 *InData, 3630 IN UINTN InDataSize, 3631 IN CONST UINT8 *PrngSeed OPTIONAL, 3632 IN UINTN PrngSeedSize OPTIONAL, 3633 IN UINT16 DigestLen OPTIONAL, 3634 OUT UINT8 **EncryptedData, 3635 OUT UINTN *EncryptedDataSize 3636 ) 3637 { 3638 return CALL_BASECRYPTLIB (Rsa.Services.RsaOaepEncrypt, RsaOaepEncrypt, (RsaContext, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize), FALSE); 3639 } 3640 3641 /** 3642 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 3643 decrypted message in a newly allocated buffer. 3644 3645 Things that can cause a failure include: 3646 - Fail to parse private key. 3647 - Fail to allocate an intermediate buffer. 3648 - Null pointer provided for a non-optional parameter. 3649 3650 @param[in] PrivateKey A pointer to the DER-encoded private key. 3651 @param[in] PrivateKeySize Size of the private key buffer. 3652 @param[in] EncryptedData Data to be decrypted. 3653 @param[in] EncryptedDataSize Size of the encrypted buffer. 3654 @param[out] OutData Pointer to an allocated buffer containing the encrypted 3655 message. 3656 @param[out] OutDataSize Size of the encrypted message buffer. 3657 3658 @retval TRUE Encryption was successful. 3659 @retval FALSE Encryption failed. 3660 3661 **/ 3662 BOOLEAN 3663 EFIAPI 3664 CryptoServicePkcs1v2Decrypt ( 3665 IN CONST UINT8 *PrivateKey, 3666 IN UINTN PrivateKeySize, 3667 IN UINT8 *EncryptedData, 3668 IN UINTN EncryptedDataSize, 3669 OUT UINT8 **OutData, 3670 OUT UINTN *OutDataSize 3671 ) 3672 { 3673 return CALL_BASECRYPTLIB (Pkcs.Services.Pkcs1v2Decrypt, Pkcs1v2Decrypt, (PrivateKey, PrivateKeySize, EncryptedData, EncryptedDataSize, OutData, OutDataSize), FALSE); 3674 } 3675 3676 /** 3677 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 3678 decrypted message in a newly allocated buffer. 3679 3680 Things that can cause a failure include: 3681 - Fail to parse private key. 3682 - Fail to allocate an intermediate buffer. 3683 - Null pointer provided for a non-optional parameter. 3684 3685 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 3686 provisioned with a private key using RsaSetKey(). 3687 @param[in] EncryptedData Data to be decrypted. 3688 @param[in] EncryptedDataSize Size of the encrypted buffer. 3689 @param[in] DigestLen [Optional] If provided, size of the hash used: 3690 SHA1_DIGEST_SIZE 3691 SHA256_DIGEST_SIZE 3692 SHA384_DIGEST_SIZE 3693 SHA512_DIGEST_SIZE 3694 0 to use default (SHA1) 3695 @param[out] OutData Pointer to an allocated buffer containing the encrypted 3696 message. 3697 @param[out] OutDataSize Size of the encrypted message buffer. 3698 3699 @retval TRUE Encryption was successful. 3700 @retval FALSE Encryption failed. 3701 3702 **/ 3703 BOOLEAN 3704 EFIAPI 3705 CryptoServiceRsaOaepDecrypt ( 3706 IN VOID *RsaContext, 3707 IN UINT8 *EncryptedData, 3708 IN UINTN EncryptedDataSize, 3709 IN UINT16 DigestLen OPTIONAL, 3710 OUT UINT8 **OutData, 3711 OUT UINTN *OutDataSize 3712 ) 3713 { 3714 return CALL_BASECRYPTLIB (Rsa.Services.RsaOaepDecrypt, RsaOaepDecrypt, (RsaContext, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize), FALSE); 3590 3715 } 3591 3716 … … 6988 7113 CryptoServiceX509GetCertFromCertChain, 6989 7114 CryptoServiceAsn1GetTag, 6990 CryptoServiceX509GetExtendedBasicConstraints 7115 CryptoServiceX509GetExtendedBasicConstraints, 7116 CryptoServicePkcs1v2Decrypt, 7117 CryptoServiceRsaOaepEncrypt, 7118 CryptoServiceRsaOaepDecrypt, 6991 7119 }; -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Include/Library/BaseCryptLib.h
r99404 r105670 6 6 7 7 Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR> 8 Copyright (c) Microsoft Corporation. All rights reserved. 8 9 SPDX-License-Identifier: BSD-2-Clause-Patent 9 10 … … 2146 2147 OUT UINT8 **EncryptedData, 2147 2148 OUT UINTN *EncryptedDataSize 2149 ); 2150 2151 /** 2152 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2153 encrypted message in a newly allocated buffer. 2154 2155 Things that can cause a failure include: 2156 - X509 key size does not match any known key size. 2157 - Fail to allocate an intermediate buffer. 2158 - Null pointer provided for a non-optional parameter. 2159 - Data size is too large for the provided key size (max size is a function of key size 2160 and hash digest size). 2161 2162 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 2163 provisioned with a public key using RsaSetKey(). 2164 @param[in] InData Data to be encrypted. 2165 @param[in] InDataSize Size of the data buffer. 2166 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 2167 to be used when initializing the PRNG. NULL otherwise. 2168 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 2169 0 otherwise. 2170 @param[in] DigestLen [Optional] If provided, size of the hash used: 2171 SHA1_DIGEST_SIZE 2172 SHA256_DIGEST_SIZE 2173 SHA384_DIGEST_SIZE 2174 SHA512_DIGEST_SIZE 2175 0 to use default (SHA1) 2176 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 2177 message. 2178 @param[out] EncryptedDataSize Size of the encrypted message buffer. 2179 2180 @retval TRUE Encryption was successful. 2181 @retval FALSE Encryption failed. 2182 2183 **/ 2184 BOOLEAN 2185 EFIAPI 2186 RsaOaepEncrypt ( 2187 IN VOID *RsaContext, 2188 IN UINT8 *InData, 2189 IN UINTN InDataSize, 2190 IN CONST UINT8 *PrngSeed OPTIONAL, 2191 IN UINTN PrngSeedSize OPTIONAL, 2192 IN UINT16 DigestLen OPTIONAL, 2193 OUT UINT8 **EncryptedData, 2194 OUT UINTN *EncryptedDataSize 2195 ); 2196 2197 /** 2198 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2199 decrypted message in a newly allocated buffer. 2200 2201 Things that can cause a failure include: 2202 - Fail to parse private key. 2203 - Fail to allocate an intermediate buffer. 2204 - Null pointer provided for a non-optional parameter. 2205 2206 @param[in] PrivateKey A pointer to the DER-encoded private key. 2207 @param[in] PrivateKeySize Size of the private key buffer. 2208 @param[in] EncryptedData Data to be decrypted. 2209 @param[in] EncryptedDataSize Size of the encrypted buffer. 2210 @param[out] OutData Pointer to an allocated buffer containing the encrypted 2211 message. 2212 @param[out] OutDataSize Size of the encrypted message buffer. 2213 2214 @retval TRUE Encryption was successful. 2215 @retval FALSE Encryption failed. 2216 2217 **/ 2218 BOOLEAN 2219 EFIAPI 2220 Pkcs1v2Decrypt ( 2221 IN CONST UINT8 *PrivateKey, 2222 IN UINTN PrivateKeySize, 2223 IN UINT8 *EncryptedData, 2224 IN UINTN EncryptedDataSize, 2225 OUT UINT8 **OutData, 2226 OUT UINTN *OutDataSize 2227 ); 2228 2229 /** 2230 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2231 decrypted message in a newly allocated buffer. 2232 2233 Things that can cause a failure include: 2234 - Fail to parse private key. 2235 - Fail to allocate an intermediate buffer. 2236 - Null pointer provided for a non-optional parameter. 2237 2238 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 2239 provisioned with a private key using RsaSetKey(). 2240 @param[in] EncryptedData Data to be decrypted. 2241 @param[in] EncryptedDataSize Size of the encrypted buffer. 2242 @param[in] DigestLen [Optional] If provided, size of the hash used: 2243 SHA1_DIGEST_SIZE 2244 SHA256_DIGEST_SIZE 2245 SHA384_DIGEST_SIZE 2246 SHA512_DIGEST_SIZE 2247 0 to use default (SHA1) 2248 @param[out] OutData Pointer to an allocated buffer containing the encrypted 2249 message. 2250 @param[out] OutDataSize Size of the encrypted message buffer. 2251 2252 @retval TRUE Encryption was successful. 2253 @retval FALSE Encryption failed. 2254 2255 **/ 2256 BOOLEAN 2257 EFIAPI 2258 RsaOaepDecrypt ( 2259 IN VOID *RsaContext, 2260 IN UINT8 *EncryptedData, 2261 IN UINTN EncryptedDataSize, 2262 IN UINT16 DigestLen OPTIONAL, 2263 OUT UINT8 **OutData, 2264 OUT UINTN *OutDataSize 2148 2265 ); 2149 2266 -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h
r99404 r105670 24 24 25 25 Copyright (c) 2019 - 2022, Intel Corporation. All rights reserved.<BR> 26 Copyright (c) Microsoft Corporation. All rights reserved. 26 27 SPDX-License-Identifier: BSD-2-Clause-Patent 27 28 … … 125 126 UINT8 AuthenticodeVerify : 1; 126 127 UINT8 ImageTimestampVerify : 1; 128 UINT8 Pkcs1v2Decrypt : 1; 127 129 } Services; 128 130 UINT32 Family; … … 159 161 UINT8 GetPrivateKeyFromPem : 1; 160 162 UINT8 GetPublicKeyFromX509 : 1; 163 UINT8 RsaOaepEncrypt : 1; 164 UINT8 RsaOaepDecrypt : 1; 161 165 } Services; 162 166 UINT32 Family; -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs1Oaep.c
r99404 r105670 4 4 SPDX-License-Identifier: BSD-2-Clause-Patent 5 5 6 Copyright (C) 2016Microsoft Corporation. All Rights Reserved.6 Copyright (C) Microsoft Corporation. All Rights Reserved. 7 7 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> 8 8 … … 14 14 #include <openssl/x509.h> 15 15 #include <Library/MemoryAllocationLib.h> 16 17 /** 18 Retrieve a pointer to EVP message digest object. 19 20 @param[in] DigestLen Length of the message digest. 21 22 **/ 23 STATIC 24 const 25 EVP_MD * 26 GetEvpMD ( 27 IN UINT16 DigestLen 28 ) 29 { 30 switch (DigestLen) { 31 case SHA1_DIGEST_SIZE: 32 return EVP_sha1 (); 33 break; 34 case SHA256_DIGEST_SIZE: 35 return EVP_sha256 (); 36 break; 37 case SHA384_DIGEST_SIZE: 38 return EVP_sha384 (); 39 break; 40 case SHA512_DIGEST_SIZE: 41 return EVP_sha512 (); 42 break; 43 default: 44 return NULL; 45 } 46 } 47 48 /** 49 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 50 encrypted message in a newly allocated buffer. 51 52 Things that can cause a failure include: 53 - X509 key size does not match any known key size. 54 - Fail to parse X509 certificate. 55 - Fail to allocate an intermediate buffer. 56 - Null pointer provided for a non-optional parameter. 57 - Data size is too large for the provided key size (max size is a function of key size 58 and hash digest size). 59 60 @param[in] Pkey A pointer to an EVP_PKEY struct that 61 will be used to encrypt the data. 62 @param[in] InData Data to be encrypted. 63 @param[in] InDataSize Size of the data buffer. 64 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 65 to be used when initializing the PRNG. NULL otherwise. 66 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 67 0 otherwise. 68 @param[in] DigestLen [Optional] If provided, size of the hash used: 69 SHA1_DIGEST_SIZE 70 SHA256_DIGEST_SIZE 71 SHA384_DIGEST_SIZE 72 SHA512_DIGEST_SIZE 73 0 to use default (SHA1) 74 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 75 message. 76 @param[out] EncryptedDataSize Size of the encrypted message buffer. 77 78 @retval TRUE Encryption was successful. 79 @retval FALSE Encryption failed. 80 81 **/ 82 BOOLEAN 83 EFIAPI 84 InternalPkcs1v2Encrypt ( 85 EVP_PKEY *Pkey, 86 IN UINT8 *InData, 87 IN UINTN InDataSize, 88 IN CONST UINT8 *PrngSeed OPTIONAL, 89 IN UINTN PrngSeedSize OPTIONAL, 90 IN UINT16 DigestLen OPTIONAL, 91 OUT UINT8 **EncryptedData, 92 OUT UINTN *EncryptedDataSize 93 ) 94 { 95 BOOLEAN Result; 96 EVP_PKEY_CTX *PkeyCtx; 97 UINT8 *OutData; 98 UINTN OutDataSize; 99 CONST EVP_MD *HashAlg; 100 101 // 102 // Check input parameters. 103 // 104 if ((Pkey == NULL) || (InData == NULL) || 105 (EncryptedData == NULL) || (EncryptedDataSize == NULL)) 106 { 107 return FALSE; 108 } 109 110 *EncryptedData = NULL; 111 *EncryptedDataSize = 0; 112 Result = FALSE; 113 PkeyCtx = NULL; 114 OutData = NULL; 115 OutDataSize = 0; 116 117 // 118 // If it provides a seed then use it. 119 // Ohterwise, we'll seed with fixed values and hope that the PRNG has already been 120 // used enough to generate sufficient entropy. 121 // 122 if (PrngSeed != NULL) { 123 RandomSeed (PrngSeed, PrngSeedSize); 124 } else { 125 RandomSeed (NULL, 0); 126 } 127 128 // 129 // Create a context for the public key operation. 130 // 131 PkeyCtx = EVP_PKEY_CTX_new (Pkey, NULL); 132 if (PkeyCtx == NULL) { 133 // 134 // Fail to create contex. 135 // 136 goto _Exit; 137 } 138 139 // 140 // Initialize the context and set the desired padding. 141 // 142 if ((EVP_PKEY_encrypt_init (PkeyCtx) <= 0) || 143 (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0)) 144 { 145 // 146 // Fail to initialize the context. 147 // 148 goto _Exit; 149 } 150 151 if (DigestLen != 0) { 152 HashAlg = GetEvpMD (DigestLen); 153 if (HashAlg == NULL) { 154 goto _Exit; 155 } 156 157 if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) { 158 goto _Exit; 159 } 160 161 if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) { 162 goto _Exit; 163 } 164 } 165 166 // 167 // Determine the required buffer length for malloc'ing. 168 // 169 if (EVP_PKEY_encrypt (PkeyCtx, NULL, &OutDataSize, InData, InDataSize) <= 0) { 170 // 171 // Fail to determine output buffer size. 172 // 173 goto _Exit; 174 } 175 176 // 177 // Allocate a buffer for the output data. 178 // 179 OutData = AllocatePool (OutDataSize); 180 if (OutData == NULL) { 181 // 182 // Fail to allocate the output buffer. 183 // 184 goto _Exit; 185 } 186 187 // 188 // Encrypt Data. 189 // 190 if (EVP_PKEY_encrypt (PkeyCtx, OutData, &OutDataSize, InData, InDataSize) <= 0) { 191 // 192 // Fail to encrypt data, need to free the output buffer. 193 // 194 FreePool (OutData); 195 OutData = NULL; 196 OutDataSize = 0; 197 goto _Exit; 198 } 199 200 // 201 // Encrypt done. 202 // 203 *EncryptedData = OutData; 204 *EncryptedDataSize = OutDataSize; 205 Result = TRUE; 206 207 _Exit: 208 // 209 // Release Resources 210 // 211 if (PkeyCtx != NULL) { 212 EVP_PKEY_CTX_free (PkeyCtx); 213 } 214 215 return Result; 216 } 16 217 17 218 /** … … 57 258 ) 58 259 { 59 BOOLEAN Result; 60 CONST UINT8 *TempPointer; 61 X509 *CertData; 62 EVP_PKEY *InternalPublicKey; 63 EVP_PKEY_CTX *PkeyCtx; 64 UINT8 *OutData; 65 UINTN OutDataSize; 260 BOOLEAN Result; 261 CONST UINT8 *TempPointer; 262 X509 *CertData; 263 EVP_PKEY *Pkey; 66 264 67 265 // … … 89 287 TempPointer = NULL; 90 288 CertData = NULL; 91 InternalPublicKey = NULL; 92 PkeyCtx = NULL; 93 OutData = NULL; 94 OutDataSize = 0; 95 96 // 97 // If it provides a seed then use it. 98 // Ohterwise, we'll seed with fixed values and hope that the PRNG has already been 99 // used enough to generate sufficient entropy. 100 // 101 if (PrngSeed != NULL) { 102 RandomSeed (PrngSeed, PrngSeedSize); 103 } else { 104 RandomSeed (NULL, 0); 105 } 289 Pkey = NULL; 106 290 107 291 // … … 121 305 // OpenSSL can use. 122 306 // 123 InternalPublicKey = X509_get_pubkey (CertData);124 if ( InternalPublicKey == NULL) {307 Pkey = X509_get_pubkey (CertData); 308 if (Pkey == NULL) { 125 309 // 126 310 // Fail to extract public key. … … 129 313 } 130 314 131 // 132 // Create a context for the public key operation. 133 // 134 PkeyCtx = EVP_PKEY_CTX_new (InternalPublicKey, NULL); 315 Result = InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, 0, EncryptedData, EncryptedDataSize); 316 317 _Exit: 318 // 319 // Release Resources 320 // 321 if (CertData != NULL) { 322 X509_free (CertData); 323 } 324 325 if (Pkey != NULL) { 326 EVP_PKEY_free (Pkey); 327 } 328 329 return Result; 330 } 331 332 /** 333 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 334 encrypted message in a newly allocated buffer. 335 336 Things that can cause a failure include: 337 - Fail to allocate an intermediate buffer. 338 - Null pointer provided for a non-optional parameter. 339 - Data size is too large for the provided key size (max size is a function of key size 340 and hash digest size). 341 342 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 343 provisioned with a public key using RsaSetKey(). 344 @param[in] InData Data to be encrypted. 345 @param[in] InDataSize Size of the data buffer. 346 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 347 to be used when initializing the PRNG. NULL otherwise. 348 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 349 0 otherwise. 350 @param[in] DigestLen [Optional] If provided, size of the hash used: 351 SHA1_DIGEST_SIZE 352 SHA256_DIGEST_SIZE 353 SHA384_DIGEST_SIZE 354 SHA512_DIGEST_SIZE 355 0 to use default (SHA1) 356 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 357 message. 358 @param[out] EncryptedDataSize Size of the encrypted message buffer. 359 360 @retval TRUE Encryption was successful. 361 @retval FALSE Encryption failed. 362 363 **/ 364 BOOLEAN 365 EFIAPI 366 RsaOaepEncrypt ( 367 IN VOID *RsaContext, 368 IN UINT8 *InData, 369 IN UINTN InDataSize, 370 IN CONST UINT8 *PrngSeed OPTIONAL, 371 IN UINTN PrngSeedSize OPTIONAL, 372 IN UINT16 DigestLen OPTIONAL, 373 OUT UINT8 **EncryptedData, 374 OUT UINTN *EncryptedDataSize 375 ) 376 { 377 BOOLEAN Result; 378 EVP_PKEY *Pkey; 379 380 // 381 // Check input parameters. 382 // 383 if (((RsaContext == NULL) || (InData == NULL)) || 384 (EncryptedData == NULL) || (EncryptedDataSize == NULL)) 385 { 386 return FALSE; 387 } 388 389 *EncryptedData = NULL; 390 *EncryptedDataSize = 0; 391 Result = FALSE; 392 Pkey = NULL; 393 394 Pkey = EVP_PKEY_new (); 395 if (Pkey == NULL) { 396 goto _Exit; 397 } 398 399 if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) { 400 goto _Exit; 401 } 402 403 Result = InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize); 404 405 _Exit: 406 // 407 // Release Resources 408 // 409 if (Pkey != NULL) { 410 EVP_PKEY_free (Pkey); 411 } 412 413 return Result; 414 } 415 416 /** 417 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 418 decrypted message in a newly allocated buffer. 419 420 Things that can cause a failure include: 421 - Fail to parse private key. 422 - Fail to allocate an intermediate buffer. 423 - Null pointer provided for a non-optional parameter. 424 425 @param[in] Pkey A pointer to an EVP_PKEY which will decrypt that data. 426 @param[in] EncryptedData Data to be decrypted. 427 @param[in] EncryptedDataSize Size of the encrypted buffer. 428 @param[in] DigestLen [Optional] If provided, size of the hash used: 429 SHA1_DIGEST_SIZE 430 SHA256_DIGEST_SIZE 431 SHA384_DIGEST_SIZE 432 SHA512_DIGEST_SIZE 433 0 to use default (SHA1) 434 @param[out] OutData Pointer to an allocated buffer containing the encrypted 435 message. 436 @param[out] OutDataSize Size of the encrypted message buffer. 437 438 @retval TRUE Encryption was successful. 439 @retval FALSE Encryption failed. 440 441 **/ 442 BOOLEAN 443 EFIAPI 444 InternalPkcs1v2Decrypt ( 445 EVP_PKEY *Pkey, 446 IN UINT8 *EncryptedData, 447 IN UINTN EncryptedDataSize, 448 IN UINT16 DigestLen OPTIONAL, 449 OUT UINT8 **OutData, 450 OUT UINTN *OutDataSize 451 ) 452 { 453 BOOLEAN Result; 454 EVP_PKEY_CTX *PkeyCtx; 455 UINT8 *TempData; 456 UINTN TempDataSize; 457 INTN ReturnCode; 458 CONST EVP_MD *HashAlg; 459 460 // 461 // Check input parameters. 462 // 463 if ((Pkey == NULL) || (EncryptedData == NULL) || 464 (OutData == NULL) || (OutDataSize == NULL)) 465 { 466 return FALSE; 467 } 468 469 Result = FALSE; 470 PkeyCtx = NULL; 471 TempData = NULL; 472 TempDataSize = 0; 473 474 // 475 // Create a context for the decryption operation. 476 // 477 PkeyCtx = EVP_PKEY_CTX_new (Pkey, NULL); 135 478 if (PkeyCtx == NULL) { 136 479 // 137 480 // Fail to create contex. 138 481 // 482 DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_CTK_new() failed\n", __func__)); 139 483 goto _Exit; 140 484 } … … 143 487 // Initialize the context and set the desired padding. 144 488 // 145 if ((EVP_PKEY_ encrypt_init (PkeyCtx) <= 0) ||489 if ((EVP_PKEY_decrypt_init (PkeyCtx) <= 0) || 146 490 (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0)) 147 491 { … … 149 493 // Fail to initialize the context. 150 494 // 151 goto _Exit; 495 DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt_init() failed\n", __func__)); 496 goto _Exit; 497 } 498 499 if (DigestLen != 0) { 500 HashAlg = GetEvpMD (DigestLen); 501 if (HashAlg == NULL) { 502 goto _Exit; 503 } 504 505 if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) { 506 goto _Exit; 507 } 508 509 if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) { 510 goto _Exit; 511 } 152 512 } 153 513 … … 155 515 // Determine the required buffer length for malloc'ing. 156 516 // 157 if (EVP_PKEY_encrypt (PkeyCtx, NULL, &OutDataSize, InData, InDataSize) <= 0) { 517 ReturnCode = EVP_PKEY_decrypt (PkeyCtx, NULL, &TempDataSize, EncryptedData, EncryptedDataSize); 518 if (ReturnCode <= 0) { 158 519 // 159 520 // Fail to determine output buffer size. 160 521 // 522 DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt() failed to determine output buffer size (rc=%d)\n", __func__, ReturnCode)); 161 523 goto _Exit; 162 524 } … … 165 527 // Allocate a buffer for the output data. 166 528 // 167 OutData = AllocatePool (OutDataSize);168 if ( OutData == NULL) {529 TempData = AllocatePool (TempDataSize); 530 if (TempData == NULL) { 169 531 // 170 532 // Fail to allocate the output buffer. … … 174 536 175 537 // 176 // Encrypt Data. 177 // 178 if (EVP_PKEY_encrypt (PkeyCtx, OutData, &OutDataSize, InData, InDataSize) <= 0) { 179 // 180 // Fail to encrypt data, need to free the output buffer. 181 // 182 FreePool (OutData); 183 OutData = NULL; 184 OutDataSize = 0; 185 goto _Exit; 186 } 187 188 // 189 // Encrypt done. 190 // 191 *EncryptedData = OutData; 192 *EncryptedDataSize = OutDataSize; 193 Result = TRUE; 538 // Decrypt Data. 539 // 540 ReturnCode = EVP_PKEY_decrypt (PkeyCtx, TempData, &TempDataSize, EncryptedData, EncryptedDataSize); 541 if (ReturnCode <= 0) { 542 // 543 // Fail to decrypt data, need to free the output buffer. 544 // 545 FreePool (TempData); 546 TempData = NULL; 547 TempDataSize = 0; 548 549 DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt(TempData) failed to decrypt (rc=%d)\n", __func__, ReturnCode)); 550 goto _Exit; 551 } 552 553 // 554 // Decrypt done. 555 // 556 *OutData = TempData; 557 *OutDataSize = TempDataSize; 558 Result = TRUE; 194 559 195 560 _Exit: 196 //197 // Release Resources198 //199 if (CertData != NULL) {200 X509_free (CertData);201 }202 203 if (InternalPublicKey != NULL) {204 EVP_PKEY_free (InternalPublicKey);205 }206 207 561 if (PkeyCtx != NULL) { 208 562 EVP_PKEY_CTX_free (PkeyCtx); … … 211 565 return Result; 212 566 } 567 568 /** 569 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 570 decrypted message in a newly allocated buffer. 571 572 Things that can cause a failure include: 573 - Fail to parse private key. 574 - Fail to allocate an intermediate buffer. 575 - Null pointer provided for a non-optional parameter. 576 577 @param[in] PrivateKey A pointer to the DER-encoded private key. 578 @param[in] PrivateKeySize Size of the private key buffer. 579 @param[in] EncryptedData Data to be decrypted. 580 @param[in] EncryptedDataSize Size of the encrypted buffer. 581 @param[out] OutData Pointer to an allocated buffer containing the encrypted 582 message. 583 @param[out] OutDataSize Size of the encrypted message buffer. 584 585 @retval TRUE Encryption was successful. 586 @retval FALSE Encryption failed. 587 588 **/ 589 BOOLEAN 590 EFIAPI 591 Pkcs1v2Decrypt ( 592 IN CONST UINT8 *PrivateKey, 593 IN UINTN PrivateKeySize, 594 IN UINT8 *EncryptedData, 595 IN UINTN EncryptedDataSize, 596 OUT UINT8 **OutData, 597 OUT UINTN *OutDataSize 598 ) 599 { 600 BOOLEAN Result; 601 EVP_PKEY *Pkey; 602 CONST UINT8 *TempPointer; 603 604 // 605 // Check input parameters. 606 // 607 if ((PrivateKey == NULL) || (EncryptedData == NULL) || 608 (OutData == NULL) || (OutDataSize == NULL)) 609 { 610 return FALSE; 611 } 612 613 Result = FALSE; 614 Pkey = NULL; 615 TempPointer = NULL; 616 617 // 618 // Parse the private key. 619 // 620 TempPointer = PrivateKey; 621 Pkey = d2i_PrivateKey (EVP_PKEY_RSA, &Pkey, &TempPointer, (UINT32)PrivateKeySize); 622 if (Pkey == NULL) { 623 // 624 // Fail to parse private key. 625 // 626 DEBUG ((DEBUG_ERROR, "[%a] d2i_PrivateKey() failed\n", __func__)); 627 goto _Exit; 628 } 629 630 Result = InternalPkcs1v2Decrypt (Pkey, EncryptedData, EncryptedDataSize, 0, OutData, OutDataSize); 631 632 _Exit: 633 if (Pkey != NULL) { 634 EVP_PKEY_free (Pkey); 635 } 636 637 return Result; 638 } 639 640 /** 641 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 642 decrypted message in a newly allocated buffer. 643 644 Things that can cause a failure include: 645 - Fail to parse private key. 646 - Fail to allocate an intermediate buffer. 647 - Null pointer provided for a non-optional parameter. 648 649 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 650 provisioned with a private key using RsaSetKey(). 651 @param[in] EncryptedData Data to be decrypted. 652 @param[in] EncryptedDataSize Size of the encrypted buffer. 653 @param[in] DigestLen [Optional] If provided, size of the hash used: 654 SHA1_DIGEST_SIZE 655 SHA256_DIGEST_SIZE 656 SHA384_DIGEST_SIZE 657 SHA512_DIGEST_SIZE 658 0 to use default (SHA1) 659 @param[out] OutData Pointer to an allocated buffer containing the encrypted 660 message. 661 @param[out] OutDataSize Size of the encrypted message buffer. 662 663 @retval TRUE Encryption was successful. 664 @retval FALSE Encryption failed. 665 666 **/ 667 BOOLEAN 668 EFIAPI 669 RsaOaepDecrypt ( 670 IN VOID *RsaContext, 671 IN UINT8 *EncryptedData, 672 IN UINTN EncryptedDataSize, 673 IN UINT16 DigestLen OPTIONAL, 674 OUT UINT8 **OutData, 675 OUT UINTN *OutDataSize 676 ) 677 { 678 BOOLEAN Result; 679 EVP_PKEY *Pkey; 680 681 // 682 // Check input parameters. 683 // 684 if ((RsaContext == NULL) || (EncryptedData == NULL) || 685 (OutData == NULL) || (OutDataSize == NULL)) 686 { 687 return FALSE; 688 } 689 690 Result = FALSE; 691 Pkey = NULL; 692 693 // 694 // Create a context for the decryption operation. 695 // 696 697 Pkey = EVP_PKEY_new (); 698 if (Pkey == NULL) { 699 goto _Exit; 700 } 701 702 if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) { 703 goto _Exit; 704 } 705 706 Result = InternalPkcs1v2Decrypt (Pkey, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize); 707 708 _Exit: 709 if (Pkey != NULL) { 710 EVP_PKEY_free (Pkey); 711 } 712 713 return Result; 714 } -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs1OaepNull.c
r99404 r105670 4 4 SPDX-License-Identifier: BSD-2-Clause-Patent 5 5 6 Copyright (C) 2016Microsoft Corporation. All Rights Reserved.6 Copyright (C) Microsoft Corporation. All Rights Reserved. 7 7 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> 8 8 … … 49 49 return FALSE; 50 50 } 51 52 /** 53 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 54 encrypted message in a newly allocated buffer. 55 56 Things that can cause a failure include: 57 - X509 key size does not match any known key size. 58 - Fail to allocate an intermediate buffer. 59 - Null pointer provided for a non-optional parameter. 60 - Data size is too large for the provided key size (max size is a function of key size 61 and hash digest size). 62 63 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 64 provisioned with a public key using RsaSetKey(). 65 @param[in] InData Data to be encrypted. 66 @param[in] InDataSize Size of the data buffer. 67 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 68 to be used when initializing the PRNG. NULL otherwise. 69 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 70 0 otherwise. 71 @param[in] DigestLen [Optional] If provided, size of the hash used: 72 SHA1_DIGEST_SIZE 73 SHA256_DIGEST_SIZE 74 SHA384_DIGEST_SIZE 75 SHA512_DIGEST_SIZE 76 0 to use default (SHA1) 77 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 78 message. 79 @param[out] EncryptedDataSize Size of the encrypted message buffer. 80 81 @retval TRUE Encryption was successful. 82 @retval FALSE Encryption failed. 83 84 **/ 85 BOOLEAN 86 EFIAPI 87 RsaOaepEncrypt ( 88 IN VOID *RsaContext, 89 IN UINT8 *InData, 90 IN UINTN InDataSize, 91 IN CONST UINT8 *PrngSeed OPTIONAL, 92 IN UINTN PrngSeedSize OPTIONAL, 93 IN UINT16 DigestLen OPTIONAL, 94 OUT UINT8 **EncryptedData, 95 OUT UINTN *EncryptedDataSize 96 ) 97 { 98 ASSERT (FALSE); 99 return FALSE; 100 } 101 102 /** 103 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 104 decrypted message in a newly allocated buffer. 105 106 Things that can cause a failure include: 107 - Fail to parse private key. 108 - Fail to allocate an intermediate buffer. 109 - Null pointer provided for a non-optional parameter. 110 111 @param[in] PrivateKey A pointer to the DER-encoded private key. 112 @param[in] PrivateKeySize Size of the private key buffer. 113 @param[in] EncryptedData Data to be decrypted. 114 @param[in] EncryptedDataSize Size of the encrypted buffer. 115 @param[out] OutData Pointer to an allocated buffer containing the encrypted 116 message. 117 @param[out] OutDataSize Size of the encrypted message buffer. 118 119 @retval TRUE Encryption was successful. 120 @retval FALSE Encryption failed. 121 122 **/ 123 BOOLEAN 124 EFIAPI 125 Pkcs1v2Decrypt ( 126 IN CONST UINT8 *PrivateKey, 127 IN UINTN PrivateKeySize, 128 IN UINT8 *EncryptedData, 129 IN UINTN EncryptedDataSize, 130 OUT UINT8 **OutData, 131 OUT UINTN *OutDataSize 132 ) 133 { 134 ASSERT (FALSE); 135 return FALSE; 136 } 137 138 /** 139 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 140 decrypted message in a newly allocated buffer. 141 142 Things that can cause a failure include: 143 - Fail to parse private key. 144 - Fail to allocate an intermediate buffer. 145 - Null pointer provided for a non-optional parameter. 146 147 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 148 provisioned with a private key using RsaSetKey(). 149 @param[in] EncryptedData Data to be decrypted. 150 @param[in] EncryptedDataSize Size of the encrypted buffer. 151 @param[in] DigestLen [Optional] If provided, size of the hash used: 152 SHA1_DIGEST_SIZE 153 SHA256_DIGEST_SIZE 154 SHA384_DIGEST_SIZE 155 SHA512_DIGEST_SIZE 156 0 to use default (SHA1) 157 @param[out] OutData Pointer to an allocated buffer containing the encrypted 158 message. 159 @param[out] OutDataSize Size of the encrypted message buffer. 160 161 @retval TRUE Encryption was successful. 162 @retval FALSE Encryption failed. 163 164 **/ 165 BOOLEAN 166 EFIAPI 167 RsaOaepDecrypt ( 168 IN VOID *RsaContext, 169 IN UINT8 *EncryptedData, 170 IN UINTN EncryptedDataSize, 171 IN UINT16 DigestLen OPTIONAL, 172 OUT UINT8 **OutData, 173 OUT UINTN *OutDataSize 174 ) 175 { 176 ASSERT (FALSE); 177 return FALSE; 178 } -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptTs.c
r99404 r105670 592 592 // 593 593 if ((EVP_add_digest (EVP_md5 ()) == 0) || (EVP_add_digest (EVP_sha1 ()) == 0) || 594 (EVP_add_digest (EVP_sha256 ()) == 0) || ((EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA)) == 0)) 594 (EVP_add_digest (EVP_sha256 ()) == 0) || (EVP_add_digest (EVP_sha384 ()) == 0) || 595 (EVP_add_digest (EVP_sha512 ()) == 0) || ((EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA)) == 0)) 595 596 { 596 597 return FALSE; -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptPkcs1OaepNull.c
r99404 r105670 4 4 SPDX-License-Identifier: BSD-2-Clause-Patent 5 5 6 Copyright (C) 2016Microsoft Corporation. All Rights Reserved.6 Copyright (C) Microsoft Corporation. All Rights Reserved. 7 7 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> 8 8 … … 49 49 return FALSE; 50 50 } 51 52 /** 53 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 54 encrypted message in a newly allocated buffer. 55 56 Things that can cause a failure include: 57 - X509 key size does not match any known key size. 58 - Fail to allocate an intermediate buffer. 59 - Null pointer provided for a non-optional parameter. 60 - Data size is too large for the provided key size (max size is a function of key size 61 and hash digest size). 62 63 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 64 provisioned with a public key using RsaSetKey(). 65 @param[in] InData Data to be encrypted. 66 @param[in] InDataSize Size of the data buffer. 67 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 68 to be used when initializing the PRNG. NULL otherwise. 69 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 70 0 otherwise. 71 @param[in] DigestLen [Optional] If provided, size of the hash used: 72 SHA1_DIGEST_SIZE 73 SHA256_DIGEST_SIZE 74 SHA384_DIGEST_SIZE 75 SHA512_DIGEST_SIZE 76 0 to use default (SHA1) 77 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 78 message. 79 @param[out] EncryptedDataSize Size of the encrypted message buffer. 80 81 @retval TRUE Encryption was successful. 82 @retval FALSE Encryption failed. 83 84 **/ 85 BOOLEAN 86 EFIAPI 87 RsaOaepEncrypt ( 88 IN VOID *RsaContext, 89 IN UINT8 *InData, 90 IN UINTN InDataSize, 91 IN CONST UINT8 *PrngSeed OPTIONAL, 92 IN UINTN PrngSeedSize OPTIONAL, 93 IN UINT16 DigestLen OPTIONAL, 94 OUT UINT8 **EncryptedData, 95 OUT UINTN *EncryptedDataSize 96 ) 97 { 98 ASSERT (FALSE); 99 return FALSE; 100 } 101 102 /** 103 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 104 decrypted message in a newly allocated buffer. 105 106 Things that can cause a failure include: 107 - Fail to parse private key. 108 - Fail to allocate an intermediate buffer. 109 - Null pointer provided for a non-optional parameter. 110 111 @param[in] PrivateKey A pointer to the DER-encoded private key. 112 @param[in] PrivateKeySize Size of the private key buffer. 113 @param[in] EncryptedData Data to be decrypted. 114 @param[in] EncryptedDataSize Size of the encrypted buffer. 115 @param[out] OutData Pointer to an allocated buffer containing the encrypted 116 message. 117 @param[out] OutDataSize Size of the encrypted message buffer. 118 119 @retval TRUE Encryption was successful. 120 @retval FALSE Encryption failed. 121 122 **/ 123 BOOLEAN 124 EFIAPI 125 Pkcs1v2Decrypt ( 126 IN CONST UINT8 *PrivateKey, 127 IN UINTN PrivateKeySize, 128 IN UINT8 *EncryptedData, 129 IN UINTN EncryptedDataSize, 130 OUT UINT8 **OutData, 131 OUT UINTN *OutDataSize 132 ) 133 { 134 ASSERT (FALSE); 135 return FALSE; 136 } 137 138 /** 139 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 140 decrypted message in a newly allocated buffer. 141 142 Things that can cause a failure include: 143 - Fail to parse private key. 144 - Fail to allocate an intermediate buffer. 145 - Null pointer provided for a non-optional parameter. 146 147 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 148 provisioned with a private key using RsaSetKey(). 149 @param[in] EncryptedData Data to be decrypted. 150 @param[in] EncryptedDataSize Size of the encrypted buffer. 151 @param[in] DigestLen [Optional] If provided, size of the hash used: 152 SHA1_DIGEST_SIZE 153 SHA256_DIGEST_SIZE 154 SHA384_DIGEST_SIZE 155 SHA512_DIGEST_SIZE 156 0 to use default (SHA1) 157 @param[out] OutData Pointer to an allocated buffer containing the encrypted 158 message. 159 @param[out] OutDataSize Size of the encrypted message buffer. 160 161 @retval TRUE Encryption was successful. 162 @retval FALSE Encryption failed. 163 164 **/ 165 BOOLEAN 166 EFIAPI 167 RsaOaepDecrypt ( 168 IN VOID *RsaContext, 169 IN UINT8 *EncryptedData, 170 IN UINTN EncryptedDataSize, 171 IN UINT16 DigestLen OPTIONAL, 172 OUT UINT8 **OutData, 173 OUT UINTN *OutDataSize 174 ) 175 { 176 ASSERT (FALSE); 177 return FALSE; 178 } -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c
r99404 r105670 2827 2827 2828 2828 /** 2829 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2830 decrypted message in a newly allocated buffer. 2831 Things that can cause a failure include: 2832 - Fail to parse private key. 2833 - Fail to allocate an intermediate buffer. 2834 - Null pointer provided for a non-optional parameter. 2835 @param[in] PrivateKey A pointer to the DER-encoded private key. 2836 @param[in] PrivateKeySize Size of the private key buffer. 2837 @param[in] EncryptedData Data to be decrypted. 2838 @param[in] EncryptedDataSize Size of the encrypted buffer. 2839 @param[out] OutData Pointer to an allocated buffer containing the encrypted 2840 message. 2841 @param[out] OutDataSize Size of the encrypted message buffer. 2842 @retval TRUE Encryption was successful. 2843 @retval FALSE Encryption failed. 2844 **/ 2845 BOOLEAN 2846 EFIAPI 2847 Pkcs1v2Decrypt ( 2848 IN CONST UINT8 *PrivateKey, 2849 IN UINTN PrivateKeySize, 2850 IN UINT8 *EncryptedData, 2851 IN UINTN EncryptedDataSize, 2852 OUT UINT8 **OutData, 2853 OUT UINTN *OutDataSize 2854 ) 2855 { 2856 CALL_CRYPTO_SERVICE (Pkcs1v2Decrypt, (PrivateKey, PrivateKeySize, EncryptedData, EncryptedDataSize, OutData, OutDataSize), FALSE); 2857 } 2858 2859 /** 2860 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2861 encrypted message in a newly allocated buffer. 2862 Things that can cause a failure include: 2863 - X509 key size does not match any known key size. 2864 - Fail to allocate an intermediate buffer. 2865 - Null pointer provided for a non-optional parameter. 2866 - Data size is too large for the provided key size (max size is a function of key size 2867 and hash digest size). 2868 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 2869 provisioned with a public key using RsaSetKey(). 2870 @param[in] InData Data to be encrypted. 2871 @param[in] InDataSize Size of the data buffer. 2872 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 2873 to be used when initializing the PRNG. NULL otherwise. 2874 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 2875 0 otherwise. 2876 @param[in] DigestLen [Optional] If provided, size of the hash used: 2877 SHA1_DIGEST_SIZE 2878 SHA256_DIGEST_SIZE 2879 SHA384_DIGEST_SIZE 2880 SHA512_DIGEST_SIZE 2881 0 to use default (SHA1) 2882 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 2883 message. 2884 @param[out] EncryptedDataSize Size of the encrypted message buffer. 2885 @retval TRUE Encryption was successful. 2886 @retval FALSE Encryption failed. 2887 **/ 2888 BOOLEAN 2889 EFIAPI 2890 RsaOaepEncrypt ( 2891 IN VOID *RsaContext, 2892 IN UINT8 *InData, 2893 IN UINTN InDataSize, 2894 IN CONST UINT8 *PrngSeed OPTIONAL, 2895 IN UINTN PrngSeedSize OPTIONAL, 2896 IN UINT16 DigestLen OPTIONAL, 2897 OUT UINT8 **EncryptedData, 2898 OUT UINTN *EncryptedDataSize 2899 ) 2900 { 2901 CALL_CRYPTO_SERVICE (RsaOaepEncrypt, (RsaContext, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize), FALSE); 2902 } 2903 2904 /** 2905 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 2906 decrypted message in a newly allocated buffer. 2907 Things that can cause a failure include: 2908 - Fail to parse private key. 2909 - Fail to allocate an intermediate buffer. 2910 - Null pointer provided for a non-optional parameter. 2911 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 2912 provisioned with a private key using RsaSetKey(). 2913 @param[in] EncryptedData Data to be decrypted. 2914 @param[in] EncryptedDataSize Size of the encrypted buffer. 2915 @param[in] DigestLen [Optional] If provided, size of the hash used: 2916 SHA1_DIGEST_SIZE 2917 SHA256_DIGEST_SIZE 2918 SHA384_DIGEST_SIZE 2919 SHA512_DIGEST_SIZE 2920 0 to use default (SHA1) 2921 @param[out] OutData Pointer to an allocated buffer containing the encrypted 2922 message. 2923 @param[out] OutDataSize Size of the encrypted message buffer. 2924 @retval TRUE Encryption was successful. 2925 @retval FALSE Encryption failed. 2926 **/ 2927 BOOLEAN 2928 EFIAPI 2929 RsaOaepDecrypt ( 2930 IN VOID *RsaContext, 2931 IN UINT8 *EncryptedData, 2932 IN UINTN EncryptedDataSize, 2933 IN UINT16 DigestLen OPTIONAL, 2934 OUT UINT8 **OutData, 2935 OUT UINTN *OutDataSize 2936 ) 2937 { 2938 CALL_CRYPTO_SERVICE (RsaOaepDecrypt, (RsaContext, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize), FALSE); 2939 } 2940 2941 /** 2829 2942 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7: 2830 2943 Cryptographic Message Syntax Standard". The input signed data could be wrapped … … 2850 2963 @retval FALSE Error occurs during the operation. 2851 2964 @retval FALSE This interface is not supported. 2965 2852 2966 2853 2967 **/ -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/TlsLib/TlsConfig.c
r101291 r105670 9 9 10 10 #include "InternalTlsLib.h" 11 12 typedef struct {13 //14 // IANA/IETF defined Cipher Suite ID15 //16 UINT16 IanaCipher;17 //18 // OpenSSL-used Cipher Suite String19 //20 CONST CHAR8 *OpensslCipher;21 //22 // Length of OpensslCipher23 //24 UINTN OpensslCipherLength;25 } TLS_CIPHER_MAPPING;26 27 //28 // Create a TLS_CIPHER_MAPPING initializer from IanaCipher and OpensslCipher so29 // that OpensslCipherLength is filled in automatically. IanaCipher must be an30 // integer constant expression, and OpensslCipher must be a string literal.31 //32 #define MAP(IanaCipher, OpensslCipher) \33 { (IanaCipher), (OpensslCipher), sizeof (OpensslCipher) - 1 }34 35 //36 // The mapping table between IANA/IETF Cipher Suite definitions and37 // OpenSSL-used Cipher Suite name.38 //39 // Keep the table uniquely sorted by the IanaCipher field, in increasing order.40 //41 STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable[] = {42 MAP (0x0001, "NULL-MD5"), /// TLS_RSA_WITH_NULL_MD543 MAP (0x0002, "NULL-SHA"), /// TLS_RSA_WITH_NULL_SHA44 MAP (0x0004, "RC4-MD5"), /// TLS_RSA_WITH_RC4_128_MD545 MAP (0x0005, "RC4-SHA"), /// TLS_RSA_WITH_RC4_128_SHA46 MAP (0x000A, "DES-CBC3-SHA"), /// TLS_RSA_WITH_3DES_EDE_CBC_SHA, mandatory TLS 1.147 MAP (0x0016, "DHE-RSA-DES-CBC3-SHA"), /// TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA48 MAP (0x002F, "AES128-SHA"), /// TLS_RSA_WITH_AES_128_CBC_SHA, mandatory TLS 1.249 MAP (0x0030, "DH-DSS-AES128-SHA"), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA50 MAP (0x0031, "DH-RSA-AES128-SHA"), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA51 MAP (0x0033, "DHE-RSA-AES128-SHA"), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA52 MAP (0x0035, "AES256-SHA"), /// TLS_RSA_WITH_AES_256_CBC_SHA53 MAP (0x0036, "DH-DSS-AES256-SHA"), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA54 MAP (0x0037, "DH-RSA-AES256-SHA"), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA55 MAP (0x0039, "DHE-RSA-AES256-SHA"), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA56 MAP (0x003B, "NULL-SHA256"), /// TLS_RSA_WITH_NULL_SHA25657 MAP (0x003C, "AES128-SHA256"), /// TLS_RSA_WITH_AES_128_CBC_SHA25658 MAP (0x003D, "AES256-SHA256"), /// TLS_RSA_WITH_AES_256_CBC_SHA25659 MAP (0x003E, "DH-DSS-AES128-SHA256"), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA25660 MAP (0x003F, "DH-RSA-AES128-SHA256"), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA25661 MAP (0x0067, "DHE-RSA-AES128-SHA256"), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA25662 MAP (0x0068, "DH-DSS-AES256-SHA256"), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA25663 MAP (0x0069, "DH-RSA-AES256-SHA256"), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA25664 MAP (0x006B, "DHE-RSA-AES256-SHA256"), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA25665 MAP (0x009F, "DHE-RSA-AES256-GCM-SHA384"), /// TLS_DHE_RSA_WITH_AES_256_GCM_SHA38466 MAP (0xC02B, "ECDHE-ECDSA-AES128-GCM-SHA256"), /// TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA25667 MAP (0xC02C, "ECDHE-ECDSA-AES256-GCM-SHA384"), /// TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA38468 MAP (0xC030, "ECDHE-RSA-AES256-GCM-SHA384"), /// TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA38469 };70 11 71 12 typedef struct { … … 96 37 { TlsSignatureAlgoEcdsa, "ECDSA" }, 97 38 }; 98 99 /**100 Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.101 102 @param[in] CipherId The supplied IANA TLS cipher suite ID.103 104 @return The corresponding OpenSSL cipher suite mapping if found,105 NULL otherwise.106 107 **/108 STATIC109 CONST TLS_CIPHER_MAPPING *110 TlsGetCipherMapping (111 IN UINT16 CipherId112 )113 {114 INTN Left;115 INTN Right;116 INTN Middle;117 118 //119 // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation120 //121 Left = 0;122 Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;123 124 while (Right >= Left) {125 Middle = (Left + Right) / 2;126 127 if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {128 //129 // Translate IANA cipher suite ID to OpenSSL name.130 //131 return &TlsCipherMappingTable[Middle];132 }133 134 if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {135 Right = Middle - 1;136 } else {137 Left = Middle + 1;138 }139 }140 141 //142 // No Cipher Mapping found, return NULL.143 //144 return NULL;145 }146 39 147 40 /** … … 282 175 ) 283 176 { 284 TLS_CONNECTION *TlsConn; 285 EFI_STATUS Status; 286 CONST TLS_CIPHER_MAPPING **MappedCipher; 287 UINTN MappedCipherBytes; 288 UINTN MappedCipherCount; 289 UINTN CipherStringSize; 290 UINTN Index; 291 CONST TLS_CIPHER_MAPPING *Mapping; 292 CHAR8 *CipherString; 293 CHAR8 *CipherStringPosition; 177 TLS_CONNECTION *TlsConn; 178 EFI_STATUS Status; 179 CONST SSL_CIPHER **MappedCipher; 180 UINTN MappedCipherBytes; 181 UINTN MappedCipherCount; 182 UINTN CipherStringSize; 183 UINTN Index; 184 INT32 StackIdx; 185 CHAR8 *CipherString; 186 CHAR8 *CipherStringPosition; 187 188 STACK_OF (SSL_CIPHER) *OpensslCipherStack; 189 CONST SSL_CIPHER *OpensslCipher; 190 CONST CHAR8 *OpensslCipherName; 191 UINTN OpensslCipherNameLength; 294 192 295 193 TlsConn = (TLS_CONNECTION *)Tls; … … 316 214 } 317 215 216 OpensslCipherStack = SSL_get_ciphers (TlsConn->Ssl); 217 318 218 // 319 219 // Map the cipher IDs, and count the number of bytes for the full … … 322 222 MappedCipherCount = 0; 323 223 CipherStringSize = 0; 324 for (Index = 0; Index < CipherNum; Index++) {224 for (Index = 0; OpensslCipherStack != NULL && Index < CipherNum; Index++) { 325 225 // 326 226 // Look up the IANA-to-OpenSSL mapping. 327 227 // 328 Mapping = TlsGetCipherMapping (CipherId[Index]); 329 if (Mapping == NULL) { 228 for (StackIdx = 0; StackIdx < sk_SSL_CIPHER_num (OpensslCipherStack); StackIdx++) { 229 OpensslCipher = sk_SSL_CIPHER_value (OpensslCipherStack, StackIdx); 230 if (CipherId[Index] == SSL_CIPHER_get_protocol_id (OpensslCipher)) { 231 break; 232 } 233 } 234 235 if (StackIdx == sk_SSL_CIPHER_num (OpensslCipherStack)) { 330 236 DEBUG (( 331 237 DEBUG_VERBOSE, … … 344 250 345 251 // 346 // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this252 // Accumulate cipher name string length into CipherStringSize. If this 347 253 // is not the first successful mapping, account for a colon (":") prefix 348 254 // too. … … 358 264 Status = SafeUintnAdd ( 359 265 CipherStringSize, 360 Mapping->OpensslCipherLength,266 AsciiStrLen (SSL_CIPHER_get_name (OpensslCipher)), 361 267 &CipherStringSize 362 268 ); … … 369 275 // Record the mapping. 370 276 // 371 MappedCipher[MappedCipherCount++] = Mapping;277 MappedCipher[MappedCipherCount++] = OpensslCipher; 372 278 } 373 279 … … 404 310 CipherStringPosition = CipherString; 405 311 for (Index = 0; Index < MappedCipherCount; Index++) { 406 Mapping = MappedCipher[Index]; 312 OpensslCipher = MappedCipher[Index]; 313 OpensslCipherName = SSL_CIPHER_get_name (OpensslCipher); 314 OpensslCipherNameLength = AsciiStrLen (OpensslCipherName); 407 315 // 408 316 // Append the colon (":") prefix except for the first mapping, then append 409 // Mapping->OpensslCipher.317 // OpensslCipherName. 410 318 // 411 319 if (Index > 0) { … … 415 323 CopyMem ( 416 324 CipherStringPosition, 417 Mapping->OpensslCipher,418 Mapping->OpensslCipherLength325 OpensslCipherName, 326 OpensslCipherNameLength 419 327 ); 420 CipherStringPosition += Mapping->OpensslCipherLength;328 CipherStringPosition += OpensslCipherNameLength; 421 329 } 422 330 -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Private/Protocol/Crypto.h
r99404 r105670 22 22 /// increased. 23 23 /// 24 #define EDKII_CRYPTO_VERSION 1 624 #define EDKII_CRYPTO_VERSION 17 25 25 26 26 /// … … 687 687 OUT UINT8 **EncryptedData, 688 688 OUT UINTN *EncryptedDataSize 689 ); 690 691 /** 692 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 693 decrypted message in a newly allocated buffer. 694 Things that can cause a failure include: 695 - Fail to parse private key. 696 - Fail to allocate an intermediate buffer. 697 - Null pointer provided for a non-optional parameter. 698 @param[in] PrivateKey A pointer to the DER-encoded private key. 699 @param[in] PrivateKeySize Size of the private key buffer. 700 @param[in] EncryptedData Data to be decrypted. 701 @param[in] EncryptedDataSize Size of the encrypted buffer. 702 @param[out] OutData Pointer to an allocated buffer containing the encrypted 703 message. 704 @param[out] OutDataSize Size of the encrypted message buffer. 705 @retval TRUE Encryption was successful. 706 @retval FALSE Encryption failed. 707 **/ 708 typedef 709 BOOLEAN 710 (EFIAPI *EDKII_CRYPTO_PKCS1V2_DECRYPT)( 711 IN CONST UINT8 *PrivateKey, 712 IN UINTN PrivateKeySize, 713 IN UINT8 *EncryptedData, 714 IN UINTN EncryptedDataSize, 715 OUT UINT8 **OutData, 716 OUT UINTN *OutDataSize 717 ); 718 719 /** 720 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 721 encrypted message in a newly allocated buffer. 722 Things that can cause a failure include: 723 - X509 key size does not match any known key size. 724 - Fail to allocate an intermediate buffer. 725 - Null pointer provided for a non-optional parameter. 726 - Data size is too large for the provided key size (max size is a function of key size 727 and hash digest size). 728 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 729 provisioned with a public key using RsaSetKey(). 730 @param[in] InData Data to be encrypted. 731 @param[in] InDataSize Size of the data buffer. 732 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer 733 to be used when initializing the PRNG. NULL otherwise. 734 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer. 735 0 otherwise. 736 @param[in] DigestLen [Optional] If provided, size of the hash used: 737 SHA1_DIGEST_SIZE 738 SHA256_DIGEST_SIZE 739 SHA384_DIGEST_SIZE 740 SHA512_DIGEST_SIZE 741 0 to use default (SHA1) 742 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted 743 message. 744 @param[out] EncryptedDataSize Size of the encrypted message buffer. 745 @retval TRUE Encryption was successful. 746 @retval FALSE Encryption failed. 747 **/ 748 typedef 749 BOOLEAN 750 (EFIAPI *EDKII_CRYPTO_RSA_OAEP_ENCRYPT)( 751 IN VOID *RsaContext, 752 IN UINT8 *InData, 753 IN UINTN InDataSize, 754 IN CONST UINT8 *PrngSeed OPTIONAL, 755 IN UINTN PrngSeedSize OPTIONAL, 756 IN UINT16 DigestLen OPTIONAL, 757 OUT UINT8 **EncryptedData, 758 OUT UINTN *EncryptedDataSize 759 ); 760 761 /** 762 Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the 763 decrypted message in a newly allocated buffer. 764 Things that can cause a failure include: 765 - Fail to parse private key. 766 - Fail to allocate an intermediate buffer. 767 - Null pointer provided for a non-optional parameter. 768 @param[in] RsaContext A pointer to an RSA context created by RsaNew() and 769 provisioned with a private key using RsaSetKey(). 770 @param[in] EncryptedData Data to be decrypted. 771 @param[in] EncryptedDataSize Size of the encrypted buffer. 772 @param[in] DigestLen [Optional] If provided, size of the hash used: 773 SHA1_DIGEST_SIZE 774 SHA256_DIGEST_SIZE 775 SHA384_DIGEST_SIZE 776 SHA512_DIGEST_SIZE 777 0 to use default (SHA1) 778 @param[out] OutData Pointer to an allocated buffer containing the encrypted 779 message. 780 @param[out] OutDataSize Size of the encrypted message buffer. 781 @retval TRUE Encryption was successful. 782 @retval FALSE Encryption failed. 783 **/ 784 typedef 785 BOOLEAN 786 (EFIAPI *EDKII_CRYPTO_RSA_OAEP_DECRYPT)( 787 IN VOID *RsaContext, 788 IN UINT8 *EncryptedData, 789 IN UINTN EncryptedDataSize, 790 IN UINT16 DigestLen OPTIONAL, 791 OUT UINT8 **OutData, 792 OUT UINTN *OutDataSize 689 793 ); 690 794 … … 5604 5708 EDKII_CRYPTO_ASN1_GET_TAG Asn1GetTag; 5605 5709 EDKII_CRYPTO_X509_GET_EXTENDED_BASIC_CONSTRAINTS X509GetExtendedBasicConstraints; 5710 EDKII_CRYPTO_PKCS1V2_DECRYPT Pkcs1v2Decrypt; 5711 EDKII_CRYPTO_RSA_OAEP_ENCRYPT RsaOaepEncrypt; 5712 EDKII_CRYPTO_RSA_OAEP_DECRYPT RsaOaepDecrypt; 5606 5713 }; 5607 5714 -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Readme.md
r99404 r105670 4 4 features such as UEFI Secure Boot, Measured Boot, firmware image authentication, 5 5 and network boot. The cryptographic service implementation in this package uses 6 services from the [OpenSSL](https://www.openssl.org/) project. 6 services from the [OpenSSL](https://www.openssl.org/) project and 7 [MbedTLS](https://www.trustedfirmware.org/projects/mbed-tls/) project. 7 8 8 9 EDK II firmware modules/libraries that requires the use of cryptographic … … 19 20 # Public Library Classes 20 21 21 * **BaseCryptLib** - Provides library functions for cryptographic primitives. 22 * **TlsLib** - Provides TLS library functions for EFI TLS protocol. 23 * **HashApiLib** - Provides Unified API for different hash implementations. 22 * **BaseCryptLib** - Provides library functions based on OpenSSL for 23 cryptographic primitives. 24 * **BaseCryptLibMbedTls** - Provides library functions based on MbedTLS for 25 cryptographic primitives. 26 * **TlsLib** - Provides TLS library functions for EFI TLS protocol. 27 * **HashApiLib** - Provides Unified API for different hash implementations. 24 28 25 29 # Private Library Classes 26 30 27 31 * **OpensslLib** - Provides library functions from the openssl project. 28 * **IntrinsicLib** - Provides C runtime library (CRT) required by openssl. 32 * **MbedTlsLib** - Provides library functions from the mbedtls project. 33 * **IntrinsicLib** - Provides C runtime library (CRT) required by openssl 34 and mbedtls. 29 35 30 36 # Private Protocols and PPIs -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HashTests.c
r99404 r105670 85 85 typedef 86 86 BOOLEAN 87 (EFIAPI *EFI_HASH_DUP)( 88 IN CONST VOID *HashContext, 89 OUT VOID *NewHashContext 90 ); 91 92 typedef 93 BOOLEAN 87 94 (EFIAPI *EFI_HASH_FINAL)( 88 95 IN OUT VOID *HashContext, … … 103 110 EFI_HASH_INIT HashInit; 104 111 EFI_HASH_UPDATE HashUpdate; 112 EFI_HASH_DUP HashDup; 105 113 EFI_HASH_FINAL HashFinal; 106 114 EFI_HASH_ALL HashAll; … … 110 118 111 119 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES 112 HASH_TEST_CONTEXT mMd5TestCtx = { MD5_DIGEST_SIZE, Md5GetContextSize, Md5Init, Md5Update, Md5 Final, Md5HashAll, Md5Digest };120 HASH_TEST_CONTEXT mMd5TestCtx = { MD5_DIGEST_SIZE, Md5GetContextSize, Md5Init, Md5Update, Md5Duplicate, Md5Final, Md5HashAll, Md5Digest }; 113 121 #endif 114 HASH_TEST_CONTEXT mSha1TestCtx = { SHA1_DIGEST_SIZE, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1 Final, Sha1HashAll, Sha1Digest };115 HASH_TEST_CONTEXT mSha256TestCtx = { SHA256_DIGEST_SIZE, Sha256GetContextSize, Sha256Init, Sha256Update, Sha256 Final, Sha256HashAll, Sha256Digest };116 HASH_TEST_CONTEXT mSha384TestCtx = { SHA384_DIGEST_SIZE, Sha384GetContextSize, Sha384Init, Sha384Update, Sha384 Final, Sha384HashAll, Sha384Digest };117 HASH_TEST_CONTEXT mSha512TestCtx = { SHA512_DIGEST_SIZE, Sha512GetContextSize, Sha512Init, Sha512Update, Sha512 Final, Sha512HashAll, Sha512Digest };122 HASH_TEST_CONTEXT mSha1TestCtx = { SHA1_DIGEST_SIZE, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1Duplicate, Sha1Final, Sha1HashAll, Sha1Digest }; 123 HASH_TEST_CONTEXT mSha256TestCtx = { SHA256_DIGEST_SIZE, Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Duplicate, Sha256Final, Sha256HashAll, Sha256Digest }; 124 HASH_TEST_CONTEXT mSha384TestCtx = { SHA384_DIGEST_SIZE, Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Duplicate, Sha384Final, Sha384HashAll, Sha384Digest }; 125 HASH_TEST_CONTEXT mSha512TestCtx = { SHA512_DIGEST_SIZE, Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Duplicate, Sha512Final, Sha512HashAll, Sha512Digest }; 118 126 119 127 UNIT_TEST_STATUS … … 158 166 UINTN DataSize; 159 167 UINT8 Digest[MAX_DIGEST_SIZE]; 168 UINT8 DigestCopy[MAX_DIGEST_SIZE]; 160 169 BOOLEAN Status; 161 170 HASH_TEST_CONTEXT *HashTestContext; 171 VOID *HashCopyContext; 162 172 163 173 HashTestContext = Context; … … 166 176 167 177 ZeroMem (Digest, MAX_DIGEST_SIZE); 178 ZeroMem (DigestCopy, MAX_DIGEST_SIZE); 179 180 HashCopyContext = AllocatePool (HashTestContext->GetContextSize ()); 168 181 169 182 Status = HashTestContext->HashInit (HashTestContext->HashCtx); 170 183 UT_ASSERT_TRUE (Status); 171 184 185 Status = HashTestContext->HashInit (HashCopyContext); 186 UT_ASSERT_TRUE (Status); 187 172 188 Status = HashTestContext->HashUpdate (HashTestContext->HashCtx, HashData, DataSize); 173 189 UT_ASSERT_TRUE (Status); 174 190 191 Status = HashTestContext->HashDup (HashTestContext->HashCtx, HashCopyContext); 192 UT_ASSERT_TRUE (Status); 193 175 194 Status = HashTestContext->HashFinal (HashTestContext->HashCtx, Digest); 176 195 UT_ASSERT_TRUE (Status); 177 196 197 Status = HashTestContext->HashFinal (HashCopyContext, DigestCopy); 198 UT_ASSERT_TRUE (Status); 199 178 200 UT_ASSERT_MEM_EQUAL (Digest, HashTestContext->Digest, HashTestContext->DigestSize); 201 UT_ASSERT_MEM_EQUAL (Digest, DigestCopy, HashTestContext->DigestSize); 179 202 180 203 ZeroMem (Digest, MAX_DIGEST_SIZE); -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HmacTests.c
r99404 r105670 83 83 84 84 typedef 85 VOID *85 VOID * 86 86 (EFIAPI *EFI_HMAC_NEW)( 87 87 VOID … … 89 89 90 90 typedef 91 VOID91 VOID 92 92 (EFIAPI *EFI_HMAC_FREE)( 93 93 IN VOID *HashContext … … 95 95 96 96 typedef 97 BOOLEAN97 BOOLEAN 98 98 (EFIAPI *EFI_HMAC_INIT)( 99 99 IN OUT VOID *HashContext, … … 103 103 104 104 typedef 105 BOOLEAN 105 BOOLEAN 106 (EFIAPI *EFI_HMAC_DUP)( 107 IN CONST VOID *HashContext, 108 OUT VOID *NewHashContext 109 ); 110 111 typedef 112 BOOLEAN 106 113 (EFIAPI *EFI_HMAC_UPDATE)( 107 114 IN OUT VOID *HashContext, … … 111 118 112 119 typedef 113 BOOLEAN120 BOOLEAN 114 121 (EFIAPI *EFI_HMAC_FINAL)( 115 122 IN OUT VOID *HashContext, 116 123 OUT UINT8 *HashValue 124 ); 125 126 typedef 127 BOOLEAN 128 (EFIAPI *EFI_HMAC_ALL)( 129 IN CONST VOID *Data, 130 IN UINTN DataSize, 131 IN CONST UINT8 *Key, 132 IN UINTN KeySize, 133 OUT UINT8 *HashValue 117 134 ); 118 135 … … 122 139 EFI_HMAC_FREE HmacFree; 123 140 EFI_HMAC_INIT HmacInit; 141 EFI_HMAC_DUP HmacDup; 124 142 EFI_HMAC_UPDATE HmacUpdate; 125 143 EFI_HMAC_FINAL HmacFinal; 144 EFI_HMAC_ALL HmacAll; 126 145 CONST UINT8 *Key; 127 146 UINTN KeySize; … … 130 149 } HMAC_TEST_CONTEXT; 131 150 132 // These functions have been deprecated but they've been left commented out for future reference 133 // HMAC_TEST_CONTEXT mHmacMd5TestCtx = {MD5_DIGEST_SIZE, HmacMd5New, HmacMd5Free, HmacMd5SetKey, HmacMd5Update, HmacMd5Final, HmacMd5Key, sizeof(HmacMd5Key), HmacMd5Digest}; 134 // HMAC_TEST_CONTEXT mHmacSha1TestCtx = {SHA1_DIGEST_SIZE, HmacSha1New, HmacSha1Free, HmacSha1SetKey, HmacSha1Update, HmacSha1Final, HmacSha1Key, sizeof(HmacSha1Key), HmacSha1Digest}; 135 HMAC_TEST_CONTEXT mHmacSha256TestCtx = { SHA256_DIGEST_SIZE, HmacSha256New, HmacSha256Free, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Key, sizeof (HmacSha256Key), HmacSha256Digest }; 136 HMAC_TEST_CONTEXT mHmacSha384TestCtx = { SHA384_DIGEST_SIZE, HmacSha384New, HmacSha384Free, HmacSha384SetKey, HmacSha384Update, HmacSha384Final, HmacSha384Key, sizeof (HmacSha384Key), HmacSha384Digest }; 151 HMAC_TEST_CONTEXT mHmacSha256TestCtx = { SHA256_DIGEST_SIZE, HmacSha256New, HmacSha256Free, HmacSha256SetKey, HmacSha256Duplicate, HmacSha256Update, HmacSha256Final, HmacSha256All, HmacSha256Key, sizeof (HmacSha256Key), HmacSha256Digest }; 152 HMAC_TEST_CONTEXT mHmacSha384TestCtx = { SHA384_DIGEST_SIZE, HmacSha384New, HmacSha384Free, HmacSha384SetKey, HmacSha384Duplicate, HmacSha384Update, HmacSha384Final, HmacSha384All, HmacSha384Key, sizeof (HmacSha384Key), HmacSha384Digest }; 137 153 138 154 UNIT_TEST_STATUS … … 174 190 { 175 191 UINT8 Digest[MAX_DIGEST_SIZE]; 192 UINT8 DigestCopy[MAX_DIGEST_SIZE]; 193 UINT8 DigestByAll[MAX_DIGEST_SIZE]; 194 VOID *HmacCopyContext; 176 195 BOOLEAN Status; 177 196 HMAC_TEST_CONTEXT *HmacTestContext; … … 180 199 181 200 ZeroMem (Digest, MAX_DIGEST_SIZE); 201 ZeroMem (DigestCopy, MAX_DIGEST_SIZE); 202 ZeroMem (DigestByAll, MAX_DIGEST_SIZE); 203 204 HmacCopyContext = HmacTestContext->HmacNew (); 182 205 183 206 Status = HmacTestContext->HmacInit (HmacTestContext->HmacCtx, HmacTestContext->Key, HmacTestContext->KeySize); 184 207 UT_ASSERT_TRUE (Status); 185 208 209 Status = HmacTestContext->HmacInit (HmacCopyContext, HmacTestContext->Key, HmacTestContext->KeySize); 210 UT_ASSERT_TRUE (Status); 211 186 212 Status = HmacTestContext->HmacUpdate (HmacTestContext->HmacCtx, HmacData, 8); 187 213 UT_ASSERT_TRUE (Status); 188 214 215 Status = HmacTestContext->HmacDup (HmacTestContext->HmacCtx, HmacCopyContext); 216 UT_ASSERT_TRUE (Status); 217 189 218 Status = HmacTestContext->HmacFinal (HmacTestContext->HmacCtx, Digest); 190 219 UT_ASSERT_TRUE (Status); 191 220 221 Status = HmacTestContext->HmacFinal (HmacCopyContext, DigestCopy); 222 UT_ASSERT_TRUE (Status); 223 224 Status = HmacTestContext->HmacAll (HmacData, 8, HmacTestContext->Key, HmacTestContext->KeySize, DigestByAll); 225 UT_ASSERT_TRUE (Status); 226 192 227 UT_ASSERT_MEM_EQUAL (Digest, HmacTestContext->Digest, HmacTestContext->DigestSize); 228 UT_ASSERT_MEM_EQUAL (Digest, DigestCopy, HmacTestContext->DigestSize); 229 UT_ASSERT_MEM_EQUAL (Digest, DigestByAll, HmacTestContext->DigestSize); 193 230 194 231 return UNIT_TEST_PASSED; … … 201 238 { "TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha256TestCtx }, 202 239 { "TestVerifyHmacSha384()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha384TestCtx }, 203 // These functions have been deprecated but they've been left commented out for future reference204 // {"TestVerifyHmacMd5()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacMd5TestCtx},205 // {"TestVerifyHmacSha1()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha1TestCtx},206 240 }; 207 241 -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/OaepEncryptTests.c
r99404 r105670 1 1 /** @file 2 This is a unit test for RSA OAEP encrypt .2 This is a unit test for RSA OAEP encrypt/decrypt. 3 3 4 4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> 5 Copyright (c) Microsoft Corporation. All rights reserved. 5 6 SPDX-License-Identifier: BSD-2-Clause-Patent 6 7 **/ … … 8 9 #include "TestBaseCryptLib.h" 9 10 10 CONST UINT8 RandSeed[] = "This is the random seed for PRNG verification.";11 STATIC CONST UINT8 RandSeed[] = "This is the random seed for PRNG verification."; 11 12 12 13 // … … 15 16 // O = Intel 16 17 // 17 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 SelfTestCert[] = {18 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 SelfTestCert[] = { 18 19 0x30, 0x82, 0x03, 0x90, 0x30, 0x82, 0x02, 0x78, 0x02, 0x09, 0x00, 0xE4, 0xDF, 0x47, 0x80, 0xEF, 19 20 0x4B, 0x3C, 0x6D, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, … … 76 77 }; 77 78 78 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 PrivateKey[] = {79 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 PrivateKey[] = { 79 80 0x30, 0x82, 0x04, 0xA4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xBC, 0xE4, 0x67, 0xDC, 80 81 0xC7, 0xEA, 0x6F, 0x8A, 0xA7, 0xCC, 0xB2, 0x54, 0x47, 0x48, 0x6A, 0xE2, 0x39, 0xFF, 0xC2, 0x48, … … 154 155 }; 155 156 157 // The following RSA key componets were extracted from the above private key with openssl. 158 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 RsaN[] = { 159 0x00, 160 0xbc,0xe4, 0x67, 0xdc, 0xc7, 0xea, 0x6f, 0x8a, 0xa7, 0xcc, 0xb2, 0x54, 0x47, 0x48, 0x6a, 0xe2, 161 0x39,0xff, 0xc2, 0x48, 0x58, 0x34, 0x07, 0x03, 0x6d, 0x39, 0xb3, 0x67, 0x46, 0x4c, 0xbc, 0xa0, 162 0xfa,0x4e, 0x64, 0x23, 0x56, 0x47, 0x7b, 0xc9, 0x1a, 0x2a, 0x55, 0x42, 0x54, 0x10, 0x18, 0x30, 163 0x92,0x60, 0x30, 0x5b, 0x9e, 0xc0, 0x65, 0xd2, 0xd4, 0x05, 0x4a, 0xa6, 0x10, 0x66, 0x04, 0xa9, 164 0x54,0x4e, 0xee, 0x49, 0x39, 0x43, 0x65, 0x1e, 0x2e, 0x28, 0xde, 0x79, 0x24, 0xa9, 0x7e, 0xd8, 165 0x5b,0xbc, 0x2f, 0x46, 0x6a, 0xb7, 0xb6, 0x0d, 0x17, 0x88, 0x37, 0x52, 0x5c, 0xfe, 0x93, 0xc0, 166 0xe2,0xfd, 0x6a, 0x08, 0x1b, 0xfb, 0xd1, 0x87, 0xbd, 0xbd, 0x58, 0x57, 0x2c, 0x06, 0x5d, 0xd2, 167 0x7d,0x52, 0xe2, 0x49, 0x8e, 0xdc, 0xe5, 0x26, 0xbd, 0x92, 0x60, 0xb0, 0x3f, 0x58, 0x5e, 0x52, 168 0xd7,0x91, 0xda, 0x93, 0x62, 0x8d, 0x71, 0x80, 0x53, 0xba, 0x15, 0xc4, 0x1f, 0xf3, 0xbd, 0xe0, 169 0xc5,0xa4, 0xb8, 0xd3, 0x64, 0x12, 0x14, 0x1b, 0x11, 0x6b, 0x7b, 0xc2, 0x92, 0xc7, 0xe2, 0x94, 170 0x0b,0xb8, 0x67, 0x38, 0x48, 0x63, 0x11, 0x74, 0x25, 0x7c, 0x37, 0xc3, 0xb2, 0xae, 0xd9, 0xa7, 171 0x17,0x9c, 0x4b, 0x9d, 0x6c, 0x27, 0xb0, 0x87, 0x16, 0x6b, 0xf2, 0x96, 0xe5, 0x1d, 0x37, 0x27, 172 0xde,0xf2, 0x98, 0xb7, 0x81, 0x08, 0xd9, 0x7a, 0xba, 0x84, 0x14, 0x61, 0x60, 0x48, 0xce, 0xce, 173 0x51,0x73, 0xf4, 0xdb, 0xf1, 0x5f, 0x7a, 0x17, 0x71, 0x4f, 0xc1, 0x0b, 0xce, 0xc7, 0x31, 0xc1, 174 0x4e,0xa3, 0xee, 0x6f, 0x72, 0x97, 0x90, 0xfb, 0x8b, 0x54, 0x9f, 0x82, 0x5b, 0x48, 0x5a, 0xf1, 175 0xad,0x8b, 0x3a, 0xcd, 0xca, 0xb2, 0x8b, 0x7a, 0x53, 0xd4, 0xf7, 0x71, 0x16, 0x75, 0xa7, 0x35, 176 }; 177 178 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 RsaE[] = { 179 0x01, 0x00, 0x01 180 }; 181 182 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 RsaD[] = { 183 0x13, 0xf7, 0xd1, 0x42, 0xf5, 0x9f, 0x42, 0xcb, 0x55, 0x91, 0xbe, 0x08, 0x4a, 0xc0, 0xcd, 0x0b, 184 0xbd, 0x35, 0xdc, 0x43, 0xe9, 0x8f, 0x16, 0x6e, 0xb6, 0x4d, 0x33, 0x39, 0xe7, 0xa4, 0x95, 0x0c, 185 0x2f, 0x69, 0xba, 0x0c, 0x42, 0x42, 0xac, 0x43, 0x46, 0x10, 0xd3, 0x92, 0x7f, 0x70, 0x74, 0x1e, 186 0x2e, 0x5b, 0x1c, 0xc1, 0x92, 0xb6, 0xa4, 0x0c, 0xf5, 0x7c, 0xd9, 0xb7, 0x54, 0x64, 0x74, 0x79, 187 0xb1, 0xff, 0xe6, 0x10, 0xb7, 0x8c, 0xf8, 0x53, 0x88, 0x6d, 0xa9, 0x97, 0x04, 0xd9, 0x26, 0x1f, 188 0x99, 0x12, 0xfb, 0xac, 0x65, 0xfb, 0xa5, 0xb3, 0x1c, 0x99, 0xb9, 0xbf, 0x6b, 0x35, 0x3e, 0x49, 189 0x55, 0xb5, 0x94, 0x4f, 0xe7, 0x25, 0x67, 0xb1, 0x01, 0xcd, 0xd2, 0x58, 0xe4, 0xbe, 0x87, 0x8c, 190 0x88, 0xd3, 0x0a, 0x38, 0xdc, 0x71, 0x5d, 0x88, 0x0a, 0xe2, 0x3e, 0x76, 0x63, 0x3b, 0xe4, 0x3c, 191 0x8f, 0x2f, 0x29, 0x1d, 0xd1, 0x66, 0x8d, 0xc0, 0x4a, 0x68, 0x15, 0x90, 0x4c, 0x95, 0x61, 0xf4, 192 0xfd, 0xe8, 0xfa, 0x9c, 0x6c, 0x00, 0x22, 0x23, 0xd5, 0x17, 0x6e, 0xee, 0xa8, 0xd8, 0x70, 0xc5, 193 0x74, 0xea, 0x09, 0x13, 0x7f, 0x0c, 0x37, 0x4d, 0x50, 0xcd, 0xe9, 0x16, 0xc2, 0xd5, 0xde, 0x5e, 194 0xc3, 0xfc, 0x46, 0x08, 0xf1, 0x99, 0xc0, 0xb4, 0x28, 0xfd, 0x2b, 0x29, 0xef, 0x76, 0xd7, 0x04, 195 0x4f, 0x02, 0x54, 0x16, 0x54, 0x55, 0x20, 0xec, 0xbc, 0xbf, 0x85, 0x5f, 0x12, 0xcc, 0xfc, 0x0d, 196 0xf2, 0xef, 0xfc, 0x4d, 0x3e, 0xa2, 0x5e, 0x97, 0xfe, 0x35, 0x10, 0x0f, 0x53, 0x1f, 0x80, 0xd5, 197 0xc0, 0xb4, 0xe9, 0xe9, 0x31, 0x4c, 0x89, 0x14, 0x72, 0x39, 0x65, 0x89, 0xef, 0x7a, 0x51, 0x4a, 198 0xb9, 0xa9, 0xcc, 0x1b, 0x52, 0xb0, 0x02, 0x52, 0x65, 0x2f, 0x0b, 0x89, 0x41, 0x70, 0x1e, 0x01, 199 }; 200 201 // test case = "123\0" 202 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 Msg1230[] = { 203 0x31, 0x32, 0x33, 0x00 204 }; 205 206 // Ciphertext of the test case using RSAES-OAEP2048 with SHA1 MD/BGF1 created with openssl. 207 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 Ct1230RsaesOaepMdSha1Mgf1Sha1[] = { 208 0x88, 0x5d, 0xf3, 0x00, 0x66, 0x77, 0x91, 0x94, 0x5c, 0x8d, 0x45, 0xb6, 0xb2, 0x24, 0x26, 0x26, 209 0x37, 0xbe, 0xe0, 0x87, 0x4f, 0x50, 0xbf, 0x88, 0xde, 0x5d, 0xe9, 0xe0, 0xb2, 0x7e, 0x66, 0xfa, 210 0x6c, 0xfd, 0x0d, 0x19, 0x48, 0x41, 0xfe, 0x7a, 0x86, 0xa8, 0x28, 0xc2, 0x01, 0xcf, 0x76, 0xd7, 211 0xea, 0xab, 0x6d, 0xc3, 0x5e, 0x2c, 0x36, 0x04, 0xc0, 0x54, 0xc2, 0x68, 0x67, 0xe7, 0x04, 0x27, 212 0x56, 0xbe, 0x53, 0xb5, 0x80, 0x94, 0xd8, 0xde, 0x8c, 0x75, 0x69, 0x42, 0xba, 0x55, 0xd6, 0x2c, 213 0xda, 0x22, 0xe6, 0x09, 0xf6, 0x90, 0x27, 0x4b, 0x10, 0x54, 0x40, 0xa0, 0x74, 0x31, 0xdb, 0x5f, 214 0x80, 0x06, 0xc7, 0x67, 0x96, 0xe8, 0x45, 0xea, 0x7f, 0x72, 0x18, 0x24, 0xe8, 0x0d, 0x46, 0xc2, 215 0xa0, 0x83, 0xca, 0x71, 0xca, 0x91, 0x4b, 0x89, 0x80, 0x61, 0x01, 0x8e, 0xcf, 0xa1, 0x68, 0x81, 216 0x2d, 0xf2, 0x08, 0xd2, 0x02, 0x9e, 0xc0, 0xa4, 0x91, 0x71, 0x90, 0x84, 0x2f, 0x4e, 0x18, 0x37, 217 0x9b, 0x61, 0x0b, 0xf5, 0x88, 0xf7, 0x6b, 0x87, 0xb9, 0x4e, 0x31, 0xda, 0xf3, 0xb5, 0xe2, 0x60, 218 0x4d, 0xd9, 0x52, 0x99, 0x6b, 0x19, 0x98, 0xa2, 0x28, 0xaa, 0xeb, 0x5a, 0x33, 0xef, 0xf1, 0x4e, 219 0x29, 0x86, 0xbf, 0x70, 0x08, 0xfd, 0x34, 0x8a, 0x8c, 0x6d, 0xef, 0xc4, 0xa1, 0xfe, 0xdf, 0x4d, 220 0xeb, 0xf0, 0x2c, 0x4c, 0xf5, 0xb3, 0xe8, 0xf8, 0xc3, 0x45, 0xc7, 0x6b, 0x59, 0x1c, 0x9b, 0xd9, 221 0x52, 0xdf, 0x65, 0x87, 0x18, 0xd2, 0x6d, 0xff, 0x8b, 0x98, 0x2a, 0x97, 0xeb, 0x93, 0xea, 0x6a, 222 0x23, 0x23, 0xc6, 0x32, 0xf5, 0xea, 0x45, 0xe3, 0x99, 0xa0, 0x4d, 0x4b, 0x8f, 0xf8, 0x1d, 0xad, 223 0xa9, 0x97, 0xa2, 0xd6, 0xaf, 0x5e, 0x11, 0xf7, 0x5f, 0x28, 0xfb, 0x38, 0x80, 0x38, 0x50, 0xc4, 224 }; 225 226 // Ciphertext of the test case using RSAES-OAEP2048 with SHA256 MD/BGF1 created with openssl. 227 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT8 Ct1230RsaesOaep2048MdSha256Mgf1Sha256[] = { 228 0xa7, 0x20, 0xa9, 0x31, 0xb5, 0xad, 0x83, 0x0a, 0x07, 0xee, 0x36, 0x46, 0xa5, 0x78, 0x3a, 0xda, 229 0x9d, 0xdf, 0xe6, 0x05, 0x0f, 0x7c, 0x46, 0xfe, 0x5f, 0xd6, 0x58, 0x16, 0xb6, 0xaa, 0x82, 0x7c, 230 0x58, 0x8a, 0x52, 0x14, 0x12, 0x29, 0x6f, 0x62, 0x80, 0xa7, 0x61, 0xfe, 0x29, 0x72, 0x6f, 0x73, 231 0xf6, 0x2f, 0x54, 0x38, 0x58, 0x7b, 0xbd, 0xa1, 0x2f, 0x9d, 0x12, 0x83, 0x72, 0xbc, 0x3d, 0x29, 232 0x65, 0x39, 0xcb, 0x93, 0x95, 0x3e, 0x73, 0xc9, 0x6f, 0xb9, 0xe8, 0xd5, 0x8b, 0x91, 0x0d, 0x87, 233 0x7e, 0x22, 0xb5, 0x93, 0x3d, 0xa8, 0x4a, 0xd9, 0x1a, 0x13, 0xf7, 0xf4, 0x7f, 0x16, 0x42, 0xfe, 234 0x63, 0x10, 0x7e, 0xa1, 0xe5, 0x04, 0xcf, 0xed, 0x93, 0x2d, 0x16, 0x3b, 0x79, 0x1f, 0x53, 0x41, 235 0xe3, 0xca, 0x69, 0x18, 0x6a, 0xe5, 0xec, 0x9a, 0xce, 0xbc, 0x47, 0xf6, 0x77, 0x9a, 0x5c, 0xea, 236 0xac, 0x7e, 0x28, 0xeb, 0x1e, 0xfe, 0x75, 0xa6, 0xbf, 0x1e, 0xfd, 0x1c, 0x63, 0x69, 0x47, 0x04, 237 0xaf, 0x69, 0x7e, 0x1c, 0xa1, 0x7f, 0x00, 0xcf, 0xec, 0x16, 0x34, 0xd9, 0xde, 0x91, 0x0e, 0x0f, 238 0x0b, 0x1e, 0x66, 0xc3, 0x41, 0x88, 0x43, 0xbe, 0xa3, 0x2a, 0x7c, 0x87, 0xff, 0xc0, 0x67, 0xdc, 239 0xc7, 0xeb, 0x28, 0x07, 0x00, 0x72, 0x85, 0x17, 0xca, 0x05, 0x9f, 0x29, 0x6b, 0xad, 0xc6, 0xae, 240 0x1c, 0x4a, 0xf2, 0xfe, 0x97, 0xc7, 0x6e, 0x4b, 0xbf, 0xfd, 0x46, 0xbe, 0xf8, 0x76, 0xc9, 0x70, 241 0x58, 0x3a, 0x73, 0xcc, 0x34, 0xda, 0xfe, 0x5b, 0x6d, 0x98, 0x74, 0x95, 0x85, 0xc7, 0xc9, 0x84, 242 0x02, 0xa8, 0x97, 0x13, 0xa3, 0x83, 0xcb, 0x28, 0x3d, 0xbb, 0x2b, 0x3b, 0x45, 0xf1, 0x6e, 0xc5, 243 0x37, 0x23, 0x21, 0xe6, 0x74, 0x2d, 0x48, 0x19, 0x97, 0xaf, 0xee, 0x3d, 0x9b, 0xd0, 0x05, 0xc7 244 }; 245 246 typedef struct _OAEP_ENC_DEC_TEST_CONTEXT OAEP_ENC_DEC_TEST_CONTEXT; 247 typedef 248 BOOLEAN 249 (EFIAPI *OAEP_TEST_ENCRYPT)( 250 IN OAEP_ENC_DEC_TEST_CONTEXT *TestContext, 251 IN CONST UINT8 *ClearText, 252 IN UINTN ClearTextSize, 253 IN CONST UINT8 *PrngSeed, 254 IN UINTN PrngSeedSize, 255 IN UINT16 DigestLen, 256 OUT UINT8 **CipherText, 257 OUT UINTN *CipherTextSize 258 ); 259 260 typedef 261 BOOLEAN 262 (EFIAPI *OAEP_TEST_DECRYPT)( 263 IN OAEP_ENC_DEC_TEST_CONTEXT *TestContext, 264 IN CONST UINT8 *CipherText, 265 IN UINTN CipherTextSize, 266 IN UINT16 DigestLen, 267 OUT UINT8 **ClearText, 268 OUT UINTN *ClearTextSize 269 ); 270 271 typedef struct _OAEP_ENC_DEC_TEST_CONTEXT { 272 CONST UINT8 *SelfTestCert; 273 UINTN SelfTestCertSize; 274 CONST UINT8 *PrivateKey; 275 UINTN PrivateKeySize; 276 CONST UINT8 *RsaN; 277 UINTN RsaNSize; 278 CONST UINT8 *RsaE; 279 UINTN RsaESize; 280 CONST UINT8 *RsaD; 281 UINTN RsaDSize; 282 CONST UINT8 *PrngSeed; 283 UINTN PrngSeedSize; 284 CONST UINT8 *ClearText; 285 UINTN ClearTextSize; 286 CONST UINT8 *CipherText; 287 UINTN CipherTextSize; 288 UINT16 DigestLen; 289 OAEP_TEST_ENCRYPT Encrypt; 290 OAEP_TEST_DECRYPT Decrypt; 291 UNIT_TEST_STATUS Expect; 292 } OAEP_ENC_DEC_TEST_CONTEXT; 293 294 BOOLEAN 295 EFIAPI 296 CallPkcs1v2Encrypt ( 297 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx, 298 CONST UINT8 *ClearText, 299 UINTN ClearTextSize, 300 CONST UINT8 *PrngSeed, 301 UINTN PrngSeedSize, 302 UINT16 DigestLen, 303 UINT8 **CipherText, 304 UINTN *CipherTextSize 305 ) 306 { 307 BOOLEAN Status; 308 309 Status = Pkcs1v2Encrypt ( 310 TestCtx->SelfTestCert, 311 TestCtx->SelfTestCertSize, 312 (UINT8 *)ClearText, 313 ClearTextSize, 314 PrngSeed, 315 PrngSeedSize, 316 CipherText, 317 CipherTextSize 318 ); 319 320 return Status; 321 } 322 323 BOOLEAN 324 EFIAPI 325 CallPkcs1v2Decrypt ( 326 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx, 327 CONST UINT8 *CipherText, 328 UINTN CipherTextSize, 329 UINT16 DigestLen, 330 UINT8 **ClearText, 331 UINTN *ClearTextSize 332 ) 333 { 334 BOOLEAN Status; 335 336 Status = Pkcs1v2Decrypt ( 337 TestCtx->PrivateKey, 338 TestCtx->PrivateKeySize, 339 (UINT8 *)CipherText, 340 CipherTextSize, 341 ClearText, 342 ClearTextSize 343 ); 344 return Status; 345 } 346 347 BOOLEAN 348 EFIAPI 349 CallRsaOaepEncrypt ( 350 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx, 351 CONST UINT8 *ClearText, 352 UINTN ClearTextSize, 353 CONST UINT8 *RandSeedIn, 354 UINTN RandSeedSizeIn, 355 UINT16 DigestLen, 356 UINT8 **CipherText, 357 UINTN *CipherTextSize 358 ) 359 { 360 VOID *RsaContext = NULL; 361 BOOLEAN Status; 362 363 RsaContext = RsaNew (); 364 UT_ASSERT_FALSE (RsaContext == NULL); 365 366 Status = RsaSetKey (RsaContext, RsaKeyN, TestCtx->RsaN, TestCtx->RsaNSize); 367 UT_ASSERT_TRUE (Status); 368 369 Status = RsaSetKey (RsaContext, RsaKeyE, TestCtx->RsaE, TestCtx->RsaESize); 370 UT_ASSERT_TRUE (Status); 371 372 Status = RsaOaepEncrypt ( 373 RsaContext, 374 (UINT8 *)ClearText, 375 ClearTextSize, 376 RandSeedIn, 377 RandSeedSizeIn, 378 DigestLen, 379 CipherText, 380 CipherTextSize 381 ); 382 383 return Status; 384 } 385 386 BOOLEAN 387 EFIAPI 388 CallRsaOaepDecrypt ( 389 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx, 390 CONST UINT8 *CipherText, 391 UINTN CipherTextSize, 392 UINT16 DigestLen, 393 UINT8 **ClearText, 394 UINTN *ClearTextSize 395 ) 396 { 397 VOID *RsaContext = NULL; 398 BOOLEAN Status; 399 400 RsaContext = RsaNew (); 401 UT_ASSERT_FALSE (RsaContext == NULL); 402 403 Status = RsaSetKey (RsaContext, RsaKeyN, TestCtx->RsaN, TestCtx->RsaNSize); 404 UT_ASSERT_TRUE (Status); 405 406 Status = RsaSetKey (RsaContext, RsaKeyE, TestCtx->RsaE, TestCtx->RsaESize); 407 UT_ASSERT_TRUE (Status); 408 409 Status = RsaSetKey (RsaContext, RsaKeyD, TestCtx->RsaD, TestCtx->RsaDSize); 410 UT_ASSERT_TRUE (Status); 411 412 Status = RsaOaepDecrypt ( 413 RsaContext, 414 (UINT8 *)CipherText, 415 CipherTextSize, 416 DigestLen, 417 ClearText, 418 ClearTextSize 419 ); 420 421 return Status; 422 } 423 156 424 UNIT_TEST_STATUS 157 425 EFIAPI 158 TestVerify OaepEncrypt (426 TestVerifyEncrypt ( 159 427 IN UNIT_TEST_CONTEXT Context 160 428 ) 161 429 { 162 BOOLEAN Status; 163 UINT8 File[4]; 164 UINT8 *OutBuffer; 165 UINTN OutBufferSize; 166 UINT8 *OutBuffer2; 167 UINTN OutBuffer2Size; 168 169 // Create a file and add content '123' in it 170 File[0] = '1'; 171 File[1] = '2'; 172 File[2] = '3'; 173 File[3] = 0; 174 175 OutBuffer = NULL; 176 OutBufferSize = 0; 177 OutBuffer2 = NULL; 178 OutBuffer2Size = 0; 179 180 Status = Pkcs1v2Encrypt ( 181 SelfTestCert, 182 (UINTN)sizeof (SelfTestCert), 183 File, 184 (UINTN)sizeof (File), 185 NULL, 186 0, 187 &OutBuffer, 188 (UINTN *)&OutBufferSize 189 ); 190 UT_ASSERT_TRUE (Status); 191 192 Status = Pkcs1v2Encrypt ( 193 SelfTestCert, 194 (UINTN)sizeof (SelfTestCert), 195 File, 196 (UINTN)4, 197 NULL, 198 0, 199 &OutBuffer2, 200 (UINTN *)&OutBuffer2Size 201 ); 430 BOOLEAN Status; 431 UINT8 *OutBuffer = NULL; 432 UINTN OutBufferSize = 0; 433 UINT8 *OutBuffer2 = NULL; 434 UINTN OutBuffer2Size = 0; 435 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx; 436 437 TestCtx = (OAEP_ENC_DEC_TEST_CONTEXT *)Context; 438 439 Status = TestCtx->Encrypt ( 440 TestCtx, 441 TestCtx->ClearText, 442 TestCtx->ClearTextSize, 443 TestCtx->PrngSeed, 444 TestCtx->PrngSeedSize, 445 TestCtx->DigestLen, 446 &OutBuffer, 447 &OutBufferSize 448 ); 449 UT_ASSERT_TRUE (Status); 450 451 Status = TestCtx->Encrypt ( 452 TestCtx, 453 TestCtx->ClearText, 454 TestCtx->ClearTextSize, 455 TestCtx->PrngSeed, 456 TestCtx->PrngSeedSize, 457 TestCtx->DigestLen, 458 &OutBuffer2, 459 &OutBuffer2Size 460 ); 202 461 UT_ASSERT_TRUE (Status); 203 462 … … 218 477 } 219 478 220 Status = Pkcs1v2Encrypt ( 221 SelfTestCert, 222 (UINTN)sizeof (SelfTestCert), 223 File, 224 (UINTN)4, 225 RandSeed, 226 (UINTN)sizeof (RandSeed), 227 &OutBuffer, 228 (UINTN *)&OutBufferSize 229 ); 230 UT_ASSERT_TRUE (Status); 231 232 Status = Pkcs1v2Encrypt ( 233 SelfTestCert, 234 (UINTN)sizeof (SelfTestCert), 235 File, 236 (UINTN)4, 237 RandSeed, 238 (UINTN)sizeof (RandSeed), 239 &OutBuffer2, 240 (UINTN *)&OutBuffer2Size 241 ); 242 UT_ASSERT_TRUE (Status); 243 244 // TRUE - the two OutBuffers are indentical. That means the Oaep encrypt result is incorrect. 245 Status = (CompareMem (OutBuffer, OutBuffer2, OutBufferSize >= OutBuffer2Size ? OutBufferSize : OutBuffer2Size) == 0); 246 UT_ASSERT_FALSE (Status); 479 return UNIT_TEST_PASSED; 480 } 481 482 UNIT_TEST_STATUS 483 EFIAPI 484 TestVerifyDecrypt ( 485 IN UNIT_TEST_CONTEXT Context 486 ) 487 { 488 BOOLEAN Status; 489 UINT8 *OutBuffer = NULL; 490 UINTN OutBufferSize = 0; 491 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx; 492 493 TestCtx = Context; 494 495 Status = TestCtx->Decrypt ( 496 TestCtx, 497 TestCtx->CipherText, 498 TestCtx->CipherTextSize, 499 TestCtx->DigestLen, 500 &OutBuffer, 501 &OutBufferSize 502 ); 503 UT_ASSERT_TRUE (Status); 504 505 UT_ASSERT_TRUE (CompareMem (OutBuffer, TestCtx->ClearText, OutBufferSize >= TestCtx->ClearTextSize ? OutBufferSize : TestCtx->ClearTextSize) == 0); 506 UT_ASSERT_TRUE (OutBufferSize == TestCtx->ClearTextSize); 247 507 248 508 if (OutBuffer) { … … 252 512 } 253 513 254 if (OutBuffer2) { 255 FreePool (OutBuffer2); 256 OutBuffer2 = NULL; 257 OutBuffer2Size = 0; 514 return UNIT_TEST_PASSED; 515 } 516 517 UNIT_TEST_STATUS 518 EFIAPI 519 TestVerifyEncryptDecrypt ( 520 IN UNIT_TEST_CONTEXT Context 521 ) 522 { 523 BOOLEAN Status; 524 UINT8 *ClearText = NULL; 525 UINTN ClearTextSize = 0; 526 UINT8 *CipherText = NULL; 527 UINTN CipherTextSize = 0; 528 OAEP_ENC_DEC_TEST_CONTEXT *TestCtx; 529 530 TestCtx = Context; 531 532 Status = TestCtx->Encrypt ( 533 TestCtx, 534 TestCtx->ClearText, 535 TestCtx->ClearTextSize, 536 TestCtx->PrngSeed, 537 TestCtx->PrngSeedSize, 538 TestCtx->DigestLen, 539 &CipherText, 540 &CipherTextSize 541 ); 542 UT_ASSERT_TRUE (Status); 543 544 Status = TestCtx->Decrypt ( 545 TestCtx, 546 CipherText, 547 CipherTextSize, 548 TestCtx->DigestLen, 549 &ClearText, 550 &ClearTextSize 551 ); 552 553 if (TestCtx->Expect == UNIT_TEST_PASSED) { 554 UT_ASSERT_TRUE (Status); 555 } else { 556 UT_ASSERT_FALSE (Status); 258 557 } 558 559 if (TestCtx->Expect == UNIT_TEST_PASSED) { 560 UT_ASSERT_TRUE (CompareMem (ClearText, TestCtx->ClearText, ClearTextSize >= TestCtx->ClearTextSize ? ClearTextSize : TestCtx->ClearTextSize) == 0); 561 UT_ASSERT_TRUE (ClearTextSize == TestCtx->ClearTextSize); 562 } 563 564 if (CipherText) { 565 FreePool (CipherText); 566 CipherText = NULL; 567 CipherTextSize = 0; 568 } 569 570 if (ClearText) { 571 FreePool (ClearText); 572 ClearText = NULL; 573 ClearTextSize = 0; 574 } 575 576 return UNIT_TEST_PASSED; 577 } 578 579 UNIT_TEST_STATUS 580 EFIAPI 581 TestVerifyPkcs1v2EncryptInterface ( 582 IN UNIT_TEST_CONTEXT Context 583 ) 584 { 585 BOOLEAN Status; 586 UINT8 *OutBuffer; 587 UINTN OutBufferSize; 259 588 260 589 Status = Pkcs1v2Encrypt ( 261 590 NULL, 262 591 (UINTN)sizeof (SelfTestCert), 263 File,592 (UINT8 *)Msg1230, 264 593 (UINTN)4, 265 594 NULL, … … 273 602 SelfTestCert, 274 603 (UINTN)sizeof (SelfTestCert), 275 File,604 (UINT8 *)Msg1230, 276 605 (UINTN)4, 277 606 NULL, … … 285 614 SelfTestCert, 286 615 (UINTN)sizeof (SelfTestCert), 287 File,616 (UINT8 *)Msg1230, 288 617 (UINTN)4, 289 618 NULL, … … 297 626 } 298 627 628 UNIT_TEST_STATUS 629 EFIAPI 630 TestVerifyRsaOaepEncryptInterface ( 631 IN UNIT_TEST_CONTEXT Context 632 ) 633 { 634 VOID *RsaContext = NULL; 635 BOOLEAN Status; 636 UINT8 *OutBuffer; 637 UINTN OutBufferSize; 638 639 RsaContext = RsaNew (); 640 UT_ASSERT_FALSE (RsaContext == NULL); 641 642 Status = RsaSetKey (RsaContext, RsaKeyN, RsaN, sizeof (RsaN)); 643 UT_ASSERT_TRUE (Status); 644 645 Status = RsaSetKey (RsaContext, RsaKeyE, RsaE, sizeof (RsaE)); 646 UT_ASSERT_TRUE (Status); 647 648 Status = RsaOaepEncrypt ( 649 NULL, 650 (UINT8 *)Msg1230, 651 (UINTN)4, 652 NULL, 653 0, 654 0, 655 &OutBuffer, 656 (UINTN *)&OutBufferSize 657 ); 658 UT_ASSERT_FALSE (Status); 659 660 Status = RsaOaepEncrypt ( 661 RsaContext, 662 (UINT8 *)Msg1230, 663 (UINTN)4, 664 NULL, 665 0, 666 0, 667 (UINT8 **)NULL, 668 (UINTN *)&OutBufferSize 669 ); 670 UT_ASSERT_FALSE (Status); 671 672 Status = RsaOaepEncrypt ( 673 RsaContext, 674 (UINT8 *)Msg1230, 675 (UINTN)4, 676 NULL, 677 0, 678 0, 679 &OutBuffer, 680 (UINTN *)NULL 681 ); 682 UT_ASSERT_FALSE (Status); 683 684 return UNIT_TEST_PASSED; 685 } 686 687 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyPkcs1v2Msg1230 = { 688 .SelfTestCert = SelfTestCert, 689 .SelfTestCertSize = sizeof (SelfTestCert), 690 .PrivateKey = PrivateKey, 691 .PrivateKeySize = sizeof (PrivateKey), 692 .RsaN = NULL, 693 .RsaNSize = 0, 694 .RsaE = NULL, 695 .RsaESize = 0, 696 .RsaD = NULL, 697 .RsaDSize = 0, 698 .PrngSeed = NULL, 699 .PrngSeedSize = 0, 700 .ClearText = Msg1230, 701 .ClearTextSize = sizeof (Msg1230), 702 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 703 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 704 .DigestLen = 0, 705 .Encrypt = CallPkcs1v2Encrypt, 706 .Decrypt = CallPkcs1v2Decrypt, 707 .Expect = UNIT_TEST_PASSED 708 }; 709 710 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyPkcs1v2Msg1230PrngSeed = { 711 .SelfTestCert = SelfTestCert, 712 .SelfTestCertSize = sizeof (SelfTestCert), 713 .PrivateKey = PrivateKey, 714 .PrivateKeySize = sizeof (PrivateKey), 715 .RsaN = NULL, 716 .RsaNSize = 0, 717 .RsaE = NULL, 718 .RsaESize = 0, 719 .RsaD = NULL, 720 .RsaDSize = 0, 721 .PrngSeed = RandSeed, 722 .PrngSeedSize = sizeof (RandSeed), 723 .ClearText = Msg1230, 724 .ClearTextSize = sizeof (Msg1230), 725 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 726 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 727 .DigestLen = 0, 728 .Encrypt = CallPkcs1v2Encrypt, 729 .Decrypt = CallPkcs1v2Decrypt, 730 .Expect = UNIT_TEST_PASSED 731 }; 732 733 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaOaepMsg1230 = { 734 .SelfTestCert = NULL, 735 .SelfTestCertSize = 0, 736 .PrivateKey = NULL, 737 .PrivateKeySize = 0, 738 .RsaN = RsaN, 739 .RsaNSize = sizeof (RsaN), 740 .RsaE = RsaE, 741 .RsaESize = sizeof (RsaE), 742 .RsaD = RsaD, 743 .RsaDSize = sizeof (RsaD), 744 .PrngSeed = NULL, 745 .PrngSeedSize = 0, 746 .ClearText = Msg1230, 747 .ClearTextSize = sizeof (Msg1230), 748 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 749 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 750 .DigestLen = 0, 751 .Encrypt = CallRsaOaepEncrypt, 752 .Decrypt = CallRsaOaepDecrypt, 753 .Expect = UNIT_TEST_PASSED 754 }; 755 756 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaOaepMsg1230PrngSeed = { 757 .SelfTestCert = NULL, 758 .SelfTestCertSize = 0, 759 .PrivateKey = NULL, 760 .PrivateKeySize = 0, 761 .RsaN = RsaN, 762 .RsaNSize = sizeof (RsaN), 763 .RsaE = RsaE, 764 .RsaESize = sizeof (RsaE), 765 .RsaD = RsaD, 766 .RsaDSize = sizeof (RsaD), 767 .PrngSeed = RandSeed, 768 .PrngSeedSize = sizeof (RandSeed), 769 .ClearText = Msg1230, 770 .ClearTextSize = sizeof (Msg1230), 771 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 772 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 773 .DigestLen = 0, 774 .Encrypt = CallRsaOaepEncrypt, 775 .Decrypt = CallRsaOaepDecrypt, 776 .Expect = UNIT_TEST_PASSED 777 }; 778 779 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyPkcs1v2EncryptRsaOaepDecrypt = { 780 .SelfTestCert = SelfTestCert, 781 .SelfTestCertSize = sizeof (SelfTestCert), 782 .PrivateKey = PrivateKey, 783 .PrivateKeySize = sizeof (PrivateKey), 784 .RsaN = RsaN, 785 .RsaNSize = sizeof (RsaN), 786 .RsaE = RsaE, 787 .RsaESize = sizeof (RsaE), 788 .RsaD = RsaD, 789 .RsaDSize = sizeof (RsaD), 790 .ClearText = Msg1230, 791 .ClearTextSize = sizeof (Msg1230), 792 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 793 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 794 .DigestLen = 0, 795 .Encrypt = CallPkcs1v2Encrypt, 796 .Decrypt = CallRsaOaepDecrypt, 797 .Expect = UNIT_TEST_PASSED 798 }; 799 800 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaOaepEncryptPkcs1v2Decrypt = { 801 .SelfTestCert = SelfTestCert, 802 .SelfTestCertSize = sizeof (SelfTestCert), 803 .PrivateKey = PrivateKey, 804 .PrivateKeySize = sizeof (PrivateKey), 805 .RsaN = RsaN, 806 .RsaNSize = sizeof (RsaN), 807 .RsaE = RsaE, 808 .RsaESize = sizeof (RsaE), 809 .RsaD = RsaD, 810 .RsaDSize = sizeof (RsaD), 811 .ClearText = Msg1230, 812 .ClearTextSize = sizeof (Msg1230), 813 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 814 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 815 .DigestLen = 0, 816 .Encrypt = CallRsaOaepEncrypt, 817 .Decrypt = CallPkcs1v2Decrypt, 818 .Expect = UNIT_TEST_PASSED 819 }; 820 821 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaesOaep2048MdDefaultBgf1Default = { 822 .SelfTestCert = NULL, 823 .SelfTestCertSize = 0, 824 .PrivateKey = NULL, 825 .PrivateKeySize = 0, 826 .RsaN = RsaN, 827 .RsaNSize = sizeof (RsaN), 828 .RsaE = RsaE, 829 .RsaESize = sizeof (RsaE), 830 .RsaD = RsaD, 831 .RsaDSize = sizeof (RsaD), 832 .ClearText = Msg1230, 833 .ClearTextSize = sizeof (Msg1230), 834 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 835 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 836 .DigestLen = 0, 837 .Encrypt = CallRsaOaepEncrypt, 838 .Decrypt = CallRsaOaepDecrypt, 839 .Expect = UNIT_TEST_PASSED 840 }; 841 842 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaesOaep2048MdSha1Mgf1Sha1 = { 843 .SelfTestCert = NULL, 844 .SelfTestCertSize = 0, 845 .PrivateKey = NULL, 846 .PrivateKeySize = 0, 847 .RsaN = RsaN, 848 .RsaNSize = sizeof (RsaN), 849 .RsaE = RsaE, 850 .RsaESize = sizeof (RsaE), 851 .RsaD = RsaD, 852 .RsaDSize = sizeof (RsaD), 853 .ClearText = Msg1230, 854 .ClearTextSize = sizeof (Msg1230), 855 .CipherText = Ct1230RsaesOaepMdSha1Mgf1Sha1, 856 .CipherTextSize = sizeof (Ct1230RsaesOaepMdSha1Mgf1Sha1), 857 .DigestLen = SHA1_DIGEST_SIZE, 858 .Encrypt = CallRsaOaepEncrypt, 859 .Decrypt = CallRsaOaepDecrypt, 860 .Expect = UNIT_TEST_PASSED 861 }; 862 863 OAEP_ENC_DEC_TEST_CONTEXT mTestVerifyRsaesOaep2048MdSha256Mgf1Sha256 = { 864 .SelfTestCert = NULL, 865 .SelfTestCertSize = 0, 866 .PrivateKey = NULL, 867 .PrivateKeySize = 0, 868 .RsaN = RsaN, 869 .RsaNSize = sizeof (RsaN), 870 .RsaE = RsaE, 871 .RsaESize = sizeof (RsaE), 872 .RsaD = RsaD, 873 .RsaDSize = sizeof (RsaD), 874 .ClearText = Msg1230, 875 .ClearTextSize = sizeof (Msg1230), 876 .CipherText = Ct1230RsaesOaep2048MdSha256Mgf1Sha256, 877 .CipherTextSize = sizeof (Ct1230RsaesOaep2048MdSha256Mgf1Sha256), 878 .DigestLen = SHA256_DIGEST_SIZE, 879 .Encrypt = CallRsaOaepEncrypt, 880 .Decrypt = CallRsaOaepDecrypt, 881 .Expect = UNIT_TEST_PASSED 882 }; 883 299 884 TEST_DESC mOaepTest[] = { 300 885 // 301 886 // -----Description--------------------------------------Class----------------------Function-----------------Pre---Post--Context 302 887 // 303 { "TestVerifyOaepEncrypt()", "CryptoPkg.BaseCryptLib.Pkcs1v2Encrypt", TestVerifyOaepEncrypt, NULL, NULL, NULL }, 888 // Pkcs1v2Encrypt / Decrypt 889 { "Pkcs1v2Encrypt (Interface)", "CryptoPkg.BaseCryptLib.Pkcs1v2Encrypt.Interface", TestVerifyPkcs1v2EncryptInterface, NULL, NULL, &mTestVerifyPkcs1v2Msg1230 }, 890 { "Pkcs1v2Encrypt (NoSeed)", "CryptoPkg.BaseCryptLib.Pkcs1v2Encrypt.NoSeed", TestVerifyEncrypt, NULL, NULL, &mTestVerifyPkcs1v2Msg1230 }, 891 { "Pkcs1v2Encrypt (Seeded)", "CryptoPkg.BaseCryptLib.Pkcs1v2Encrypt.Seeded", TestVerifyEncrypt, NULL, NULL, &mTestVerifyPkcs1v2Msg1230PrngSeed }, 892 { "Pkcs1v2Decrypt", "CryptoPkg.BaseCryptLib.Pkcs1v2Decrypt", TestVerifyDecrypt, NULL, NULL, &mTestVerifyPkcs1v2Msg1230 }, 893 { "Pkcs1v2EncryptDecrypt", "CryptoPkg.BaseCryptLib.Pkcs1v2EncryptDecrypt", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyPkcs1v2Msg1230 }, 894 895 // RsaOaepEncrypt / Decrypt 896 { "RsaOaepEncrypt (Interface)", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.Interface", TestVerifyRsaOaepEncryptInterface, NULL, NULL, &mTestVerifyRsaOaepMsg1230 }, 897 { "RsaOaepEncrypt (NoSeed)", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.NoSeed", TestVerifyEncrypt, NULL, NULL, &mTestVerifyRsaOaepMsg1230 }, 898 { "RsaOaepEncrypt (Seeded)", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.Seeded", TestVerifyEncrypt, NULL, NULL, &mTestVerifyRsaOaepMsg1230PrngSeed }, 899 { "RsaOaepDecrypt", "CryptoPkg.BaseCryptLib.RsaOaepDecrypt", TestVerifyDecrypt, NULL, NULL, &mTestVerifyRsaOaepMsg1230 }, 900 { "RsaOaepEncryptDecrypt", "CryptoPkg.BaseCryptLib.RsaOaepEncryptDecrypt", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyRsaOaepMsg1230 }, 901 902 // Mix interfaces 903 { "RsaOaepEncryptPkcs1v2Decrypt", "CryptoPkg.BaseCryptLib.RsaOaepEncryptPkcs1v2Decrypt", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyRsaOaepEncryptPkcs1v2Decrypt }, 904 { "Pkcs1v2EncryptRsaOaepDecrypt", "CryptoPkg.BaseCryptLib.Pkcs1v2EncryptRsaOaepDecrypt", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyPkcs1v2EncryptRsaOaepDecrypt }, 905 906 // Message digest default / MGF1 default (SHA1) 907 { "RsaOaepEncrypt (MdDefaultMgf1Default)", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.MdDefaultMgf1Default", TestVerifyEncrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdDefaultBgf1Default }, 908 { "RsaOaepDecrypt (MdDefaultMgf1Default)", "CryptoPkg.BaseCryptLib.RsaOaepDecrypt.MdDefaultMgf1Default", TestVerifyDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdDefaultBgf1Default }, 909 { "RsaOaepEncryptDecrypt (MdDefaultMgf1Default)", "CryptoPkg.BaseCryptLib.RsaOaepEncryptDecrypt.MdDefaultMgf1Default", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdDefaultBgf1Default }, 910 911 // Message digest SHA1 / MGF1 SHA1 912 { "RsaOaepEncrypt (MdSha1Bgf1Sha1", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.MdSha1Bgf1Sha1", TestVerifyEncrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha1Mgf1Sha1 }, 913 { "RsaOaepDecrypt (MdSha1Bgf1Sha1)", "CryptoPkg.BaseCryptLib.RsaOaepDecrypt.MdSha1Bgf1Sha1", TestVerifyDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha1Mgf1Sha1 }, 914 { "RsaOaepEncryptDecrypt (MdSha1Bgf1Sha1)", "CryptoPkg.BaseCryptLib.RsaOaepEncryptDecrypt.MdSha1Bgf1Sha1", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha1Mgf1Sha1 }, 915 916 // Message digest SHA256 / MGF1 SHA256 917 { "RsaOaepEncrypt (MdSha256Bgf1Sha256)", "CryptoPkg.BaseCryptLib.RsaOaepEncrypt.MdSha256Bgf1Sha256", TestVerifyEncrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha256Mgf1Sha256 }, 918 { "RsaOaepDecrypt (MdSha256Bgf1Sha256)", "CryptoPkg.BaseCryptLib.RsaOaepDecrypt.MdSha256Bgf1Sha256", TestVerifyDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha256Mgf1Sha256 }, 919 { "RsaOaepEncryptDecrypt (MdSha256Bgf1Sha256)", "CryptoPkg.BaseCryptLib.RsaOaepEncryptDecryptMdSha256Bgf1Sha256", TestVerifyEncryptDecrypt, NULL, NULL, &mTestVerifyRsaesOaep2048MdSha256Mgf1Sha256 }, 304 920 }; 305 921 -
trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c
r99404 r105670 195 195 UINTN KeySize; 196 196 UINT8 *KeyBuffer; 197 UINT8 TestPublicExponent1[] = { 0x03 }; 198 UINT8 TestPublicExponent2[] = { 0x01, 0x01 }; 199 UINT8 TestPublicExponent3[] = { 0x01, 0x00, 0x01 }; 197 200 198 201 // … … 201 204 202 205 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, NULL, 0); 206 UT_ASSERT_TRUE (Status); 207 208 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent1, sizeof (TestPublicExponent1)); 209 UT_ASSERT_TRUE (Status); 210 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent2, sizeof (TestPublicExponent2)); 211 UT_ASSERT_TRUE (Status); 212 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent3, sizeof (TestPublicExponent3)); 203 213 UT_ASSERT_TRUE (Status); 204 214
Note:
See TracChangeset
for help on using the changeset viewer.