To follow UEFI spec, update the code to make correct check to support multiple _ADR...
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLibRuntimeCryptProtocol / RuntimeDxeIpfCryptLib.c
CommitLineData
97f98500
HT
1/** @file\r
2 Implementation of The runtime cryptographic library instance (for IPF).\r
3\r
16d2c32c 4Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/BaseLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
20#include <Library/UefiRuntimeLib.h>\r
21\r
22#include <Protocol/RuntimeCrypt.h>\r
23\r
24#include <Guid/EventGroup.h>\r
25\r
26EFI_RUNTIME_CRYPT_PROTOCOL *mCryptProtocol = NULL;\r
27EFI_EVENT mIpfCryptLibVirtualNotifyEvent;\r
28\r
29/**\r
30 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, which converts\r
31 pointer to new virtual address.\r
32\r
33 @param Event Event whose notification function is being invoked.\r
34 @param Context Pointer to the notification function's context\r
35\r
36**/\r
37VOID\r
38EFIAPI\r
39IpfCryptLibAddressChangeEvent (\r
40 IN EFI_EVENT Event,\r
41 IN VOID *Context\r
42 )\r
43{\r
44 //\r
45 // Convert Address of Runtime Crypto Protocol.\r
46 //\r
47 EfiConvertPointer (0x0, (VOID **) &mCryptProtocol);\r
48}\r
49\r
50/**\r
51 Constructor of IPF Crypto Library Instance.\r
52 This function locates the Runtime Crypt Protocol and register notification\r
53 function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
54\r
55 @param ImageHandle The firmware allocated handle for the EFI image.\r
56 @param SystemTable A pointer to the EFI System Table.\r
57\r
58 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
59\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63RuntimeDxeIpfCryptLibConstructor (\r
64 IN EFI_HANDLE ImageHandle,\r
65 IN EFI_SYSTEM_TABLE *SystemTable\r
66 )\r
67{\r
68 EFI_STATUS Status;\r
69\r
70 //\r
71 // Locate Runtime Crypt Protocol Instance\r
72 //\r
73 Status = gBS->LocateProtocol (\r
74 &gEfiRuntimeCryptProtocolGuid,\r
75 NULL,\r
76 (VOID**) &mCryptProtocol\r
77 );\r
78 ASSERT_EFI_ERROR (Status);\r
79 ASSERT (mCryptProtocol != NULL);\r
80\r
81 //\r
82 // Register SetVirtualAddressMap () notify function\r
83 //\r
84 Status = gBS->CreateEventEx (\r
85 EVT_NOTIFY_SIGNAL,\r
86 TPL_NOTIFY,\r
87 IpfCryptLibAddressChangeEvent,\r
88 NULL,\r
89 &gEfiEventVirtualAddressChangeGuid,\r
90 &mIpfCryptLibVirtualNotifyEvent\r
91 );\r
92 ASSERT_EFI_ERROR (Status);\r
93\r
94 return Status;\r
95}\r
96\r
97/**\r
98 Destructor of IPF Crypto Library Instance.\r
99\r
100 @param ImageHandle The firmware allocated handle for the EFI image.\r
101 @param SystemTable A pointer to the EFI System Table.\r
102\r
103 @retval EFI_SUCCESS The destructor completed successfully.\r
104 @retval Other value The destructor did not complete successfully.\r
105\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109RuntimeDxeIpfCryptLibDestructor (\r
110 IN EFI_HANDLE ImageHandle,\r
111 IN EFI_SYSTEM_TABLE *SystemTable\r
112 )\r
113{\r
114 EFI_STATUS Status;\r
115\r
116 //\r
117 // Close the Set Virtual Address Map event\r
118 //\r
119 Status = gBS->CloseEvent (mIpfCryptLibVirtualNotifyEvent);\r
120 ASSERT_EFI_ERROR (Status);\r
121\r
122 return Status;\r
123}\r
124\r
125/**\r
126 Check whether crypto service provided by Runtime Crypt protocol is ready to use.\r
127\r
128 Crypto service is available if the call is in physical mode prior to\r
129 SetVirtualAddressMap() or virtual mode after SetVirtualAddressMap(). If either\r
130 of these two conditions are met, this routine will return TRUE; if neither of\r
131 these conditions are met, this routine will return FALSE.\r
132\r
133 @retval TRUE The Crypto service is ready to use.\r
134 @retval FALSE The Crypto service is not available.\r
135\r
136**/\r
137BOOLEAN\r
138EFIAPI\r
139InternalIsCryptServiveAvailable (\r
140 VOID\r
141 )\r
142{\r
143 INT64 CpuMode;\r
144 BOOLEAN GoneVirtual;\r
145\r
146 CpuMode = AsmCpuVirtual();\r
147 if (CpuMode < 0) {\r
148 //\r
149 // CPU is in mixed mode, return failing the operation gracefully.\r
150 //\r
151 return FALSE;\r
152 }\r
153\r
154 GoneVirtual = EfiGoneVirtual();\r
155\r
156 if ((CpuMode > 0) && !GoneVirtual) {\r
157 //\r
158 // CPU is in virtual mode, but SetVirtualAddressMap() has not been called,\r
159 // so return failing the operation gracefully.\r
160 //\r
161 return FALSE;\r
162 }\r
163\r
164 if ((CpuMode == 0) && GoneVirtual) {\r
165 //\r
166 // CPU is in physical mode, but SetVirtualAddressMap() has been called,\r
167 // so return failing the operation gracefully.\r
168 //\r
169 return FALSE;\r
170 }\r
171\r
172 return TRUE;\r
173}\r
174\r
175/**\r
176 Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.\r
177\r
178 @return The size, in bytes, of the context buffer required for SHA-256 operations.\r
179\r
180**/\r
181UINTN\r
182EFIAPI\r
183Sha256GetContextSize (\r
184 VOID\r
185 )\r
186{\r
187 if (!InternalIsCryptServiveAvailable ()) {\r
188 return 0;\r
189 }\r
190\r
191 return mCryptProtocol->Sha256GetContextSize ();\r
192}\r
193\r
194/**\r
195 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
196 subsequent use.\r
197\r
16d2c32c 198 If Sha256Context is NULL, then return FALSE.\r
97f98500
HT
199\r
200 @param[in, out] Sha256Context Pointer to SHA-256 Context being initialized.\r
201\r
202 @retval TRUE SHA-256 context initialization succeeded.\r
203 @retval FALSE SHA-256 context initialization failed.\r
204\r
205**/\r
206BOOLEAN\r
207EFIAPI\r
208Sha256Init (\r
209 IN OUT VOID *Sha256Context\r
210 )\r
211{\r
212 if (!InternalIsCryptServiveAvailable ()) {\r
213 return FALSE;\r
214 }\r
215\r
216 return mCryptProtocol->Sha256Init (Sha256Context);\r
217}\r
218\r
219\r
220/**\r
221 Performs SHA-256 digest on a data buffer of the specified length. This function can\r
222 be called multiple times to compute the digest of long or discontinuous data streams.\r
223\r
16d2c32c 224 If Sha256Context is NULL, then return FALSE.\r
97f98500
HT
225\r
226 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
227 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
228 @param[in] DataLength Length of Data buffer in bytes.\r
229\r
230 @retval TRUE SHA-256 data digest succeeded.\r
231 @retval FALSE Invalid SHA-256 context. After Sha256Final function has been called, the\r
232 SHA-256 context cannot be reused.\r
233\r
234**/\r
235BOOLEAN\r
236EFIAPI\r
237Sha256Update (\r
238 IN OUT VOID *Sha256Context,\r
239 IN CONST VOID *Data,\r
240 IN UINTN DataLength\r
241 )\r
242{\r
243 if (!InternalIsCryptServiveAvailable ()) {\r
244 return FALSE;\r
245 }\r
246\r
247 return mCryptProtocol->Sha256Update (Sha256Context, Data, DataLength);\r
248}\r
249\r
250/**\r
251 Completes SHA-256 hash computation and retrieves the digest value into the specified\r
252 memory. After this function has been called, the SHA-256 context cannot be used again.\r
253\r
16d2c32c 254 If Sha256Context is NULL, then return FALSE.\r
255 If HashValue is NULL, then return FALSE.\r
97f98500
HT
256\r
257 @param[in, out] Sha256Context Pointer to SHA-256 context\r
258 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
259 value (32 bytes).\r
260\r
261 @retval TRUE SHA-256 digest computation succeeded.\r
262 @retval FALSE SHA-256 digest computation failed.\r
263\r
264**/\r
265BOOLEAN\r
266EFIAPI\r
267Sha256Final (\r
268 IN OUT VOID *Sha256Context,\r
269 OUT UINT8 *HashValue\r
270 )\r
271{\r
272 if (!InternalIsCryptServiveAvailable ()) {\r
273 return FALSE;\r
274 }\r
275\r
276 return mCryptProtocol->Sha256Final (Sha256Context, HashValue);\r
277}\r
278\r
279/**\r
280 Allocates and Initializes one RSA Context for subsequent use.\r
281\r
282 @return Pointer to the RSA Context that has been initialized.\r
283 If the allocations fails, RsaNew() returns NULL.\r
284\r
285**/\r
286VOID *\r
287EFIAPI\r
288RsaNew (\r
289 VOID\r
290 )\r
291{\r
292 if (!InternalIsCryptServiveAvailable ()) {\r
293 return FALSE;\r
294 }\r
295\r
296 return mCryptProtocol->RsaNew ();\r
297}\r
298\r
299/**\r
300 Release the specified RSA Context.\r
301\r
302 @param[in] RsaContext Pointer to the RSA context to be released.\r
303\r
304**/\r
305VOID\r
306EFIAPI\r
307RsaFree (\r
308 IN VOID *RsaContext\r
309 )\r
310{\r
311 if (!InternalIsCryptServiveAvailable ()) {\r
312 return;\r
313 }\r
314\r
315 mCryptProtocol->RsaFree (RsaContext);\r
316}\r
317\r
318/**\r
319 Sets the tag-designated RSA key component into the established RSA context from\r
320 the user-specified nonnegative integer (octet string format represented in RSA\r
321 PKCS#1).\r
322\r
16d2c32c 323 If RsaContext is NULL, then return FALSE.\r
97f98500
HT
324\r
325 @param[in, out] RsaContext Pointer to RSA context being set.\r
326 @param[in] KeyTag Tag of RSA key component being set.\r
327 @param[in] BigNumber Pointer to octet integer buffer.\r
328 @param[in] BnLength Length of big number buffer in bytes.\r
329\r
330 @return TRUE RSA key component was set successfully.\r
331 @return FALSE Invalid RSA key component tag.\r
332\r
333**/\r
334BOOLEAN\r
335EFIAPI\r
336RsaSetKey (\r
337 IN OUT VOID *RsaContext,\r
338 IN RSA_KEY_TAG KeyTag,\r
339 IN CONST UINT8 *BigNumber,\r
340 IN UINTN BnLength\r
341 )\r
342{\r
343 if (!InternalIsCryptServiveAvailable ()) {\r
344 return FALSE;\r
345 }\r
346\r
347 return mCryptProtocol->RsaSetKey (RsaContext, KeyTag, BigNumber, BnLength);\r
348}\r
349\r
350/**\r
351 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
352 RSA PKCS#1.\r
353\r
16d2c32c 354 If RsaContext is NULL, then return FALSE.\r
355 If MessageHash is NULL, then return FALSE.\r
356 If Signature is NULL, then return FALSE.\r
357 If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
97f98500
HT
358\r
359 @param[in] RsaContext Pointer to RSA context for signature verification.\r
360 @param[in] MessageHash Pointer to octet message hash to be checked.\r
361 @param[in] HashLength Length of the message hash in bytes.\r
362 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.\r
363 @param[in] SigLength Length of signature in bytes.\r
364\r
365 @return TRUE Valid signature encoded in PKCS1-v1_5.\r
366 @return FALSE Invalid signature or invalid RSA context.\r
367\r
368**/\r
369BOOLEAN\r
370EFIAPI\r
371RsaPkcs1Verify (\r
372 IN VOID *RsaContext,\r
373 IN CONST UINT8 *MessageHash,\r
374 IN UINTN HashLength,\r
375 IN UINT8 *Signature,\r
376 IN UINTN SigLength\r
377 )\r
378{\r
379 if (!InternalIsCryptServiveAvailable ()) {\r
380 return FALSE;\r
381 }\r
382\r
383 return mCryptProtocol->RsaPkcs1Verify (\r
384 RsaContext,\r
385 MessageHash,\r
386 HashLength,\r
387 Signature,\r
388 SigLength\r
389 );\r
390}\r