]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/CryptRuntimeDxe/CryptRuntime.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / CryptoPkg / CryptRuntimeDxe / CryptRuntime.c
CommitLineData
97f98500
HT
1/** @file\r
2 Runtime Cryptographic Driver Implementation, which produce one crypto\r
3 protocol.\r
4\r
16d2c32c 5Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "CryptRuntime.h"\r
17\r
18//\r
19// The handle onto which the Runtime Crypt Protocol instance is installed\r
20//\r
21EFI_HANDLE mRuntimeCryptHandle = NULL;\r
22\r
23//\r
24// The Runtime Crypt Protocol instance produced by this driver\r
25//\r
26EFI_RUNTIME_CRYPT_PROTOCOL mRuntimeCryptProtocol = {\r
27 RuntimeCryptSha256GetContextSize,\r
28 RuntimeCryptSha256Init,\r
29 RuntimeCryptSha256Update,\r
30 RuntimeCryptSha256Final,\r
31 RuntimeCryptRsaNew,\r
32 RuntimeCryptRsaFree,\r
33 RuntimeCryptRsaSetKey,\r
34 RuntimeCryptRsaPkcs1Verify\r
35};\r
36\r
37/**\r
38 Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.\r
39\r
40 @return The size, in bytes, of the context buffer required for SHA-256 operations.\r
41\r
42**/\r
43UINTN\r
44EFIAPI\r
45RuntimeCryptSha256GetContextSize (\r
46 VOID\r
47 )\r
48{\r
49 return Sha256GetContextSize ();\r
50}\r
51\r
52/**\r
53 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
54 subsequent use.\r
55\r
16d2c32c 56 If Sha256Context is NULL, then return FALSE.\r
97f98500
HT
57\r
58 @param[in, out] Sha256Context Pointer to SHA-256 Context being initialized.\r
59\r
60 @retval TRUE SHA-256 context initialization succeeded.\r
61 @retval FALSE SHA-256 context initialization failed.\r
62\r
63**/\r
64BOOLEAN\r
65EFIAPI\r
66RuntimeCryptSha256Init (\r
67 IN OUT VOID *Sha256Context\r
68 )\r
69{\r
70 return Sha256Init (Sha256Context);\r
71}\r
72\r
73/**\r
74 Performs SHA-256 digest on a data buffer of the specified length. This function can\r
75 be called multiple times to compute the digest of long or discontinuous data streams.\r
76\r
16d2c32c 77 If Sha256Context is NULL, then return FALSE.\r
97f98500
HT
78\r
79 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
80 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
81 @param[in] DataLength Length of Data buffer in bytes.\r
82\r
83 @retval TRUE SHA-256 data digest succeeded.\r
84 @retval FALSE Invalid SHA-256 context. After Sha256Final function has been called, the\r
85 SHA-256 context cannot be reused.\r
86\r
87**/\r
88BOOLEAN\r
89EFIAPI\r
90RuntimeCryptSha256Update (\r
91 IN OUT VOID *Sha256Context,\r
92 IN CONST VOID *Data,\r
93 IN UINTN DataLength\r
94 )\r
95{\r
96 return Sha256Update (Sha256Context, Data, DataLength);\r
97}\r
98\r
99/**\r
100 Completes SHA-256 hash computation and retrieves the digest value into the specified\r
101 memory. After this function has been called, the SHA-256 context cannot be used again.\r
102\r
16d2c32c 103 If Sha256Context is NULL, then return FALSE.\r
104 If HashValue is NULL, then return FALSE.\r
97f98500
HT
105\r
106 @param[in, out] Sha256Context Pointer to SHA-256 context\r
107 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
108 value (32 bytes).\r
109\r
110 @retval TRUE SHA-256 digest computation succeeded.\r
111 @retval FALSE SHA-256 digest computation failed.\r
112\r
113**/\r
114BOOLEAN\r
115EFIAPI\r
116RuntimeCryptSha256Final (\r
117 IN OUT VOID *Sha256Context,\r
118 OUT UINT8 *HashValue\r
119 )\r
120{\r
121 return Sha256Final (Sha256Context, HashValue);\r
122}\r
123\r
124/**\r
125 Allocates and Initializes one RSA Context for subsequent use.\r
126\r
127 @return Pointer to the RSA Context that has been initialized.\r
128 If the allocations fails, RsaNew() returns NULL.\r
129\r
130**/\r
131VOID *\r
132EFIAPI\r
133RuntimeCryptRsaNew (\r
134 VOID\r
135 )\r
136{\r
137 return RsaNew ();\r
138}\r
139\r
140/**\r
141 Release the specified RSA Context.\r
142\r
143 @param[in] RsaContext Pointer to the RSA context to be released.\r
144\r
145**/\r
146VOID\r
147EFIAPI\r
148RuntimeCryptRsaFree (\r
149 IN VOID *RsaContext\r
150 )\r
151{\r
152 RsaFree (RsaContext);\r
153}\r
154\r
155/**\r
156 Sets the tag-designated RSA key component into the established RSA context from\r
157 the user-specified nonnegative integer (octet string format represented in RSA\r
158 PKCS#1).\r
159\r
16d2c32c 160 If RsaContext is NULL, then return FALSE.\r
97f98500
HT
161\r
162 @param[in, out] RsaContext Pointer to RSA context being set.\r
163 @param[in] KeyTag Tag of RSA key component being set.\r
164 @param[in] BigNumber Pointer to octet integer buffer.\r
165 @param[in] BnLength Length of big number buffer in bytes.\r
166\r
167 @return TRUE RSA key component was set successfully.\r
168 @return FALSE Invalid RSA key component tag.\r
169\r
170**/\r
171BOOLEAN\r
172EFIAPI\r
173RuntimeCryptRsaSetKey (\r
174 IN OUT VOID *RsaContext,\r
175 IN RSA_KEY_TAG KeyTag,\r
176 IN CONST UINT8 *BigNumber,\r
177 IN UINTN BnLength\r
178 )\r
179{\r
180 return RsaSetKey (RsaContext, KeyTag, BigNumber, BnLength);\r
181}\r
182\r
183/**\r
184 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
185 RSA PKCS#1.\r
186\r
16d2c32c 187 If RsaContext is NULL, then return FALSE.\r
188 If MessageHash is NULL, then return FALSE.\r
189 If Signature is NULL, then return FALSE.\r
190 If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, return FALSE.\r
97f98500
HT
191\r
192 @param[in] RsaContext Pointer to RSA context for signature verification.\r
193 @param[in] MessageHash Pointer to octet message hash to be checked.\r
194 @param[in] HashLength Length of the message hash in bytes.\r
195 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.\r
196 @param[in] SigLength Length of signature in bytes.\r
197\r
198 @return TRUE Valid signature encoded in PKCS1-v1_5.\r
199 @return FALSE Invalid signature or invalid RSA context.\r
200\r
201**/\r
202BOOLEAN\r
203EFIAPI\r
204RuntimeCryptRsaPkcs1Verify (\r
205 IN VOID *RsaContext,\r
206 IN CONST UINT8 *MessageHash,\r
207 IN UINTN HashLength,\r
8c5720b4 208 IN CONST UINT8 *Signature,\r
97f98500
HT
209 IN UINTN SigLength\r
210 )\r
211{\r
212 return RsaPkcs1Verify (RsaContext, MessageHash, HashLength, Signature, SigLength);\r
213}\r
214\r
215/**\r
216 Entry Point for Runtime Cryptographic Driver.\r
217\r
218 This function installs Runtime Crypt Protocol.\r
219\r
220 @param ImageHandle Image handle of this driver.\r
221 @param SystemTable a Pointer to the EFI System Table.\r
222\r
223 @retval EFI_SUCEESS Runtime Crypt Protocol is successfully installed\r
224 @return Others Some error occurs when installing Runtime Crypt Protocol.\r
225\r
226**/\r
227EFI_STATUS\r
228EFIAPI\r
229CryptRuntimeDriverInitialize (\r
230 IN EFI_HANDLE ImageHandle,\r
231 IN EFI_SYSTEM_TABLE *SystemTable\r
232 )\r
233{\r
234 EFI_STATUS Status;\r
235\r
236 //\r
237 // Install the Runtime Crypt Protocol onto a new handle\r
238 //\r
239 Status = gBS->InstallMultipleProtocolInterfaces (\r
240 &mRuntimeCryptHandle,\r
241 &gEfiRuntimeCryptProtocolGuid,\r
242 &mRuntimeCryptProtocol,\r
243 NULL\r
244 );\r
245 ASSERT_EFI_ERROR (Status);\r
246\r
247 return Status;\r
248}\r