]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptMd5.c
1. Remove conducting ASSERT in BaseCryptLib.
[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
16d2c32c 4Copyright (c) 2009 - 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
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
25UINTN\r
26EFIAPI\r
27Md5GetContextSize (\r
28 VOID\r
29 )\r
30{\r
31 //\r
32 // Retrieves the OpenSSL MD5 Context Size\r
33 //\r
34 return (UINTN)(sizeof (MD5_CTX));\r
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
50BOOLEAN\r
51EFIAPI\r
52Md5Init (\r
a8c44645 53 OUT VOID *Md5Context\r
97f98500
HT
54 )\r
55{\r
56 //\r
16d2c32c 57 // Check input parameters.\r
97f98500 58 //\r
16d2c32c 59 if ((Md5Context == NULL)) {\r
60 return FALSE;\r
61 }\r
97f98500
HT
62\r
63 //\r
64 // OpenSSL MD5 Context Initialization\r
65 //\r
66 return (BOOLEAN) (MD5_Init ((MD5_CTX *)Md5Context));\r
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
82BOOLEAN\r
83EFIAPI\r
84Md5Duplicate (\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
119BOOLEAN\r
120EFIAPI\r
121Md5Update (\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
a8c44645 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
167BOOLEAN\r
168EFIAPI\r
169Md5Final (\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
184 return (BOOLEAN) (MD5_Final (HashValue, (MD5_CTX *)Md5Context));\r
185}\r