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