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