TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptPkcs1Oaep.c
Go to the documentation of this file.
1
10#include "InternalCryptLib.h"
11#include <mbedtls/rsa.h>
12#include <mbedtls/x509_crt.h>
14
44BOOLEAN
45EFIAPI
47 IN CONST UINT8 *PublicKey,
48 IN UINTN PublicKeySize,
49 IN UINT8 *InData,
50 IN UINTN InDataSize,
51 IN CONST UINT8 *PrngSeed OPTIONAL,
52 IN UINTN PrngSeedSize OPTIONAL,
53 OUT UINT8 **EncryptedData,
54 OUT UINTN *EncryptedDataSize
55 )
56{
57 BOOLEAN Result;
58 UINT32 Ret;
59 UINT8 *OutData;
60 mbedtls_x509_crt CertContext;
61 mbedtls_rsa_context RsaContext;
62
63 //
64 // Check input parameters.
65 //
66 if ((PublicKey == NULL) || (InData == NULL) ||
67 (EncryptedData == NULL) || (EncryptedDataSize == NULL))
68 {
69 return FALSE;
70 }
71
72 //
73 // Check public key size.
74 //
75 if (PublicKeySize > UINT_MAX) {
76 //
77 // Public key size is too large for implementation.
78 //
79 return FALSE;
80 }
81
82 *EncryptedData = NULL;
83 *EncryptedDataSize = 0;
84 Result = FALSE;
85 OutData = NULL;
86
87 mbedtls_x509_crt_init (&CertContext);
88
89 if (mbedtls_x509_crt_parse_der (&CertContext, PublicKey, (UINT32)PublicKeySize) != 0) {
90 goto _Exit;
91 }
92
93 if (mbedtls_pk_get_type (&CertContext.pk) != MBEDTLS_PK_RSA) {
94 goto _Exit;
95 }
96
97 mbedtls_rsa_init (&RsaContext);
98 if (mbedtls_rsa_set_padding (&RsaContext, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_NONE) != 0) {
99 goto _Exit;
100 }
101
102 Ret = mbedtls_rsa_copy (&RsaContext, mbedtls_pk_rsa (CertContext.pk));
103 if (Ret != 0) {
104 goto _Exit;
105 }
106
107 *EncryptedDataSize = RsaContext.len;
108
109 //
110 // Allocate a buffer for the output data.
111 //
112 OutData = AllocateZeroPool (*EncryptedDataSize);
113 if (OutData == NULL) {
114 //
115 // Fail to allocate the output buffer.
116 //
117 goto _Exit;
118 }
119
120 Ret = mbedtls_rsa_pkcs1_encrypt (
121 &RsaContext,
123 NULL,
124 InDataSize,
125 InData,
126 OutData
127 );
128 if (Ret != 0) {
129 FreePool (OutData);
130 OutData = NULL;
131 goto _Exit;
132 }
133
134 *EncryptedData = OutData;
135 Result = TRUE;
136
137_Exit:
138 //
139 // Release Resources
140 //
141 if (&CertContext != NULL) {
142 mbedtls_x509_crt_free (&CertContext);
143 }
144
145 if (&RsaContext != NULL) {
146 mbedtls_rsa_free (&RsaContext);
147 }
148
149 return Result;
150}
151
185BOOLEAN
186EFIAPI
188 IN VOID *RsaContext,
189 IN UINT8 *InData,
190 IN UINTN InDataSize,
191 IN CONST UINT8 *PrngSeed OPTIONAL,
192 IN UINTN PrngSeedSize OPTIONAL,
193 IN UINT16 DigestLen OPTIONAL,
194 OUT UINT8 **EncryptedData,
195 OUT UINTN *EncryptedDataSize
196 )
197{
198 ASSERT (FALSE);
199 return FALSE;
200}
201
223BOOLEAN
224EFIAPI
226 IN CONST UINT8 *PrivateKey,
227 IN UINTN PrivateKeySize,
228 IN UINT8 *EncryptedData,
229 IN UINTN EncryptedDataSize,
230 OUT UINT8 **OutData,
231 OUT UINTN *OutDataSize
232 )
233{
234 ASSERT (FALSE);
235 return FALSE;
236}
237
265BOOLEAN
266EFIAPI
268 IN VOID *RsaContext,
269 IN UINT8 *EncryptedData,
270 IN UINTN EncryptedDataSize,
271 IN UINT16 DigestLen OPTIONAL,
272 OUT UINT8 **OutData,
273 OUT UINTN *OutDataSize
274 )
275{
276 ASSERT (FALSE);
277 return FALSE;
278}
UINT64 UINTN
INT32 MbedtlsRand(VOID *RngState, UINT8 *Output, UINTN Len)
Definition: CryptRand.c:103
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI Pkcs1v2Encrypt(IN CONST UINT8 *PublicKey, IN UINTN PublicKeySize, IN UINT8 *InData, IN UINTN InDataSize, IN CONST UINT8 *PrngSeed OPTIONAL, IN UINTN PrngSeedSize OPTIONAL, OUT UINT8 **EncryptedData, OUT UINTN *EncryptedDataSize)
BOOLEAN EFIAPI RsaOaepEncrypt(IN VOID *RsaContext, IN UINT8 *InData, IN UINTN InDataSize, IN CONST UINT8 *PrngSeed OPTIONAL, IN UINTN PrngSeedSize OPTIONAL, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **EncryptedData, OUT UINTN *EncryptedDataSize)
BOOLEAN EFIAPI RsaOaepDecrypt(IN VOID *RsaContext, IN UINT8 *EncryptedData, IN UINTN EncryptedDataSize, IN UINT16 DigestLen OPTIONAL, OUT UINT8 **OutData, OUT UINTN *OutDataSize)
BOOLEAN EFIAPI Pkcs1v2Decrypt(IN CONST UINT8 *PrivateKey, IN UINTN PrivateKeySize, IN UINT8 *EncryptedData, IN UINTN EncryptedDataSize, OUT UINT8 **OutData, OUT UINTN *OutDataSize)