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