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