]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptMd5.c
CryptoPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptMd5.c
CommitLineData
97f98500
HT
1/** @file\r
2 MD5 Digest Wrapper Implementation over OpenSSL.\r
3\r
b7d1ba0a 4Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
2009f6b4 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
97f98500
HT
6\r
7**/\r
8\r
a8c44645 9#include "InternalCryptLib.h"\r
97f98500
HT
10#include <openssl/md5.h>\r
11\r
12\r
13/**\r
14 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.\r
15\r
16 @return The size, in bytes, of the context buffer required for MD5 hash operations.\r
17\r
18**/\r
19UINTN\r
20EFIAPI\r
21Md5GetContextSize (\r
22 VOID\r
23 )\r
24{\r
25 //\r
26 // Retrieves the OpenSSL MD5 Context Size\r
27 //\r
6b8ebcb8 28 return (UINTN) (sizeof (MD5_CTX));\r
97f98500
HT
29}\r
30\r
31\r
32/**\r
33 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for\r
34 subsequent use.\r
35\r
16d2c32c 36 If Md5Context is NULL, then return FALSE.\r
97f98500 37\r
a8c44645 38 @param[out] Md5Context Pointer to MD5 context being initialized.\r
97f98500
HT
39\r
40 @retval TRUE MD5 context initialization succeeded.\r
41 @retval FALSE MD5 context initialization failed.\r
42\r
43**/\r
44BOOLEAN\r
45EFIAPI\r
46Md5Init (\r
a8c44645 47 OUT VOID *Md5Context\r
97f98500
HT
48 )\r
49{\r
50 //\r
16d2c32c 51 // Check input parameters.\r
97f98500 52 //\r
8ca8dd9b 53 if (Md5Context == NULL) {\r
16d2c32c 54 return FALSE;\r
55 }\r
97f98500
HT
56\r
57 //\r
58 // OpenSSL MD5 Context Initialization\r
59 //\r
6b8ebcb8 60 return (BOOLEAN) (MD5_Init ((MD5_CTX *) Md5Context));\r
97f98500
HT
61}\r
62\r
a8c44645 63/**\r
64 Makes a copy of an existing MD5 context.\r
65\r
16d2c32c 66 If Md5Context is NULL, then return FALSE.\r
67 If NewMd5Context is NULL, then return FALSE.\r
a8c44645 68\r
69 @param[in] Md5Context Pointer to MD5 context being copied.\r
70 @param[out] NewMd5Context Pointer to new MD5 context.\r
71\r
72 @retval TRUE MD5 context copy succeeded.\r
73 @retval FALSE MD5 context copy failed.\r
74\r
75**/\r
76BOOLEAN\r
77EFIAPI\r
78Md5Duplicate (\r
79 IN CONST VOID *Md5Context,\r
80 OUT VOID *NewMd5Context\r
81 )\r
82{\r
4a567c96 83 //\r
16d2c32c 84 // Check input parameters.\r
4a567c96 85 //\r
16d2c32c 86 if (Md5Context == NULL || NewMd5Context == NULL) {\r
87 return FALSE;\r
88 }\r
4a567c96 89\r
a8c44645 90 CopyMem (NewMd5Context, Md5Context, sizeof (MD5_CTX));\r
91\r
92 return TRUE;\r
93}\r
97f98500
HT
94\r
95/**\r
a8c44645 96 Digests the input data and updates MD5 context.\r
97\r
98 This function performs MD5 digest on a data buffer of the specified size.\r
99 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
2998af86 100 MD5 context should be already correctly initialized by Md5Init(), and should not be finalized\r
a8c44645 101 by Md5Final(). Behavior with invalid context is undefined.\r
97f98500 102\r
16d2c32c 103 If Md5Context is NULL, then return FALSE.\r
97f98500
HT
104\r
105 @param[in, out] Md5Context Pointer to the MD5 context.\r
106 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
a8c44645 107 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
108\r
109 @retval TRUE MD5 data digest succeeded.\r
a8c44645 110 @retval FALSE MD5 data digest failed.\r
97f98500
HT
111\r
112**/\r
113BOOLEAN\r
114EFIAPI\r
115Md5Update (\r
116 IN OUT VOID *Md5Context,\r
117 IN CONST VOID *Data,\r
a8c44645 118 IN UINTN DataSize\r
97f98500
HT
119 )\r
120{\r
121 //\r
16d2c32c 122 // Check input parameters.\r
97f98500 123 //\r
16d2c32c 124 if (Md5Context == NULL) {\r
125 return FALSE;\r
126 }\r
97f98500
HT
127\r
128 //\r
16d2c32c 129 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
97f98500 130 //\r
16d2c32c 131 if (Data == NULL && (DataSize != 0)) {\r
132 return FALSE;\r
97f98500
HT
133 }\r
134\r
135 //\r
136 // OpenSSL MD5 Hash Update\r
137 //\r
6b8ebcb8 138 return (BOOLEAN) (MD5_Update ((MD5_CTX *) Md5Context, Data, DataSize));\r
97f98500
HT
139}\r
140\r
97f98500 141/**\r
a8c44645 142 Completes computation of the MD5 digest value.\r
143\r
144 This function completes MD5 hash computation and retrieves the digest value into\r
145 the specified memory. After this function has been called, the MD5 context cannot\r
146 be used again.\r
2998af86 147 MD5 context should be already correctly initialized by Md5Init(), and should not be\r
a8c44645 148 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.\r
97f98500 149\r
16d2c32c 150 If Md5Context is NULL, then return FALSE.\r
151 If HashValue is NULL, then return FALSE.\r
97f98500 152\r
a8c44645 153 @param[in, out] Md5Context Pointer to the MD5 context.\r
97f98500
HT
154 @param[out] HashValue Pointer to a buffer that receives the MD5 digest\r
155 value (16 bytes).\r
156\r
157 @retval TRUE MD5 digest computation succeeded.\r
158 @retval FALSE MD5 digest computation failed.\r
159\r
160**/\r
161BOOLEAN\r
162EFIAPI\r
163Md5Final (\r
164 IN OUT VOID *Md5Context,\r
165 OUT UINT8 *HashValue\r
166 )\r
167{\r
168 //\r
16d2c32c 169 // Check input parameters.\r
97f98500 170 //\r
16d2c32c 171 if (Md5Context == NULL || HashValue == NULL) {\r
172 return FALSE;\r
173 }\r
97f98500
HT
174\r
175 //\r
176 // OpenSSL MD5 Hash Finalization\r
177 //\r
6b8ebcb8 178 return (BOOLEAN) (MD5_Final (HashValue, (MD5_CTX *) Md5Context));\r
97f98500 179}\r
b7d1ba0a
QL
180\r
181/**\r
182 Computes the MD5 message digest of a input data buffer.\r
183\r
184 This function performs the MD5 message digest of a given data buffer, and places\r
185 the digest value into the specified memory.\r
186\r
187 If this interface is not supported, then return FALSE.\r
188\r
189 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
190 @param[in] DataSize Size of Data buffer in bytes.\r
191 @param[out] HashValue Pointer to a buffer that receives the MD5 digest\r
192 value (16 bytes).\r
193\r
194 @retval TRUE MD5 digest computation succeeded.\r
195 @retval FALSE MD5 digest computation failed.\r
196 @retval FALSE This interface is not supported.\r
197\r
198**/\r
199BOOLEAN\r
200EFIAPI\r
201Md5HashAll (\r
202 IN CONST VOID *Data,\r
203 IN UINTN DataSize,\r
204 OUT UINT8 *HashValue\r
205 )\r
206{\r
207 //\r
208 // Check input parameters.\r
209 //\r
210 if (HashValue == NULL) {\r
211 return FALSE;\r
212 }\r
213 if (Data == NULL && (DataSize != 0)) {\r
214 return FALSE;\r
215 }\r
216\r
217 //\r
218 // OpenSSL MD5 Hash Computation.\r
219 //\r
220 if (MD5 (Data, DataSize, HashValue) == NULL) {\r
221 return FALSE;\r
222 } else {\r
223 return TRUE;\r
224 }\r
225}\r