]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hash/CryptMd4.c
a5769133ed1efb160ef0ed98fc1e5d9bf1036fd2
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptMd4.c
1 /** @file
2 MD4 Digest Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010, 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/md4.h>
17
18 /**
19 Retrieves the size, in bytes, of the context buffer required for MD4 hash operations.
20
21 @return The size, in bytes, of the context buffer required for MD4 hash operations.
22
23 **/
24 UINTN
25 EFIAPI
26 Md4GetContextSize (
27 VOID
28 )
29 {
30 //
31 // Retrieves the OpenSSL MD4 Context Size
32 //
33 return (UINTN)(sizeof (MD4_CTX));
34 }
35
36 /**
37 Initializes user-supplied memory pointed by Md4Context as MD4 hash context for
38 subsequent use.
39
40 If Md4Context is NULL, then ASSERT().
41
42 @param[out] Md4Context Pointer to MD4 context being initialized.
43
44 @retval TRUE MD4 context initialization succeeded.
45 @retval FALSE MD4 context initialization failed.
46
47 **/
48 BOOLEAN
49 EFIAPI
50 Md4Init (
51 OUT VOID *Md4Context
52 )
53 {
54 //
55 // ASSERT if Md4Context is NULL.
56 //
57 ASSERT (Md4Context != NULL);
58
59 //
60 // OpenSSL MD4 Context Initialization
61 //
62 return (BOOLEAN) (MD4_Init ((MD4_CTX *)Md4Context));
63 }
64
65 /**
66 Makes a copy of an existing MD4 context.
67
68 If Md4Context is NULL, then ASSERT().
69 If NewMd4Context is NULL, then ASSERT().
70
71 @param[in] Md4Context Pointer to MD4 context being copied.
72 @param[out] NewMd4Context Pointer to new MD4 context.
73
74 @retval TRUE MD4 context copy succeeded.
75 @retval FALSE MD4 context copy failed.
76
77 **/
78 BOOLEAN
79 EFIAPI
80 Md4Duplicate (
81 IN CONST VOID *Md4Context,
82 OUT VOID *NewMd4Context
83 )
84 {
85 //
86 // ASSERT if Md4Context or NewMd4Context is NULL.
87 //
88 ASSERT (Md4Context != NULL);
89 ASSERT (NewMd4Context != NULL);
90
91 CopyMem (NewMd4Context, Md4Context, sizeof (MD4_CTX));
92
93 return TRUE;
94 }
95
96 /**
97 Digests the input data and updates MD4 context.
98
99 This function performs MD4 digest on a data buffer of the specified size.
100 It can be called multiple times to compute the digest of long or discontinuous data streams.
101 MD4 context should be already correctly intialized by Md4Init(), and should not be finalized
102 by Md4Final(). Behavior with invalid context is undefined.
103
104 If Md4Context is NULL, then ASSERT().
105
106 @param[in, out] Md4Context Pointer to the MD4 context.
107 @param[in] Data Pointer to the buffer containing the data to be hashed.
108 @param[in] DataSize Size of Data buffer in bytes.
109
110 @retval TRUE MD4 data digest succeeded.
111 @retval FALSE MD4 data digest failed.
112
113 **/
114 BOOLEAN
115 EFIAPI
116 Md4Update (
117 IN OUT VOID *Md4Context,
118 IN CONST VOID *Data,
119 IN UINTN DataSize
120 )
121 {
122 //
123 // ASSERT if Md4Context is NULL
124 //
125 ASSERT (Md4Context != NULL);
126
127 //
128 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL
129 //
130 if (Data == NULL) {
131 ASSERT (DataSize == 0);
132 }
133
134 //
135 // OpenSSL MD4 Hash Update
136 //
137 return (BOOLEAN) (MD4_Update ((MD4_CTX *)Md4Context, Data, DataSize));
138 }
139
140 /**
141 Completes computation of the MD4 digest value.
142
143 This function completes MD4 hash computation and retrieves the digest value into
144 the specified memory. After this function has been called, the MD4 context cannot
145 be used again.
146 MD4 context should be already correctly intialized by Md4Init(), and should not be
147 finalized by Md4Final(). Behavior with invalid MD4 context is undefined.
148
149 If Md4Context is NULL, then ASSERT().
150 If HashValue is NULL, then ASSERT().
151
152 @param[in, out] Md4Context Pointer to the MD4 context.
153 @param[out] HashValue Pointer to a buffer that receives the MD4 digest
154 value (16 bytes).
155
156 @retval TRUE MD4 digest computation succeeded.
157 @retval FALSE MD4 digest computation failed.
158
159 **/
160 BOOLEAN
161 EFIAPI
162 Md4Final (
163 IN OUT VOID *Md4Context,
164 OUT UINT8 *HashValue
165 )
166 {
167 //
168 // ASSERT if Md4Context is NULL or HashValue is NULL
169 //
170 ASSERT (Md4Context != NULL);
171 ASSERT (HashValue != NULL);
172
173 //
174 // OpenSSL MD4 Hash Finalization
175 //
176 return (BOOLEAN) (MD4_Final (HashValue, (MD4_CTX *)Md4Context));
177 }